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

321 lines
7.8 KiB
TypeScript
Raw Normal View History

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