1
0
Fork 0
peertube/shared/extra-utils/videos/video-playlists.ts

321 lines
7.8 KiB
TypeScript
Raw Normal View History

2019-02-26 04:55:40 -05:00
import { makeDeleteRequest, makeGetRequest, makePostBodyRequest, makePutBodyRequest, makeUploadRequest } from '../requests/requests'
import { VideoPlaylistCreate } from '../../models/videos/playlist/video-playlist-create.model'
import { omit } from 'lodash'
import { VideoPlaylistUpdate } from '../../models/videos/playlist/video-playlist-update.model'
import { VideoPlaylistElementCreate } from '../../models/videos/playlist/video-playlist-element-create.model'
import { VideoPlaylistElementUpdate } from '../../models/videos/playlist/video-playlist-element-update.model'
2019-03-05 04:58:44 -05:00
import { videoUUIDToId } from './videos'
import { join } from 'path'
import { root } from '..'
import { readdir } from 'fs-extra'
import { expect } from 'chai'
import { VideoPlaylistType } from '../../models/videos/playlist/video-playlist-type.model'
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
2019-01-29 02:37:25 -05:00
2019-02-26 04:55:40 -05:00
function getVideoPlaylistsList (url: string, start: number, count: number, sort?: string) {
const path = '/api/v1/video-playlists'
const query = {
start,
count,
sort
}
return makeGetRequest({
url,
path,
2019-03-05 04:58:44 -05:00
query,
statusCodeExpected: HttpStatusCode.OK_200
2019-03-05 04:58:44 -05:00
})
}
function getVideoChannelPlaylistsList (url: string, videoChannelName: string, start: number, count: number, sort?: string) {
const path = '/api/v1/video-channels/' + videoChannelName + '/video-playlists'
const query = {
start,
count,
sort
}
return makeGetRequest({
url,
path,
query,
statusCodeExpected: HttpStatusCode.OK_200
2019-03-05 04:58:44 -05:00
})
}
2019-12-27 03:04:04 -05:00
function getAccountPlaylistsList (url: string, accountName: string, start: number, count: number, sort?: string, search?: string) {
2019-03-05 04:58:44 -05:00
const path = '/api/v1/accounts/' + accountName + '/video-playlists'
const query = {
start,
count,
2019-12-27 03:04:04 -05:00
sort,
search
2019-03-05 04:58:44 -05:00
}
return makeGetRequest({
url,
path,
query,
statusCodeExpected: HttpStatusCode.OK_200
2019-03-05 04:58:44 -05:00
})
}
function getAccountPlaylistsListWithToken (
url: string,
token: string,
accountName: string,
start: number,
count: number,
2019-03-14 04:19:03 -04:00
playlistType?: VideoPlaylistType,
sort?: string
2019-03-05 04:58:44 -05:00
) {
const path = '/api/v1/accounts/' + accountName + '/video-playlists'
const query = {
start,
count,
2019-03-14 04:19:03 -04:00
playlistType,
sort
2019-03-05 04:58:44 -05:00
}
return makeGetRequest({
url,
token,
path,
query,
statusCodeExpected: HttpStatusCode.OK_200
2019-02-26 04:55:40 -05:00
})
2019-01-29 02:37:25 -05:00
}
function getVideoPlaylist (url: string, playlistId: number | string, statusCodeExpected = HttpStatusCode.OK_200) {
2019-02-26 04:55:40 -05:00
const path = '/api/v1/video-playlists/' + playlistId
return makeGetRequest({
url,
path,
statusCodeExpected
})
2019-01-29 02:37:25 -05:00
}
function getVideoPlaylistWithToken (url: string, token: string, playlistId: number | string, statusCodeExpected = HttpStatusCode.OK_200) {
2019-03-05 04:58:44 -05:00
const path = '/api/v1/video-playlists/' + playlistId
return makeGetRequest({
url,
token,
path,
statusCodeExpected
})
}
function deleteVideoPlaylist (url: string, token: string, playlistId: number | string, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) {
2019-02-26 04:55:40 -05:00
const path = '/api/v1/video-playlists/' + playlistId
return makeDeleteRequest({
url,
path,
token,
statusCodeExpected
})
2019-01-29 02:37:25 -05:00
}
2019-02-26 04:55:40 -05:00
function createVideoPlaylist (options: {
2020-01-31 10:56:52 -05:00
url: string
token: string
playlistAttrs: VideoPlaylistCreate
2019-02-28 05:14:26 -05:00
expectedStatus?: number
2019-02-26 04:55:40 -05:00
}) {
2019-03-19 09:13:53 -04:00
const path = '/api/v1/video-playlists'
2019-02-26 04:55:40 -05:00
const fields = omit(options.playlistAttrs, 'thumbnailfile')
2019-02-26 04:55:40 -05:00
const attaches = options.playlistAttrs.thumbnailfile
? { thumbnailfile: options.playlistAttrs.thumbnailfile }
: {}
2019-02-26 04:55:40 -05:00
return makeUploadRequest({
method: 'POST',
url: options.url,
path,
token: options.token,
fields,
attaches,
statusCodeExpected: options.expectedStatus || HttpStatusCode.OK_200
2019-02-26 04:55:40 -05:00
})
}
2019-02-26 04:55:40 -05:00
function updateVideoPlaylist (options: {
2020-01-31 10:56:52 -05:00
url: string
token: string
playlistAttrs: VideoPlaylistUpdate
playlistId: number | string
2019-02-28 05:14:26 -05:00
expectedStatus?: number
2019-02-26 04:55:40 -05:00
}) {
2019-02-28 05:14:26 -05:00
const path = '/api/v1/video-playlists/' + options.playlistId
2019-02-26 04:55:40 -05:00
const fields = omit(options.playlistAttrs, 'thumbnailfile')
2019-02-26 04:55:40 -05:00
const attaches = options.playlistAttrs.thumbnailfile
? { thumbnailfile: options.playlistAttrs.thumbnailfile }
: {}
2019-02-26 04:55:40 -05:00
return makeUploadRequest({
method: 'PUT',
url: options.url,
path,
token: options.token,
fields,
attaches,
statusCodeExpected: options.expectedStatus || HttpStatusCode.NO_CONTENT_204
2019-02-26 04:55:40 -05:00
})
}
2019-03-05 04:58:44 -05:00
async function addVideoInPlaylist (options: {
2020-01-31 10:56:52 -05:00
url: string
token: string
playlistId: number | string
2019-03-05 04:58:44 -05:00
elementAttrs: VideoPlaylistElementCreate | { videoId: string }
2019-02-28 05:14:26 -05:00
expectedStatus?: number
2019-02-26 04:55:40 -05:00
}) {
2019-03-05 04:58:44 -05:00
options.elementAttrs.videoId = await videoUUIDToId(options.url, options.elementAttrs.videoId)
2019-02-26 04:55:40 -05:00
const path = '/api/v1/video-playlists/' + options.playlistId + '/videos'
return makePostBodyRequest({
url: options.url,
path,
token: options.token,
fields: options.elementAttrs,
statusCodeExpected: options.expectedStatus || HttpStatusCode.OK_200
2019-02-26 04:55:40 -05:00
})
}
function updateVideoPlaylistElement (options: {
2020-01-31 10:56:52 -05:00
url: string
token: string
playlistId: number | string
playlistElementId: number | string
elementAttrs: VideoPlaylistElementUpdate
2019-02-28 05:14:26 -05:00
expectedStatus?: number
2019-02-26 04:55:40 -05:00
}) {
2019-07-31 09:57:32 -04:00
const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.playlistElementId
2019-02-26 04:55:40 -05:00
return makePutBodyRequest({
url: options.url,
path,
token: options.token,
fields: options.elementAttrs,
statusCodeExpected: options.expectedStatus || HttpStatusCode.NO_CONTENT_204
2019-02-26 04:55:40 -05:00
})
}
function removeVideoFromPlaylist (options: {
2020-01-31 10:56:52 -05:00
url: string
token: string
playlistId: number | string
playlistElementId: number
2019-03-05 04:58:44 -05:00
expectedStatus?: number
2019-02-26 04:55:40 -05:00
}) {
2019-07-31 09:57:32 -04:00
const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.playlistElementId
2019-02-26 04:55:40 -05:00
return makeDeleteRequest({
url: options.url,
path,
token: options.token,
statusCodeExpected: options.expectedStatus || HttpStatusCode.NO_CONTENT_204
2019-02-26 04:55:40 -05:00
})
}
function reorderVideosPlaylist (options: {
2020-01-31 10:56:52 -05:00
url: string
token: string
playlistId: number | string
2019-02-26 04:55:40 -05:00
elementAttrs: {
2020-01-31 10:56:52 -05:00
startPosition: number
insertAfterPosition: number
2019-02-26 04:55:40 -05:00
reorderLength?: number
2020-01-31 10:56:52 -05:00
}
2019-03-05 04:58:44 -05:00
expectedStatus?: number
2019-02-26 04:55:40 -05:00
}) {
2019-02-28 05:14:26 -05:00
const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/reorder'
2019-02-26 04:55:40 -05:00
2019-02-28 05:14:26 -05:00
return makePostBodyRequest({
2019-02-26 04:55:40 -05:00
url: options.url,
path,
token: options.token,
fields: options.elementAttrs,
statusCodeExpected: options.expectedStatus || HttpStatusCode.NO_CONTENT_204
2019-02-26 04:55:40 -05:00
})
}
2019-03-05 04:58:44 -05:00
async function checkPlaylistFilesWereRemoved (
playlistUUID: string,
2019-04-26 02:50:52 -04:00
internalServerNumber: number,
2019-03-05 04:58:44 -05:00
directories = [ 'thumbnails' ]
) {
2019-04-26 02:50:52 -04:00
const testDirectory = 'test' + internalServerNumber
2019-03-05 04:58:44 -05:00
for (const directory of directories) {
const directoryPath = join(root(), testDirectory, directory)
const files = await readdir(directoryPath)
for (const file of files) {
expect(file).to.not.contain(playlistUUID)
}
}
}
2019-03-06 09:36:44 -05:00
function getVideoPlaylistPrivacies (url: string) {
const path = '/api/v1/video-playlists/privacies'
return makeGetRequest({
url,
path,
statusCodeExpected: HttpStatusCode.OK_200
2019-03-06 09:36:44 -05:00
})
}
2019-03-13 11:03:03 -04:00
function doVideosExistInMyPlaylist (url: string, token: string, videoIds: number[]) {
const path = '/api/v1/users/me/video-playlists/videos-exist'
return makeGetRequest({
url,
token,
path,
query: { videoIds },
statusCodeExpected: HttpStatusCode.OK_200
2019-03-13 11:03:03 -04:00
})
}
2019-01-29 02:37:25 -05:00
// ---------------------------------------------------------------------------
export {
2019-03-06 09:36:44 -05:00
getVideoPlaylistPrivacies,
2019-02-26 04:55:40 -05:00
getVideoPlaylistsList,
2019-03-05 04:58:44 -05:00
getVideoChannelPlaylistsList,
getAccountPlaylistsList,
getAccountPlaylistsListWithToken,
2019-02-26 04:55:40 -05:00
getVideoPlaylist,
2019-03-05 04:58:44 -05:00
getVideoPlaylistWithToken,
2019-02-26 04:55:40 -05:00
createVideoPlaylist,
updateVideoPlaylist,
deleteVideoPlaylist,
addVideoInPlaylist,
2019-02-28 05:14:26 -05:00
updateVideoPlaylistElement,
2019-02-26 04:55:40 -05:00
removeVideoFromPlaylist,
2019-03-05 04:58:44 -05:00
reorderVideosPlaylist,
2019-03-13 11:03:03 -04:00
checkPlaylistFilesWereRemoved,
doVideosExistInMyPlaylist
2019-01-29 02:37:25 -05:00
}