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