]>
Commit | Line | Data |
---|---|---|
1 | import { omit, pick } from '@shared/core-utils' | |
2 | import { | |
3 | HttpStatusCode, | |
4 | MyUser, | |
5 | ResultList, | |
6 | ScopedToken, | |
7 | User, | |
8 | UserAdminFlag, | |
9 | UserCreateResult, | |
10 | UserRole, | |
11 | UserUpdate, | |
12 | UserUpdateMe, | |
13 | UserVideoQuota, | |
14 | UserVideoRate | |
15 | } from '@shared/models' | |
16 | import { unwrapBody } from '../requests' | |
17 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | |
18 | ||
19 | export class UsersCommand extends AbstractCommand { | |
20 | ||
21 | askResetPassword (options: OverrideCommandOptions & { | |
22 | email: string | |
23 | }) { | |
24 | const { email } = options | |
25 | const path = '/api/v1/users/ask-reset-password' | |
26 | ||
27 | return this.postBodyRequest({ | |
28 | ...options, | |
29 | ||
30 | path, | |
31 | fields: { email }, | |
32 | implicitToken: false, | |
33 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
34 | }) | |
35 | } | |
36 | ||
37 | resetPassword (options: OverrideCommandOptions & { | |
38 | userId: number | |
39 | verificationString: string | |
40 | password: string | |
41 | }) { | |
42 | const { userId, verificationString, password } = options | |
43 | const path = '/api/v1/users/' + userId + '/reset-password' | |
44 | ||
45 | return this.postBodyRequest({ | |
46 | ...options, | |
47 | ||
48 | path, | |
49 | fields: { password, verificationString }, | |
50 | implicitToken: false, | |
51 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
52 | }) | |
53 | } | |
54 | ||
55 | // --------------------------------------------------------------------------- | |
56 | ||
57 | askSendVerifyEmail (options: OverrideCommandOptions & { | |
58 | email: string | |
59 | }) { | |
60 | const { email } = options | |
61 | const path = '/api/v1/users/ask-send-verify-email' | |
62 | ||
63 | return this.postBodyRequest({ | |
64 | ...options, | |
65 | ||
66 | path, | |
67 | fields: { email }, | |
68 | implicitToken: false, | |
69 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
70 | }) | |
71 | } | |
72 | ||
73 | verifyEmail (options: OverrideCommandOptions & { | |
74 | userId: number | |
75 | verificationString: string | |
76 | isPendingEmail?: boolean // default false | |
77 | }) { | |
78 | const { userId, verificationString, isPendingEmail = false } = options | |
79 | const path = '/api/v1/users/' + userId + '/verify-email' | |
80 | ||
81 | return this.postBodyRequest({ | |
82 | ...options, | |
83 | ||
84 | path, | |
85 | fields: { | |
86 | verificationString, | |
87 | isPendingEmail | |
88 | }, | |
89 | implicitToken: false, | |
90 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
91 | }) | |
92 | } | |
93 | ||
94 | // --------------------------------------------------------------------------- | |
95 | ||
96 | banUser (options: OverrideCommandOptions & { | |
97 | userId: number | |
98 | reason?: string | |
99 | }) { | |
100 | const { userId, reason } = options | |
101 | const path = '/api/v1/users' + '/' + userId + '/block' | |
102 | ||
103 | return this.postBodyRequest({ | |
104 | ...options, | |
105 | ||
106 | path, | |
107 | fields: { reason }, | |
108 | implicitToken: true, | |
109 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
110 | }) | |
111 | } | |
112 | ||
113 | unbanUser (options: OverrideCommandOptions & { | |
114 | userId: number | |
115 | }) { | |
116 | const { userId } = options | |
117 | const path = '/api/v1/users' + '/' + userId + '/unblock' | |
118 | ||
119 | return this.postBodyRequest({ | |
120 | ...options, | |
121 | ||
122 | path, | |
123 | implicitToken: true, | |
124 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
125 | }) | |
126 | } | |
127 | ||
128 | // --------------------------------------------------------------------------- | |
129 | ||
130 | getMyScopedTokens (options: OverrideCommandOptions = {}) { | |
131 | const path = '/api/v1/users/scoped-tokens' | |
132 | ||
133 | return this.getRequestBody<ScopedToken>({ | |
134 | ...options, | |
135 | ||
136 | path, | |
137 | implicitToken: true, | |
138 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
139 | }) | |
140 | } | |
141 | ||
142 | renewMyScopedTokens (options: OverrideCommandOptions = {}) { | |
143 | const path = '/api/v1/users/scoped-tokens' | |
144 | ||
145 | return this.postBodyRequest({ | |
146 | ...options, | |
147 | ||
148 | path, | |
149 | implicitToken: true, | |
150 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
151 | }) | |
152 | } | |
153 | ||
154 | // --------------------------------------------------------------------------- | |
155 | ||
156 | create (options: OverrideCommandOptions & { | |
157 | username: string | |
158 | password?: string | |
159 | videoQuota?: number | |
160 | videoQuotaDaily?: number | |
161 | role?: UserRole | |
162 | adminFlags?: UserAdminFlag | |
163 | }) { | |
164 | const { | |
165 | username, | |
166 | adminFlags, | |
167 | password = 'password', | |
168 | videoQuota = 42000000, | |
169 | videoQuotaDaily = -1, | |
170 | role = UserRole.USER | |
171 | } = options | |
172 | ||
173 | const path = '/api/v1/users' | |
174 | ||
175 | return unwrapBody<{ user: UserCreateResult }>(this.postBodyRequest({ | |
176 | ...options, | |
177 | ||
178 | path, | |
179 | fields: { | |
180 | username, | |
181 | password, | |
182 | role, | |
183 | adminFlags, | |
184 | email: username + '@example.com', | |
185 | videoQuota, | |
186 | videoQuotaDaily | |
187 | }, | |
188 | implicitToken: true, | |
189 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
190 | })).then(res => res.user) | |
191 | } | |
192 | ||
193 | async generate (username: string, role?: UserRole) { | |
194 | const password = 'password' | |
195 | const user = await this.create({ username, password, role }) | |
196 | ||
197 | const token = await this.server.login.getAccessToken({ username, password }) | |
198 | ||
199 | const me = await this.getMyInfo({ token }) | |
200 | ||
201 | return { | |
202 | token, | |
203 | userId: user.id, | |
204 | userChannelId: me.videoChannels[0].id, | |
205 | userChannelName: me.videoChannels[0].name | |
206 | } | |
207 | } | |
208 | ||
209 | async generateUserAndToken (username: string, role?: UserRole) { | |
210 | const password = 'password' | |
211 | await this.create({ username, password, role }) | |
212 | ||
213 | return this.server.login.getAccessToken({ username, password }) | |
214 | } | |
215 | ||
216 | register (options: OverrideCommandOptions & { | |
217 | username: string | |
218 | password?: string | |
219 | displayName?: string | |
220 | email?: string | |
221 | channel?: { | |
222 | name: string | |
223 | displayName: string | |
224 | } | |
225 | }) { | |
226 | const { username, password = 'password', displayName, channel, email = username + '@example.com' } = options | |
227 | const path = '/api/v1/users/register' | |
228 | ||
229 | return this.postBodyRequest({ | |
230 | ...options, | |
231 | ||
232 | path, | |
233 | fields: { | |
234 | username, | |
235 | password, | |
236 | email, | |
237 | displayName, | |
238 | channel | |
239 | }, | |
240 | implicitToken: false, | |
241 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
242 | }) | |
243 | } | |
244 | ||
245 | // --------------------------------------------------------------------------- | |
246 | ||
247 | getMyInfo (options: OverrideCommandOptions = {}) { | |
248 | const path = '/api/v1/users/me' | |
249 | ||
250 | return this.getRequestBody<MyUser>({ | |
251 | ...options, | |
252 | ||
253 | path, | |
254 | implicitToken: true, | |
255 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
256 | }) | |
257 | } | |
258 | ||
259 | getMyQuotaUsed (options: OverrideCommandOptions = {}) { | |
260 | const path = '/api/v1/users/me/video-quota-used' | |
261 | ||
262 | return this.getRequestBody<UserVideoQuota>({ | |
263 | ...options, | |
264 | ||
265 | path, | |
266 | implicitToken: true, | |
267 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
268 | }) | |
269 | } | |
270 | ||
271 | getMyRating (options: OverrideCommandOptions & { | |
272 | videoId: number | string | |
273 | }) { | |
274 | const { videoId } = options | |
275 | const path = '/api/v1/users/me/videos/' + videoId + '/rating' | |
276 | ||
277 | return this.getRequestBody<UserVideoRate>({ | |
278 | ...options, | |
279 | ||
280 | path, | |
281 | implicitToken: true, | |
282 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
283 | }) | |
284 | } | |
285 | ||
286 | deleteMe (options: OverrideCommandOptions = {}) { | |
287 | const path = '/api/v1/users/me' | |
288 | ||
289 | return this.deleteRequest({ | |
290 | ...options, | |
291 | ||
292 | path, | |
293 | implicitToken: true, | |
294 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
295 | }) | |
296 | } | |
297 | ||
298 | updateMe (options: OverrideCommandOptions & UserUpdateMe) { | |
299 | const path = '/api/v1/users/me' | |
300 | ||
301 | const toSend: UserUpdateMe = omit(options, [ 'expectedStatus', 'token' ]) | |
302 | ||
303 | return this.putBodyRequest({ | |
304 | ...options, | |
305 | ||
306 | path, | |
307 | fields: toSend, | |
308 | implicitToken: true, | |
309 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
310 | }) | |
311 | } | |
312 | ||
313 | updateMyAvatar (options: OverrideCommandOptions & { | |
314 | fixture: string | |
315 | }) { | |
316 | const { fixture } = options | |
317 | const path = '/api/v1/users/me/avatar/pick' | |
318 | ||
319 | return this.updateImageRequest({ | |
320 | ...options, | |
321 | ||
322 | path, | |
323 | fixture, | |
324 | fieldname: 'avatarfile', | |
325 | ||
326 | implicitToken: true, | |
327 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
328 | }) | |
329 | } | |
330 | ||
331 | // --------------------------------------------------------------------------- | |
332 | ||
333 | get (options: OverrideCommandOptions & { | |
334 | userId: number | |
335 | withStats?: boolean // default false | |
336 | }) { | |
337 | const { userId, withStats } = options | |
338 | const path = '/api/v1/users/' + userId | |
339 | ||
340 | return this.getRequestBody<User>({ | |
341 | ...options, | |
342 | ||
343 | path, | |
344 | query: { withStats }, | |
345 | implicitToken: true, | |
346 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
347 | }) | |
348 | } | |
349 | ||
350 | list (options: OverrideCommandOptions & { | |
351 | start?: number | |
352 | count?: number | |
353 | sort?: string | |
354 | search?: string | |
355 | blocked?: boolean | |
356 | } = {}) { | |
357 | const path = '/api/v1/users' | |
358 | ||
359 | return this.getRequestBody<ResultList<User>>({ | |
360 | ...options, | |
361 | ||
362 | path, | |
363 | query: pick(options, [ 'start', 'count', 'sort', 'search', 'blocked' ]), | |
364 | implicitToken: true, | |
365 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
366 | }) | |
367 | } | |
368 | ||
369 | remove (options: OverrideCommandOptions & { | |
370 | userId: number | |
371 | }) { | |
372 | const { userId } = options | |
373 | const path = '/api/v1/users/' + userId | |
374 | ||
375 | return this.deleteRequest({ | |
376 | ...options, | |
377 | ||
378 | path, | |
379 | implicitToken: true, | |
380 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
381 | }) | |
382 | } | |
383 | ||
384 | update (options: OverrideCommandOptions & { | |
385 | userId: number | |
386 | email?: string | |
387 | emailVerified?: boolean | |
388 | videoQuota?: number | |
389 | videoQuotaDaily?: number | |
390 | password?: string | |
391 | adminFlags?: UserAdminFlag | |
392 | pluginAuth?: string | |
393 | role?: UserRole | |
394 | }) { | |
395 | const path = '/api/v1/users/' + options.userId | |
396 | ||
397 | const toSend: UserUpdate = {} | |
398 | if (options.password !== undefined && options.password !== null) toSend.password = options.password | |
399 | if (options.email !== undefined && options.email !== null) toSend.email = options.email | |
400 | if (options.emailVerified !== undefined && options.emailVerified !== null) toSend.emailVerified = options.emailVerified | |
401 | if (options.videoQuota !== undefined && options.videoQuota !== null) toSend.videoQuota = options.videoQuota | |
402 | if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend.videoQuotaDaily = options.videoQuotaDaily | |
403 | if (options.role !== undefined && options.role !== null) toSend.role = options.role | |
404 | if (options.adminFlags !== undefined && options.adminFlags !== null) toSend.adminFlags = options.adminFlags | |
405 | if (options.pluginAuth !== undefined) toSend.pluginAuth = options.pluginAuth | |
406 | ||
407 | return this.putBodyRequest({ | |
408 | ...options, | |
409 | ||
410 | path, | |
411 | fields: toSend, | |
412 | implicitToken: true, | |
413 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
414 | }) | |
415 | } | |
416 | } |