1 import { makeDeleteRequest, makeGetRequest, makePostBodyRequest, makePutBodyRequest, makeUploadRequest } from '../requests/requests'
2 import { VideoPlaylistCreate } from '../../models/videos/playlist/video-playlist-create.model'
3 import { omit } from 'lodash'
4 import { VideoPlaylistUpdate } from '../../models/videos/playlist/video-playlist-update.model'
5 import { VideoPlaylistElementCreate } from '../../models/videos/playlist/video-playlist-element-create.model'
6 import { VideoPlaylistElementUpdate } from '../../models/videos/playlist/video-playlist-element-update.model'
7 import { videoUUIDToId } from './videos'
8 import { join } from 'path'
9 import { root } from '..'
10 import { readdir } from 'fs-extra'
11 import { expect } from 'chai'
12 import { VideoPlaylistType } from '../../models/videos/playlist/video-playlist-type.model'
14 function getVideoPlaylistsList (url: string, start: number, count: number, sort?: string) {
15 const path = '/api/v1/video-playlists'
23 return makeGetRequest({
27 statusCodeExpected: 200
31 function getVideoChannelPlaylistsList (url: string, videoChannelName: string, start: number, count: number, sort?: string) {
32 const path = '/api/v1/video-channels/' + videoChannelName + '/video-playlists'
40 return makeGetRequest({
44 statusCodeExpected: 200
48 function getAccountPlaylistsList (url: string, accountName: string, start: number, count: number, sort?: string, search?: string) {
49 const path = '/api/v1/accounts/' + accountName + '/video-playlists'
58 return makeGetRequest({
62 statusCodeExpected: 200
66 function getAccountPlaylistsListWithToken (
72 playlistType?: VideoPlaylistType,
75 const path = '/api/v1/accounts/' + accountName + '/video-playlists'
84 return makeGetRequest({
89 statusCodeExpected: 200
93 function getVideoPlaylist (url: string, playlistId: number | string, statusCodeExpected = 200) {
94 const path = '/api/v1/video-playlists/' + playlistId
96 return makeGetRequest({
103 function getVideoPlaylistWithToken (url: string, token: string, playlistId: number | string, statusCodeExpected = 200) {
104 const path = '/api/v1/video-playlists/' + playlistId
106 return makeGetRequest({
114 function deleteVideoPlaylist (url: string, token: string, playlistId: number | string, statusCodeExpected = 204) {
115 const path = '/api/v1/video-playlists/' + playlistId
117 return makeDeleteRequest({
125 function createVideoPlaylist (options: {
128 playlistAttrs: VideoPlaylistCreate
129 expectedStatus?: number
131 const path = '/api/v1/video-playlists'
133 const fields = omit(options.playlistAttrs, 'thumbnailfile')
135 const attaches = options.playlistAttrs.thumbnailfile
136 ? { thumbnailfile: options.playlistAttrs.thumbnailfile }
139 return makeUploadRequest({
143 token: options.token,
146 statusCodeExpected: options.expectedStatus || 200
150 function updateVideoPlaylist (options: {
153 playlistAttrs: VideoPlaylistUpdate
154 playlistId: number | string
155 expectedStatus?: number
157 const path = '/api/v1/video-playlists/' + options.playlistId
159 const fields = omit(options.playlistAttrs, 'thumbnailfile')
161 const attaches = options.playlistAttrs.thumbnailfile
162 ? { thumbnailfile: options.playlistAttrs.thumbnailfile }
165 return makeUploadRequest({
169 token: options.token,
172 statusCodeExpected: options.expectedStatus || 204
176 async function addVideoInPlaylist (options: {
179 playlistId: number | string
180 elementAttrs: VideoPlaylistElementCreate | { videoId: string }
181 expectedStatus?: number
183 options.elementAttrs.videoId = await videoUUIDToId(options.url, options.elementAttrs.videoId)
185 const path = '/api/v1/video-playlists/' + options.playlistId + '/videos'
187 return makePostBodyRequest({
190 token: options.token,
191 fields: options.elementAttrs,
192 statusCodeExpected: options.expectedStatus || 200
196 function updateVideoPlaylistElement (options: {
199 playlistId: number | string
200 playlistElementId: number | string
201 elementAttrs: VideoPlaylistElementUpdate
202 expectedStatus?: number
204 const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.playlistElementId
206 return makePutBodyRequest({
209 token: options.token,
210 fields: options.elementAttrs,
211 statusCodeExpected: options.expectedStatus || 204
215 function removeVideoFromPlaylist (options: {
218 playlistId: number | string
219 playlistElementId: number
220 expectedStatus?: number
222 const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.playlistElementId
224 return makeDeleteRequest({
227 token: options.token,
228 statusCodeExpected: options.expectedStatus || 204
232 function reorderVideosPlaylist (options: {
235 playlistId: number | string
237 startPosition: number
238 insertAfterPosition: number
239 reorderLength?: number
241 expectedStatus?: number
243 const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/reorder'
245 return makePostBodyRequest({
248 token: options.token,
249 fields: options.elementAttrs,
250 statusCodeExpected: options.expectedStatus || 204
254 async function checkPlaylistFilesWereRemoved (
255 playlistUUID: string,
256 internalServerNumber: number,
257 directories = [ 'thumbnails' ]
259 const testDirectory = 'test' + internalServerNumber
261 for (const directory of directories) {
262 const directoryPath = join(root(), testDirectory, directory)
264 const files = await readdir(directoryPath)
265 for (const file of files) {
266 expect(file).to.not.contain(playlistUUID)
271 function getVideoPlaylistPrivacies (url: string) {
272 const path = '/api/v1/video-playlists/privacies'
274 return makeGetRequest({
277 statusCodeExpected: 200
281 function doVideosExistInMyPlaylist (url: string, token: string, videoIds: number[]) {
282 const path = '/api/v1/users/me/video-playlists/videos-exist'
284 return makeGetRequest({
289 statusCodeExpected: 200
293 // ---------------------------------------------------------------------------
296 getVideoPlaylistPrivacies,
298 getVideoPlaylistsList,
299 getVideoChannelPlaylistsList,
300 getAccountPlaylistsList,
301 getAccountPlaylistsListWithToken,
304 getVideoPlaylistWithToken,
311 updateVideoPlaylistElement,
312 removeVideoFromPlaylist,
314 reorderVideosPlaylist,
316 checkPlaylistFilesWereRemoved,
318 doVideosExistInMyPlaylist