]>
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 | ||
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) { | |
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, | |
71 | playlistType?: VideoPlaylistType, | |
72 | sort?: string | |
73 | ) { | |
74 | const path = '/api/v1/accounts/' + accountName + '/video-playlists' | |
75 | ||
76 | const query = { | |
77 | start, | |
78 | count, | |
79 | playlistType, | |
80 | sort | |
81 | } | |
82 | ||
83 | return makeGetRequest({ | |
84 | url, | |
85 | token, | |
86 | path, | |
87 | query, | |
88 | statusCodeExpected: 200 | |
89 | }) | |
90 | } | |
91 | ||
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 | }) | |
100 | } | |
101 | ||
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 | ||
113 | function deleteVideoPlaylist (url: string, token: string, playlistId: number | string, statusCodeExpected = 204) { | |
114 | const path = '/api/v1/video-playlists/' + playlistId | |
115 | ||
116 | return makeDeleteRequest({ | |
117 | url, | |
118 | path, | |
119 | token, | |
120 | statusCodeExpected | |
121 | }) | |
122 | } | |
123 | ||
124 | function createVideoPlaylist (options: { | |
125 | url: string, | |
126 | token: string, | |
127 | playlistAttrs: VideoPlaylistCreate, | |
128 | expectedStatus?: number | |
129 | }) { | |
130 | const path = '/api/v1/video-playlists' | |
131 | ||
132 | const fields = omit(options.playlistAttrs, 'thumbnailfile') | |
133 | ||
134 | const attaches = options.playlistAttrs.thumbnailfile | |
135 | ? { thumbnailfile: options.playlistAttrs.thumbnailfile } | |
136 | : {} | |
137 | ||
138 | return makeUploadRequest({ | |
139 | method: 'POST', | |
140 | url: options.url, | |
141 | path, | |
142 | token: options.token, | |
143 | fields, | |
144 | attaches, | |
145 | statusCodeExpected: options.expectedStatus || 200 | |
146 | }) | |
147 | } | |
148 | ||
149 | function updateVideoPlaylist (options: { | |
150 | url: string, | |
151 | token: string, | |
152 | playlistAttrs: VideoPlaylistUpdate, | |
153 | playlistId: number | string, | |
154 | expectedStatus?: number | |
155 | }) { | |
156 | const path = '/api/v1/video-playlists/' + options.playlistId | |
157 | ||
158 | const fields = omit(options.playlistAttrs, 'thumbnailfile') | |
159 | ||
160 | const attaches = options.playlistAttrs.thumbnailfile | |
161 | ? { thumbnailfile: options.playlistAttrs.thumbnailfile } | |
162 | : {} | |
163 | ||
164 | return makeUploadRequest({ | |
165 | method: 'PUT', | |
166 | url: options.url, | |
167 | path, | |
168 | token: options.token, | |
169 | fields, | |
170 | attaches, | |
171 | statusCodeExpected: options.expectedStatus || 204 | |
172 | }) | |
173 | } | |
174 | ||
175 | async function addVideoInPlaylist (options: { | |
176 | url: string, | |
177 | token: string, | |
178 | playlistId: number | string, | |
179 | elementAttrs: VideoPlaylistElementCreate | { videoId: string } | |
180 | expectedStatus?: number | |
181 | }) { | |
182 | options.elementAttrs.videoId = await videoUUIDToId(options.url, options.elementAttrs.videoId) | |
183 | ||
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, | |
191 | statusCodeExpected: options.expectedStatus || 200 | |
192 | }) | |
193 | } | |
194 | ||
195 | function updateVideoPlaylistElement (options: { | |
196 | url: string, | |
197 | token: string, | |
198 | playlistId: number | string, | |
199 | playlistElementId: number | string, | |
200 | elementAttrs: VideoPlaylistElementUpdate, | |
201 | expectedStatus?: number | |
202 | }) { | |
203 | const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.playlistElementId | |
204 | ||
205 | return makePutBodyRequest({ | |
206 | url: options.url, | |
207 | path, | |
208 | token: options.token, | |
209 | fields: options.elementAttrs, | |
210 | statusCodeExpected: options.expectedStatus || 204 | |
211 | }) | |
212 | } | |
213 | ||
214 | function removeVideoFromPlaylist (options: { | |
215 | url: string, | |
216 | token: string, | |
217 | playlistId: number | string, | |
218 | playlistElementId: number, | |
219 | expectedStatus?: number | |
220 | }) { | |
221 | const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.playlistElementId | |
222 | ||
223 | return makeDeleteRequest({ | |
224 | url: options.url, | |
225 | path, | |
226 | token: options.token, | |
227 | statusCodeExpected: options.expectedStatus || 204 | |
228 | }) | |
229 | } | |
230 | ||
231 | function reorderVideosPlaylist (options: { | |
232 | url: string, | |
233 | token: string, | |
234 | playlistId: number | string, | |
235 | elementAttrs: { | |
236 | startPosition: number, | |
237 | insertAfterPosition: number, | |
238 | reorderLength?: number | |
239 | }, | |
240 | expectedStatus?: number | |
241 | }) { | |
242 | const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/reorder' | |
243 | ||
244 | return makePostBodyRequest({ | |
245 | url: options.url, | |
246 | path, | |
247 | token: options.token, | |
248 | fields: options.elementAttrs, | |
249 | statusCodeExpected: options.expectedStatus || 204 | |
250 | }) | |
251 | } | |
252 | ||
253 | async function checkPlaylistFilesWereRemoved ( | |
254 | playlistUUID: string, | |
255 | internalServerNumber: number, | |
256 | directories = [ 'thumbnails' ] | |
257 | ) { | |
258 | const testDirectory = 'test' + internalServerNumber | |
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 | ||
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 | ||
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 | ||
292 | // --------------------------------------------------------------------------- | |
293 | ||
294 | export { | |
295 | getVideoPlaylistPrivacies, | |
296 | ||
297 | getVideoPlaylistsList, | |
298 | getVideoChannelPlaylistsList, | |
299 | getAccountPlaylistsList, | |
300 | getAccountPlaylistsListWithToken, | |
301 | ||
302 | getVideoPlaylist, | |
303 | getVideoPlaylistWithToken, | |
304 | ||
305 | createVideoPlaylist, | |
306 | updateVideoPlaylist, | |
307 | deleteVideoPlaylist, | |
308 | ||
309 | addVideoInPlaylist, | |
310 | updateVideoPlaylistElement, | |
311 | removeVideoFromPlaylist, | |
312 | ||
313 | reorderVideosPlaylist, | |
314 | ||
315 | checkPlaylistFilesWereRemoved, | |
316 | ||
317 | doVideosExistInMyPlaylist | |
318 | } |