]>
Commit | Line | Data |
---|---|---|
1 | import { omit, pick } from '@shared/core-utils' | |
2 | import { | |
3 | BooleanBothQuery, | |
4 | HttpStatusCode, | |
5 | ResultList, | |
6 | VideoExistInPlaylist, | |
7 | VideoPlaylist, | |
8 | VideoPlaylistCreate, | |
9 | VideoPlaylistCreateResult, | |
10 | VideoPlaylistElement, | |
11 | VideoPlaylistElementCreate, | |
12 | VideoPlaylistElementCreateResult, | |
13 | VideoPlaylistElementUpdate, | |
14 | VideoPlaylistReorder, | |
15 | VideoPlaylistType, | |
16 | VideoPlaylistUpdate | |
17 | } from '@shared/models' | |
18 | import { unwrapBody } from '../requests' | |
19 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | |
20 | ||
21 | export class PlaylistsCommand extends AbstractCommand { | |
22 | ||
23 | list (options: OverrideCommandOptions & { | |
24 | start?: number | |
25 | count?: number | |
26 | sort?: string | |
27 | playlistType?: VideoPlaylistType | |
28 | }) { | |
29 | const path = '/api/v1/video-playlists' | |
30 | const query = pick(options, [ 'start', 'count', 'sort', 'playlistType' ]) | |
31 | ||
32 | return this.getRequestBody<ResultList<VideoPlaylist>>({ | |
33 | ...options, | |
34 | ||
35 | path, | |
36 | query, | |
37 | implicitToken: false, | |
38 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
39 | }) | |
40 | } | |
41 | ||
42 | listByChannel (options: OverrideCommandOptions & { | |
43 | handle: string | |
44 | start?: number | |
45 | count?: number | |
46 | sort?: string | |
47 | playlistType?: VideoPlaylistType | |
48 | }) { | |
49 | const path = '/api/v1/video-channels/' + options.handle + '/video-playlists' | |
50 | const query = pick(options, [ 'start', 'count', 'sort', 'playlistType' ]) | |
51 | ||
52 | return this.getRequestBody<ResultList<VideoPlaylist>>({ | |
53 | ...options, | |
54 | ||
55 | path, | |
56 | query, | |
57 | implicitToken: false, | |
58 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
59 | }) | |
60 | } | |
61 | ||
62 | listByAccount (options: OverrideCommandOptions & { | |
63 | handle: string | |
64 | start?: number | |
65 | count?: number | |
66 | sort?: string | |
67 | search?: string | |
68 | playlistType?: VideoPlaylistType | |
69 | }) { | |
70 | const path = '/api/v1/accounts/' + options.handle + '/video-playlists' | |
71 | const query = pick(options, [ 'start', 'count', 'sort', 'search', 'playlistType' ]) | |
72 | ||
73 | return this.getRequestBody<ResultList<VideoPlaylist>>({ | |
74 | ...options, | |
75 | ||
76 | path, | |
77 | query, | |
78 | implicitToken: false, | |
79 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
80 | }) | |
81 | } | |
82 | ||
83 | get (options: OverrideCommandOptions & { | |
84 | playlistId: number | string | |
85 | }) { | |
86 | const { playlistId } = options | |
87 | const path = '/api/v1/video-playlists/' + playlistId | |
88 | ||
89 | return this.getRequestBody<VideoPlaylist>({ | |
90 | ...options, | |
91 | ||
92 | path, | |
93 | implicitToken: false, | |
94 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
95 | }) | |
96 | } | |
97 | ||
98 | listVideos (options: OverrideCommandOptions & { | |
99 | playlistId: number | string | |
100 | start?: number | |
101 | count?: number | |
102 | query?: { nsfw?: BooleanBothQuery } | |
103 | }) { | |
104 | const path = '/api/v1/video-playlists/' + options.playlistId + '/videos' | |
105 | const query = options.query ?? {} | |
106 | ||
107 | return this.getRequestBody<ResultList<VideoPlaylistElement>>({ | |
108 | ...options, | |
109 | ||
110 | path, | |
111 | query: { | |
112 | ...query, | |
113 | start: options.start, | |
114 | count: options.count | |
115 | }, | |
116 | implicitToken: true, | |
117 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
118 | }) | |
119 | } | |
120 | ||
121 | delete (options: OverrideCommandOptions & { | |
122 | playlistId: number | string | |
123 | }) { | |
124 | const path = '/api/v1/video-playlists/' + options.playlistId | |
125 | ||
126 | return this.deleteRequest({ | |
127 | ...options, | |
128 | ||
129 | path, | |
130 | implicitToken: true, | |
131 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
132 | }) | |
133 | } | |
134 | ||
135 | async create (options: OverrideCommandOptions & { | |
136 | attributes: VideoPlaylistCreate | |
137 | }) { | |
138 | const path = '/api/v1/video-playlists' | |
139 | ||
140 | const fields = omit(options.attributes, [ 'thumbnailfile' ]) | |
141 | ||
142 | const attaches = options.attributes.thumbnailfile | |
143 | ? { thumbnailfile: options.attributes.thumbnailfile } | |
144 | : {} | |
145 | ||
146 | const body = await unwrapBody<{ videoPlaylist: VideoPlaylistCreateResult }>(this.postUploadRequest({ | |
147 | ...options, | |
148 | ||
149 | path, | |
150 | fields, | |
151 | attaches, | |
152 | implicitToken: true, | |
153 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
154 | })) | |
155 | ||
156 | return body.videoPlaylist | |
157 | } | |
158 | ||
159 | update (options: OverrideCommandOptions & { | |
160 | attributes: VideoPlaylistUpdate | |
161 | playlistId: number | string | |
162 | }) { | |
163 | const path = '/api/v1/video-playlists/' + options.playlistId | |
164 | ||
165 | const fields = omit(options.attributes, [ 'thumbnailfile' ]) | |
166 | ||
167 | const attaches = options.attributes.thumbnailfile | |
168 | ? { thumbnailfile: options.attributes.thumbnailfile } | |
169 | : {} | |
170 | ||
171 | return this.putUploadRequest({ | |
172 | ...options, | |
173 | ||
174 | path, | |
175 | fields, | |
176 | attaches, | |
177 | implicitToken: true, | |
178 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
179 | }) | |
180 | } | |
181 | ||
182 | async addElement (options: OverrideCommandOptions & { | |
183 | playlistId: number | string | |
184 | attributes: VideoPlaylistElementCreate | { videoId: string } | |
185 | }) { | |
186 | const attributes = { | |
187 | ...options.attributes, | |
188 | ||
189 | videoId: await this.server.videos.getId({ ...options, uuid: options.attributes.videoId }) | |
190 | } | |
191 | ||
192 | const path = '/api/v1/video-playlists/' + options.playlistId + '/videos' | |
193 | ||
194 | const body = await unwrapBody<{ videoPlaylistElement: VideoPlaylistElementCreateResult }>(this.postBodyRequest({ | |
195 | ...options, | |
196 | ||
197 | path, | |
198 | fields: attributes, | |
199 | implicitToken: true, | |
200 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
201 | })) | |
202 | ||
203 | return body.videoPlaylistElement | |
204 | } | |
205 | ||
206 | updateElement (options: OverrideCommandOptions & { | |
207 | playlistId: number | string | |
208 | elementId: number | string | |
209 | attributes: VideoPlaylistElementUpdate | |
210 | }) { | |
211 | const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.elementId | |
212 | ||
213 | return this.putBodyRequest({ | |
214 | ...options, | |
215 | ||
216 | path, | |
217 | fields: options.attributes, | |
218 | implicitToken: true, | |
219 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
220 | }) | |
221 | } | |
222 | ||
223 | removeElement (options: OverrideCommandOptions & { | |
224 | playlistId: number | string | |
225 | elementId: number | |
226 | }) { | |
227 | const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.elementId | |
228 | ||
229 | return this.deleteRequest({ | |
230 | ...options, | |
231 | ||
232 | path, | |
233 | implicitToken: true, | |
234 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
235 | }) | |
236 | } | |
237 | ||
238 | reorderElements (options: OverrideCommandOptions & { | |
239 | playlistId: number | string | |
240 | attributes: VideoPlaylistReorder | |
241 | }) { | |
242 | const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/reorder' | |
243 | ||
244 | return this.postBodyRequest({ | |
245 | ...options, | |
246 | ||
247 | path, | |
248 | fields: options.attributes, | |
249 | implicitToken: true, | |
250 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
251 | }) | |
252 | } | |
253 | ||
254 | getPrivacies (options: OverrideCommandOptions = {}) { | |
255 | const path = '/api/v1/video-playlists/privacies' | |
256 | ||
257 | return this.getRequestBody<{ [ id: number ]: string }>({ | |
258 | ...options, | |
259 | ||
260 | path, | |
261 | implicitToken: false, | |
262 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
263 | }) | |
264 | } | |
265 | ||
266 | videosExist (options: OverrideCommandOptions & { | |
267 | videoIds: number[] | |
268 | }) { | |
269 | const { videoIds } = options | |
270 | const path = '/api/v1/users/me/video-playlists/videos-exist' | |
271 | ||
272 | return this.getRequestBody<VideoExistInPlaylist>({ | |
273 | ...options, | |
274 | ||
275 | path, | |
276 | query: { videoIds }, | |
277 | implicitToken: true, | |
278 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
279 | }) | |
280 | } | |
281 | } |