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