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