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