]>
Commit | Line | Data |
---|---|---|
bbd5aa7e | 1 | import { omit, pick } from '@shared/core-utils' |
7926c5f9 | 2 | import { |
4c7e60bc | 3 | HttpStatusCode, |
7926c5f9 C |
4 | MyUser, |
5 | ResultList, | |
c55e3d72 | 6 | ScopedToken, |
7926c5f9 C |
7 | User, |
8 | UserAdminFlag, | |
9 | UserCreateResult, | |
10 | UserRole, | |
11 | UserUpdate, | |
12 | UserUpdateMe, | |
13 | UserVideoQuota, | |
14 | UserVideoRate | |
15 | } from '@shared/models' | |
7926c5f9 C |
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 | ||
906f46d0 | 193 | async generate (username: string, role?: UserRole) { |
7926c5f9 | 194 | const password = 'password' |
906f46d0 | 195 | const user = await this.create({ username, password, role }) |
7926c5f9 | 196 | |
89d241a7 | 197 | const token = await this.server.login.getAccessToken({ username, password }) |
7926c5f9 C |
198 | |
199 | const me = await this.getMyInfo({ token }) | |
200 | ||
201 | return { | |
202 | token, | |
203 | userId: user.id, | |
a37e9e74 | 204 | userChannelId: me.videoChannels[0].id, |
56f47830 C |
205 | userChannelName: me.videoChannels[0].name, |
206 | password | |
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 | |
84e7ff27 | 221 | email?: string |
7926c5f9 C |
222 | channel?: { |
223 | name: string | |
224 | displayName: string | |
225 | } | |
226 | }) { | |
84e7ff27 | 227 | const { username, password = 'password', displayName, channel, email = username + '@example.com' } = options |
7926c5f9 C |
228 | const path = '/api/v1/users/register' |
229 | ||
230 | return this.postBodyRequest({ | |
231 | ...options, | |
232 | ||
233 | path, | |
234 | fields: { | |
235 | username, | |
236 | password, | |
84e7ff27 | 237 | email, |
7926c5f9 C |
238 | displayName, |
239 | channel | |
240 | }, | |
241 | implicitToken: false, | |
242 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
243 | }) | |
244 | } | |
245 | ||
246 | // --------------------------------------------------------------------------- | |
247 | ||
248 | getMyInfo (options: OverrideCommandOptions = {}) { | |
249 | const path = '/api/v1/users/me' | |
250 | ||
251 | return this.getRequestBody<MyUser>({ | |
252 | ...options, | |
253 | ||
254 | path, | |
255 | implicitToken: true, | |
256 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
257 | }) | |
258 | } | |
259 | ||
260 | getMyQuotaUsed (options: OverrideCommandOptions = {}) { | |
261 | const path = '/api/v1/users/me/video-quota-used' | |
262 | ||
263 | return this.getRequestBody<UserVideoQuota>({ | |
264 | ...options, | |
265 | ||
266 | path, | |
267 | implicitToken: true, | |
268 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
269 | }) | |
270 | } | |
271 | ||
272 | getMyRating (options: OverrideCommandOptions & { | |
273 | videoId: number | string | |
274 | }) { | |
275 | const { videoId } = options | |
276 | const path = '/api/v1/users/me/videos/' + videoId + '/rating' | |
277 | ||
278 | return this.getRequestBody<UserVideoRate>({ | |
279 | ...options, | |
280 | ||
281 | path, | |
282 | implicitToken: true, | |
283 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
284 | }) | |
285 | } | |
286 | ||
287 | deleteMe (options: OverrideCommandOptions = {}) { | |
288 | const path = '/api/v1/users/me' | |
289 | ||
290 | return this.deleteRequest({ | |
291 | ...options, | |
292 | ||
293 | path, | |
294 | implicitToken: true, | |
295 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
296 | }) | |
297 | } | |
298 | ||
299 | updateMe (options: OverrideCommandOptions & UserUpdateMe) { | |
300 | const path = '/api/v1/users/me' | |
301 | ||
bbd5aa7e | 302 | const toSend: UserUpdateMe = omit(options, [ 'expectedStatus', 'token' ]) |
7926c5f9 C |
303 | |
304 | return this.putBodyRequest({ | |
305 | ...options, | |
306 | ||
307 | path, | |
308 | fields: toSend, | |
309 | implicitToken: true, | |
310 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
311 | }) | |
312 | } | |
313 | ||
314 | updateMyAvatar (options: OverrideCommandOptions & { | |
315 | fixture: string | |
316 | }) { | |
317 | const { fixture } = options | |
318 | const path = '/api/v1/users/me/avatar/pick' | |
319 | ||
320 | return this.updateImageRequest({ | |
321 | ...options, | |
322 | ||
323 | path, | |
324 | fixture, | |
325 | fieldname: 'avatarfile', | |
326 | ||
327 | implicitToken: true, | |
328 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
329 | }) | |
330 | } | |
331 | ||
332 | // --------------------------------------------------------------------------- | |
333 | ||
334 | get (options: OverrideCommandOptions & { | |
335 | userId: number | |
336 | withStats?: boolean // default false | |
337 | }) { | |
338 | const { userId, withStats } = options | |
339 | const path = '/api/v1/users/' + userId | |
340 | ||
341 | return this.getRequestBody<User>({ | |
342 | ...options, | |
343 | ||
344 | path, | |
345 | query: { withStats }, | |
346 | implicitToken: true, | |
347 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
348 | }) | |
349 | } | |
350 | ||
351 | list (options: OverrideCommandOptions & { | |
352 | start?: number | |
353 | count?: number | |
354 | sort?: string | |
355 | search?: string | |
356 | blocked?: boolean | |
357 | } = {}) { | |
358 | const path = '/api/v1/users' | |
359 | ||
360 | return this.getRequestBody<ResultList<User>>({ | |
361 | ...options, | |
362 | ||
363 | path, | |
364 | query: pick(options, [ 'start', 'count', 'sort', 'search', 'blocked' ]), | |
365 | implicitToken: true, | |
366 | defaultExpectedStatus: HttpStatusCode.OK_200 | |
367 | }) | |
368 | } | |
369 | ||
370 | remove (options: OverrideCommandOptions & { | |
371 | userId: number | |
372 | }) { | |
373 | const { userId } = options | |
374 | const path = '/api/v1/users/' + userId | |
375 | ||
376 | return this.deleteRequest({ | |
377 | ...options, | |
378 | ||
379 | path, | |
380 | implicitToken: true, | |
381 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
382 | }) | |
383 | } | |
384 | ||
385 | update (options: OverrideCommandOptions & { | |
386 | userId: number | |
387 | email?: string | |
388 | emailVerified?: boolean | |
389 | videoQuota?: number | |
390 | videoQuotaDaily?: number | |
391 | password?: string | |
392 | adminFlags?: UserAdminFlag | |
393 | pluginAuth?: string | |
394 | role?: UserRole | |
395 | }) { | |
396 | const path = '/api/v1/users/' + options.userId | |
397 | ||
398 | const toSend: UserUpdate = {} | |
399 | if (options.password !== undefined && options.password !== null) toSend.password = options.password | |
400 | if (options.email !== undefined && options.email !== null) toSend.email = options.email | |
401 | if (options.emailVerified !== undefined && options.emailVerified !== null) toSend.emailVerified = options.emailVerified | |
402 | if (options.videoQuota !== undefined && options.videoQuota !== null) toSend.videoQuota = options.videoQuota | |
403 | if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend.videoQuotaDaily = options.videoQuotaDaily | |
404 | if (options.role !== undefined && options.role !== null) toSend.role = options.role | |
405 | if (options.adminFlags !== undefined && options.adminFlags !== null) toSend.adminFlags = options.adminFlags | |
406 | if (options.pluginAuth !== undefined) toSend.pluginAuth = options.pluginAuth | |
407 | ||
408 | return this.putBodyRequest({ | |
409 | ...options, | |
410 | ||
411 | path, | |
412 | fields: toSend, | |
413 | implicitToken: true, | |
414 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | |
415 | }) | |
416 | } | |
417 | } |