PeerTube/shared/extra-utils/videos/video-playlists.ts

319 lines
7.4 KiB
TypeScript
Raw Normal View History

2019-02-26 03:55:40 -06: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 03:58:44 -06: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 01:37:25 -06:00
2019-02-26 03:55:40 -06: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 03:58:44 -06: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 03:19:03 -05:00
playlistType?: VideoPlaylistType,
sort?: string
2019-03-05 03:58:44 -06:00
) {
const path = '/api/v1/accounts/' + accountName + '/video-playlists'
const query = {
start,
count,
2019-03-14 03:19:03 -05:00
playlistType,
sort
2019-03-05 03:58:44 -06:00
}
return makeGetRequest({
url,
token,
path,
query,
statusCodeExpected: 200
2019-02-26 03:55:40 -06:00
})
2019-01-29 01:37:25 -06:00
}
2019-02-26 03:55:40 -06: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 01:37:25 -06:00
}
2019-03-05 03:58:44 -06: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 04:14:26 -06:00
function deleteVideoPlaylist (url: string, token: string, playlistId: number | string, statusCodeExpected = 204) {
2019-02-26 03:55:40 -06:00
const path = '/api/v1/video-playlists/' + playlistId
return makeDeleteRequest({
url,
path,
token,
statusCodeExpected
})
2019-01-29 01:37:25 -06:00
}
2019-02-26 03:55:40 -06:00
function createVideoPlaylist (options: {
url: string,
token: string,
playlistAttrs: VideoPlaylistCreate,
2019-02-28 04:14:26 -06:00
expectedStatus?: number
2019-02-26 03:55:40 -06:00
}) {
2019-03-19 08:13:53 -05:00
const path = '/api/v1/video-playlists'
2019-02-26 03:55:40 -06:00
const fields = omit(options.playlistAttrs, 'thumbnailfile')
2019-02-26 03:55:40 -06:00
const attaches = options.playlistAttrs.thumbnailfile
? { thumbnailfile: options.playlistAttrs.thumbnailfile }
: {}
2019-02-26 03:55:40 -06:00
return makeUploadRequest({
method: 'POST',
url: options.url,
path,
token: options.token,
fields,
attaches,
2019-02-28 04:14:26 -06:00
statusCodeExpected: options.expectedStatus || 200
2019-02-26 03:55:40 -06:00
})
}
2019-02-26 03:55:40 -06:00
function updateVideoPlaylist (options: {
url: string,
token: string,
playlistAttrs: VideoPlaylistUpdate,
2019-02-28 04:14:26 -06:00
playlistId: number | string,
expectedStatus?: number
2019-02-26 03:55:40 -06:00
}) {
2019-02-28 04:14:26 -06:00
const path = '/api/v1/video-playlists/' + options.playlistId
2019-02-26 03:55:40 -06:00
const fields = omit(options.playlistAttrs, 'thumbnailfile')
2019-02-26 03:55:40 -06:00
const attaches = options.playlistAttrs.thumbnailfile
? { thumbnailfile: options.playlistAttrs.thumbnailfile }
: {}
2019-02-26 03:55:40 -06:00
return makeUploadRequest({
method: 'PUT',
url: options.url,
path,
token: options.token,
fields,
attaches,
2019-02-28 04:14:26 -06:00
statusCodeExpected: options.expectedStatus || 204
2019-02-26 03:55:40 -06:00
})
}
2019-03-05 03:58:44 -06:00
async function addVideoInPlaylist (options: {
2019-02-26 03:55:40 -06:00
url: string,
token: string,
playlistId: number | string,
2019-03-05 03:58:44 -06:00
elementAttrs: VideoPlaylistElementCreate | { videoId: string }
2019-02-28 04:14:26 -06:00
expectedStatus?: number
2019-02-26 03:55:40 -06:00
}) {
2019-03-05 03:58:44 -06:00
options.elementAttrs.videoId = await videoUUIDToId(options.url, options.elementAttrs.videoId)
2019-02-26 03:55:40 -06: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 04:14:26 -06:00
statusCodeExpected: options.expectedStatus || 200
2019-02-26 03:55:40 -06:00
})
}
function updateVideoPlaylistElement (options: {
url: string,
token: string,
playlistId: number | string,
videoId: number | string,
elementAttrs: VideoPlaylistElementUpdate,
2019-02-28 04:14:26 -06:00
expectedStatus?: number
2019-02-26 03:55:40 -06: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 04:14:26 -06:00
statusCodeExpected: options.expectedStatus || 204
2019-02-26 03:55:40 -06:00
})
}
function removeVideoFromPlaylist (options: {
url: string,
token: string,
playlistId: number | string,
videoId: number | string,
2019-03-05 03:58:44 -06:00
expectedStatus?: number
2019-02-26 03:55:40 -06:00
}) {
const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.videoId
return makeDeleteRequest({
url: options.url,
path,
token: options.token,
2019-02-28 04:14:26 -06:00
statusCodeExpected: options.expectedStatus || 204
2019-02-26 03:55:40 -06:00
})
}
function reorderVideosPlaylist (options: {
url: string,
token: string,
playlistId: number | string,
elementAttrs: {
startPosition: number,
2019-02-28 04:14:26 -06:00
insertAfterPosition: number,
2019-02-26 03:55:40 -06:00
reorderLength?: number
},
2019-03-05 03:58:44 -06:00
expectedStatus?: number
2019-02-26 03:55:40 -06:00
}) {
2019-02-28 04:14:26 -06:00
const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/reorder'
2019-02-26 03:55:40 -06:00
2019-02-28 04:14:26 -06:00
return makePostBodyRequest({
2019-02-26 03:55:40 -06:00
url: options.url,
path,
token: options.token,
fields: options.elementAttrs,
2019-03-05 03:58:44 -06:00
statusCodeExpected: options.expectedStatus || 204
2019-02-26 03:55:40 -06:00
})
}
2019-03-05 03:58:44 -06:00
async function checkPlaylistFilesWereRemoved (
playlistUUID: string,
2019-04-26 01:50:52 -05:00
internalServerNumber: number,
2019-03-05 03:58:44 -06:00
directories = [ 'thumbnails' ]
) {
2019-04-26 01:50:52 -05:00
const testDirectory = 'test' + internalServerNumber
2019-03-05 03:58:44 -06: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 08:36:44 -06:00
function getVideoPlaylistPrivacies (url: string) {
const path = '/api/v1/video-playlists/privacies'
return makeGetRequest({
url,
path,
statusCodeExpected: 200
})
}
2019-03-13 10:03:03 -05: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 01:37:25 -06:00
// ---------------------------------------------------------------------------
export {
2019-03-06 08:36:44 -06:00
getVideoPlaylistPrivacies,
2019-02-26 03:55:40 -06:00
getVideoPlaylistsList,
2019-03-05 03:58:44 -06:00
getVideoChannelPlaylistsList,
getAccountPlaylistsList,
getAccountPlaylistsListWithToken,
2019-02-26 03:55:40 -06:00
getVideoPlaylist,
2019-03-05 03:58:44 -06:00
getVideoPlaylistWithToken,
2019-02-26 03:55:40 -06:00
createVideoPlaylist,
updateVideoPlaylist,
deleteVideoPlaylist,
addVideoInPlaylist,
2019-02-28 04:14:26 -06:00
updateVideoPlaylistElement,
2019-02-26 03:55:40 -06:00
removeVideoFromPlaylist,
2019-03-05 03:58:44 -06:00
reorderVideosPlaylist,
2019-03-13 10:03:03 -05:00
checkPlaylistFilesWereRemoved,
doVideosExistInMyPlaylist
2019-01-29 01:37:25 -06:00
}