]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/videos/video-playlists.ts
Merge branch 'release/2.1.0' into develop
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / videos / video-playlists.ts
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'
13
14 function getVideoPlaylistsList (url: string, start: number, count: number, sort?: string) {
15 const path = '/api/v1/video-playlists'
16
17 const query = {
18 start,
19 count,
20 sort
21 }
22
23 return makeGetRequest({
24 url,
25 path,
26 query,
27 statusCodeExpected: 200
28 })
29 }
30
31 function getVideoChannelPlaylistsList (url: string, videoChannelName: string, start: number, count: number, sort?: string) {
32 const path = '/api/v1/video-channels/' + videoChannelName + '/video-playlists'
33
34 const query = {
35 start,
36 count,
37 sort
38 }
39
40 return makeGetRequest({
41 url,
42 path,
43 query,
44 statusCodeExpected: 200
45 })
46 }
47
48 function getAccountPlaylistsList (url: string, accountName: string, start: number, count: number, sort?: string, search?: string) {
49 const path = '/api/v1/accounts/' + accountName + '/video-playlists'
50
51 const query = {
52 start,
53 count,
54 sort,
55 search
56 }
57
58 return makeGetRequest({
59 url,
60 path,
61 query,
62 statusCodeExpected: 200
63 })
64 }
65
66 function getAccountPlaylistsListWithToken (
67 url: string,
68 token: string,
69 accountName: string,
70 start: number,
71 count: number,
72 playlistType?: VideoPlaylistType,
73 sort?: string
74 ) {
75 const path = '/api/v1/accounts/' + accountName + '/video-playlists'
76
77 const query = {
78 start,
79 count,
80 playlistType,
81 sort
82 }
83
84 return makeGetRequest({
85 url,
86 token,
87 path,
88 query,
89 statusCodeExpected: 200
90 })
91 }
92
93 function getVideoPlaylist (url: string, playlistId: number | string, statusCodeExpected = 200) {
94 const path = '/api/v1/video-playlists/' + playlistId
95
96 return makeGetRequest({
97 url,
98 path,
99 statusCodeExpected
100 })
101 }
102
103 function getVideoPlaylistWithToken (url: string, token: string, playlistId: number | string, statusCodeExpected = 200) {
104 const path = '/api/v1/video-playlists/' + playlistId
105
106 return makeGetRequest({
107 url,
108 token,
109 path,
110 statusCodeExpected
111 })
112 }
113
114 function deleteVideoPlaylist (url: string, token: string, playlistId: number | string, statusCodeExpected = 204) {
115 const path = '/api/v1/video-playlists/' + playlistId
116
117 return makeDeleteRequest({
118 url,
119 path,
120 token,
121 statusCodeExpected
122 })
123 }
124
125 function createVideoPlaylist (options: {
126 url: string
127 token: string
128 playlistAttrs: VideoPlaylistCreate
129 expectedStatus?: number
130 }) {
131 const path = '/api/v1/video-playlists'
132
133 const fields = omit(options.playlistAttrs, 'thumbnailfile')
134
135 const attaches = options.playlistAttrs.thumbnailfile
136 ? { thumbnailfile: options.playlistAttrs.thumbnailfile }
137 : {}
138
139 return makeUploadRequest({
140 method: 'POST',
141 url: options.url,
142 path,
143 token: options.token,
144 fields,
145 attaches,
146 statusCodeExpected: options.expectedStatus || 200
147 })
148 }
149
150 function updateVideoPlaylist (options: {
151 url: string
152 token: string
153 playlistAttrs: VideoPlaylistUpdate
154 playlistId: number | string
155 expectedStatus?: number
156 }) {
157 const path = '/api/v1/video-playlists/' + options.playlistId
158
159 const fields = omit(options.playlistAttrs, 'thumbnailfile')
160
161 const attaches = options.playlistAttrs.thumbnailfile
162 ? { thumbnailfile: options.playlistAttrs.thumbnailfile }
163 : {}
164
165 return makeUploadRequest({
166 method: 'PUT',
167 url: options.url,
168 path,
169 token: options.token,
170 fields,
171 attaches,
172 statusCodeExpected: options.expectedStatus || 204
173 })
174 }
175
176 async function addVideoInPlaylist (options: {
177 url: string
178 token: string
179 playlistId: number | string
180 elementAttrs: VideoPlaylistElementCreate | { videoId: string }
181 expectedStatus?: number
182 }) {
183 options.elementAttrs.videoId = await videoUUIDToId(options.url, options.elementAttrs.videoId)
184
185 const path = '/api/v1/video-playlists/' + options.playlistId + '/videos'
186
187 return makePostBodyRequest({
188 url: options.url,
189 path,
190 token: options.token,
191 fields: options.elementAttrs,
192 statusCodeExpected: options.expectedStatus || 200
193 })
194 }
195
196 function updateVideoPlaylistElement (options: {
197 url: string
198 token: string
199 playlistId: number | string
200 playlistElementId: number | string
201 elementAttrs: VideoPlaylistElementUpdate
202 expectedStatus?: number
203 }) {
204 const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.playlistElementId
205
206 return makePutBodyRequest({
207 url: options.url,
208 path,
209 token: options.token,
210 fields: options.elementAttrs,
211 statusCodeExpected: options.expectedStatus || 204
212 })
213 }
214
215 function removeVideoFromPlaylist (options: {
216 url: string
217 token: string
218 playlistId: number | string
219 playlistElementId: number
220 expectedStatus?: number
221 }) {
222 const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.playlistElementId
223
224 return makeDeleteRequest({
225 url: options.url,
226 path,
227 token: options.token,
228 statusCodeExpected: options.expectedStatus || 204
229 })
230 }
231
232 function reorderVideosPlaylist (options: {
233 url: string
234 token: string
235 playlistId: number | string
236 elementAttrs: {
237 startPosition: number
238 insertAfterPosition: number
239 reorderLength?: number
240 }
241 expectedStatus?: number
242 }) {
243 const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/reorder'
244
245 return makePostBodyRequest({
246 url: options.url,
247 path,
248 token: options.token,
249 fields: options.elementAttrs,
250 statusCodeExpected: options.expectedStatus || 204
251 })
252 }
253
254 async function checkPlaylistFilesWereRemoved (
255 playlistUUID: string,
256 internalServerNumber: number,
257 directories = [ 'thumbnails' ]
258 ) {
259 const testDirectory = 'test' + internalServerNumber
260
261 for (const directory of directories) {
262 const directoryPath = join(root(), testDirectory, directory)
263
264 const files = await readdir(directoryPath)
265 for (const file of files) {
266 expect(file).to.not.contain(playlistUUID)
267 }
268 }
269 }
270
271 function getVideoPlaylistPrivacies (url: string) {
272 const path = '/api/v1/video-playlists/privacies'
273
274 return makeGetRequest({
275 url,
276 path,
277 statusCodeExpected: 200
278 })
279 }
280
281 function doVideosExistInMyPlaylist (url: string, token: string, videoIds: number[]) {
282 const path = '/api/v1/users/me/video-playlists/videos-exist'
283
284 return makeGetRequest({
285 url,
286 token,
287 path,
288 query: { videoIds },
289 statusCodeExpected: 200
290 })
291 }
292
293 // ---------------------------------------------------------------------------
294
295 export {
296 getVideoPlaylistPrivacies,
297
298 getVideoPlaylistsList,
299 getVideoChannelPlaylistsList,
300 getAccountPlaylistsList,
301 getAccountPlaylistsListWithToken,
302
303 getVideoPlaylist,
304 getVideoPlaylistWithToken,
305
306 createVideoPlaylist,
307 updateVideoPlaylist,
308 deleteVideoPlaylist,
309
310 addVideoInPlaylist,
311 updateVideoPlaylistElement,
312 removeVideoFromPlaylist,
313
314 reorderVideosPlaylist,
315
316 checkPlaylistFilesWereRemoved,
317
318 doVideosExistInMyPlaylist
319 }