]>
Commit | Line | Data |
---|---|---|
b033851f C |
1 | import { omit } from 'lodash' |
2 | import { pick } from '@shared/core-utils' | |
7926c5f9 | 3 | import { |
4c7e60bc | 4 | HttpStatusCode, |
7926c5f9 C |
5 | MyUser, |
6 | ResultList, | |
c55e3d72 | 7 | ScopedToken, |
7926c5f9 C |
8 | User, |
9 | UserAdminFlag, | |
10 | UserCreateResult, | |
11 | UserRole, | |
12 | UserUpdate, | |
13 | UserUpdateMe, | |
14 | UserVideoQuota, | |
15 | UserVideoRate | |
16 | } from '@shared/models' | |
7926c5f9 C |
17 | import { unwrapBody } from '../requests' |
18 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | |
19 | ||
20 | export class UsersCommand extends AbstractCommand { | |
21 | ||
22 | askResetPassword (options: OverrideCommandOptions & { | |
23 | email: string | |
24 | }) { | |
25 | const { email } = options | |
26 | const path = '/api/v1/users/ask-reset-password' | |
27 | ||
28 | return this.postBodyRequest({ | |
29 | ...options, | |
30 | ||
31 | path, | |
32 | fields: { email }, | |
33 | implicitToken: false, | |
34 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
35 | }) | |
36 | } | |
37 | ||
38 | resetPassword (options: OverrideCommandOptions & { | |
39 | userId: number | |
40 | verificationString: string | |
41 | password: string | |
42 | }) { | |
43 | const { userId, verificationString, password } = options | |
44 | const path = '/api/v1/users/' + userId + '/reset-password' | |
45 | ||
46 | return this.postBodyRequest({ | |
47 | ...options, | |
48 | ||
49 | path, | |
50 | fields: { password, verificationString }, | |
51 | implicitToken: false, | |
52 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
53 | }) | |
54 | } | |
55 | ||
56 | // --------------------------------------------------------------------------- | |
57 | ||
58 | askSendVerifyEmail (options: OverrideCommandOptions & { | |
59 | email: string | |
60 | }) { | |
61 | const { email } = options | |
62 | const path = '/api/v1/users/ask-send-verify-email' | |
63 | ||
64 | return this.postBodyRequest({ | |
65 | ...options, | |
66 | ||
67 | path, | |
68 | fields: { email }, | |
69 | implicitToken: false, | |
70 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
71 | }) | |
72 | } | |
73 | ||
74 | verifyEmail (options: OverrideCommandOptions & { | |
75 | userId: number | |
76 | verificationString: string | |
77 | isPendingEmail?: boolean // default false | |
78 | }) { | |
79 | const { userId, verificationString, isPendingEmail = false } = options | |
80 | const path = '/api/v1/users/' + userId + '/verify-email' | |
81 | ||
82 | return this.postBodyRequest({ | |
83 | ...options, | |
84 | ||
85 | path, | |
86 | fields: { | |
87 | verificationString, | |
88 | isPendingEmail | |
89 | }, | |
90 | implicitToken: false, | |
91 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
92 | }) | |
93 | } | |
94 | ||
95 | // --------------------------------------------------------------------------- | |
96 | ||
97 | banUser (options: OverrideCommandOptions & { | |
98 | userId: number | |
99 | reason?: string | |
100 | }) { | |
101 | const { userId, reason } = options | |
102 | const path = '/api/v1/users' + '/' + userId + '/block' | |
103 | ||
104 | return this.postBodyRequest({ | |
105 | ...options, | |
106 | ||
107 | path, | |
108 | fields: { reason }, | |
109 | implicitToken: true, | |
110 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
111 | }) | |
112 | } | |
113 | ||
114 | unbanUser (options: OverrideCommandOptions & { | |
115 | userId: number | |
116 | }) { | |
117 | const { userId } = options | |
118 | const path = '/api/v1/users' + '/' + userId + '/unblock' | |
119 | ||
120 | return this.postBodyRequest({ | |
121 | ...options, | |
122 | ||
123 | path, | |
124 | implicitToken: true, | |
125 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
126 | }) | |
127 | } | |
128 | ||
129 | // --------------------------------------------------------------------------- | |
130 | ||
131 | getMyScopedTokens (options: OverrideCommandOptions = {}) { | |
132 | const path = '/api/v1/users/scoped-tokens' | |
133 | ||
134 | return this.getRequestBody<ScopedToken>({ | |
135 | ...options, | |
136 | ||
137 | path, | |
138 | implicitToken: true, | |
139 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
140 | }) | |
141 | } | |
142 | ||
143 | renewMyScopedTokens (options: OverrideCommandOptions = {}) { | |
144 | const path = '/api/v1/users/scoped-tokens' | |
145 | ||
146 | return this.postBodyRequest({ | |
147 | ...options, | |
148 | ||
149 | path, | |
150 | implicitToken: true, | |
151 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
152 | }) | |
153 | } | |
154 | ||
155 | // --------------------------------------------------------------------------- | |
156 | ||
157 | create (options: OverrideCommandOptions & { | |
158 | username: string | |
159 | password?: string | |
160 | videoQuota?: number | |
161 | videoQuotaDaily?: number | |
162 | role?: UserRole | |
163 | adminFlags?: UserAdminFlag | |
164 | }) { | |
165 | const { | |
166 | username, | |
167 | adminFlags, | |
168 | password = 'password', | |
169 | videoQuota = 42000000, | |
170 | videoQuotaDaily = -1, | |
171 | role = UserRole.USER | |
172 | } = options | |
173 | ||
174 | const path = '/api/v1/users' | |
175 | ||
176 | return unwrapBody<{ user: UserCreateResult }>(this.postBodyRequest({ | |
177 | ...options, | |
178 | ||
179 | path, | |
180 | fields: { | |
181 | username, | |
182 | password, | |
183 | role, | |
184 | adminFlags, | |
185 | email: username + '@example.com', | |
186 | videoQuota, | |
187 | videoQuotaDaily | |
188 | }, | |
189 | implicitToken: true, | |
190 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
191 | })).then(res => res.user) | |
192 | } | |
193 | ||
906f46d0 | 194 | async generate (username: string, role?: UserRole) { |
7926c5f9 | 195 | const password = 'password' |
906f46d0 | 196 | const user = await this.create({ username, password, role }) |
7926c5f9 | 197 | |
89d241a7 | 198 | const token = await this.server.login.getAccessToken({ username, password }) |
7926c5f9 C |
199 | |
200 | const me = await this.getMyInfo({ token }) | |
201 | ||
202 | return { | |
203 | token, | |
204 | userId: user.id, | |
a37e9e74 | 205 | userChannelId: me.videoChannels[0].id, |
206 | userChannelName: me.videoChannels[0].name | |
7926c5f9 C |
207 | } |
208 | } | |
209 | ||
906f46d0 | 210 | async generateUserAndToken (username: string, role?: UserRole) { |
7926c5f9 | 211 | const password = 'password' |
906f46d0 | 212 | await this.create({ username, password, role }) |
7926c5f9 | 213 | |
89d241a7 | 214 | return this.server.login.getAccessToken({ username, password }) |
7926c5f9 C |
215 | } |
216 | ||
217 | register (options: OverrideCommandOptions & { | |
218 | username: string | |
219 | password?: string | |
220 | displayName?: string | |
221 | channel?: { | |
222 | name: string | |
223 | displayName: string | |
224 | } | |
225 | }) { | |
226 | const { username, password = 'password', displayName, channel } = 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: username + '@example.com', | |
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, 'url', 'accessToken') | |
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 | } |