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

319 lines
7.4 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'
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: 200
})
}
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: 200
})
}
function getAccountPlaylistsList (url: string, accountName: string, start: number, count: number, sort?: string) {
const path = '/api/v1/accounts/' + accountName + '/video-playlists'
const query = {
start,
count,
sort
}
return makeGetRequest({
url,
path,
query,
statusCodeExpected: 200
})
}
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: 200
2019-02-26 04:55:40 -05:00
})
2019-01-29 02:37:25 -05:00
}
2019-02-26 04:55:40 -05:00
function getVideoPlaylist (url: string, playlistId: number | string, statusCodeExpected = 200) {
const path = '/api/v1/video-playlists/' + playlistId
return makeGetRequest({
url,
path,
statusCodeExpected
})
2019-01-29 02:37:25 -05:00
}
2019-03-05 04:58:44 -05:00
function getVideoPlaylistWithToken (url: string, token: string, playlistId: number | string, statusCodeExpected = 200) {
const path = '/api/v1/video-playlists/' + playlistId
return makeGetRequest({
url,
token,
path,
statusCodeExpected
})
}
2019-02-28 05:14:26 -05:00
function deleteVideoPlaylist (url: string, token: string, playlistId: number | string, statusCodeExpected = 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: {
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,
2019-02-28 05:14:26 -05:00
statusCodeExpected: options.expectedStatus || 200
2019-02-26 04:55:40 -05:00
})
}
2019-02-26 04:55:40 -05:00
function updateVideoPlaylist (options: {
url: string,
token: string,
playlistAttrs: VideoPlaylistUpdate,
2019-02-28 05:14:26 -05:00
playlistId: number | string,
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,
2019-02-28 05:14:26 -05:00
statusCodeExpected: options.expectedStatus || 204
2019-02-26 04:55:40 -05:00
})
}
2019-03-05 04:58:44 -05:00
async function addVideoInPlaylist (options: {
2019-02-26 04:55:40 -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,
2019-02-28 05:14:26 -05:00
statusCodeExpected: options.expectedStatus || 200
2019-02-26 04:55:40 -05:00
})
}
function updateVideoPlaylistElement (options: {
url: string,
token: string,
playlistId: number | string,
videoId: number | string,
elementAttrs: VideoPlaylistElementUpdate,
2019-02-28 05:14:26 -05:00
expectedStatus?: number
2019-02-26 04:55:40 -05:00
}) {
const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.videoId
return makePutBodyRequest({
url: options.url,
path,
token: options.token,
fields: options.elementAttrs,
2019-02-28 05:14:26 -05:00
statusCodeExpected: options.expectedStatus || 204
2019-02-26 04:55:40 -05:00
})
}
function removeVideoFromPlaylist (options: {
url: string,
token: string,
playlistId: number | string,
videoId: number | string,
2019-03-05 04:58:44 -05:00
expectedStatus?: number
2019-02-26 04:55:40 -05:00
}) {
const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.videoId
return makeDeleteRequest({
url: options.url,
path,
token: options.token,
2019-02-28 05:14:26 -05:00
statusCodeExpected: options.expectedStatus || 204
2019-02-26 04:55:40 -05:00
})
}
function reorderVideosPlaylist (options: {
url: string,
token: string,
playlistId: number | string,
elementAttrs: {
startPosition: number,
2019-02-28 05:14:26 -05:00
insertAfterPosition: number,
2019-02-26 04:55:40 -05:00
reorderLength?: number
},
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,
2019-03-05 04:58:44 -05:00
statusCodeExpected: options.expectedStatus || 204
2019-02-26 04:55:40 -05:00
})
}
2019-03-05 04:58:44 -05:00
async function checkPlaylistFilesWereRemoved (
playlistUUID: string,
serverNumber: number,
directories = [ 'thumbnails' ]
) {
const testDirectory = 'test' + serverNumber
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: 200
})
}
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: 200
})
}
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
}