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