]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/videos/playlists-command.ts
Shorter server command names
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / videos / playlists-command.ts
CommitLineData
e6346d59 1import { omit, pick } from 'lodash'
d23dd9fb 2import { HttpStatusCode } from '@shared/core-utils'
e6346d59
C
3import {
4 BooleanBothQuery,
5 ResultList,
6 VideoExistInPlaylist,
7 VideoPlaylist,
d23dd9fb 8 VideoPlaylistCreate,
e6346d59
C
9 VideoPlaylistCreateResult,
10 VideoPlaylistElement,
d23dd9fb 11 VideoPlaylistElementCreate,
e6346d59 12 VideoPlaylistElementCreateResult,
d23dd9fb
C
13 VideoPlaylistElementUpdate,
14 VideoPlaylistReorder,
15 VideoPlaylistType,
16 VideoPlaylistUpdate
e6346d59 17} from '@shared/models'
e6346d59
C
18import { unwrapBody } from '../requests'
19import { AbstractCommand, OverrideCommandOptions } from '../shared'
e6346d59
C
20
21export 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
89d241a7 187 videoId: await this.server.videos.getId({ ...options, uuid: options.attributes.videoId })
e6346d59
C
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}