aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/users/users-command.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-07-21 15:51:30 +0200
committerChocobozzz <me@florianbigard.com>2021-07-21 15:51:30 +0200
commita24bd1ed41b43790bab6ba789580bb4e85f07d85 (patch)
treea54b0f6c921ba83a6e909cd0ced325b2d4b8863c /shared/extra-utils/users/users-command.ts
parent5f26f13b3c16ac5ae0a3b0a7142d84a9528cf565 (diff)
parentc63830f15403ac4e750829f27d8bbbdc9a59282c (diff)
downloadPeerTube-a24bd1ed41b43790bab6ba789580bb4e85f07d85.tar.gz
PeerTube-a24bd1ed41b43790bab6ba789580bb4e85f07d85.tar.zst
PeerTube-a24bd1ed41b43790bab6ba789580bb4e85f07d85.zip
Merge branch 'next' into develop
Diffstat (limited to 'shared/extra-utils/users/users-command.ts')
-rw-r--r--shared/extra-utils/users/users-command.ts414
1 files changed, 414 insertions, 0 deletions
diff --git a/shared/extra-utils/users/users-command.ts b/shared/extra-utils/users/users-command.ts
new file mode 100644
index 000000000..d66ad15f2
--- /dev/null
+++ b/shared/extra-utils/users/users-command.ts
@@ -0,0 +1,414 @@
1import { omit, pick } from 'lodash'
2import {
3 HttpStatusCode,
4 MyUser,
5 ResultList,
6 User,
7 UserAdminFlag,
8 UserCreateResult,
9 UserRole,
10 UserUpdate,
11 UserUpdateMe,
12 UserVideoQuota,
13 UserVideoRate
14} from '@shared/models'
15import { ScopedToken } from '@shared/models/users/user-scoped-token'
16import { unwrapBody } from '../requests'
17import { AbstractCommand, OverrideCommandOptions } from '../shared'
18
19export 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) {
194 const password = 'password'
195 const user = await this.create({ username, password })
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 }
206 }
207
208 async generateUserAndToken (username: string) {
209 const password = 'password'
210 await this.create({ username, password })
211
212 return this.server.login.getAccessToken({ username, password })
213 }
214
215 register (options: OverrideCommandOptions & {
216 username: string
217 password?: string
218 displayName?: string
219 channel?: {
220 name: string
221 displayName: string
222 }
223 }) {
224 const { username, password = 'password', displayName, channel } = options
225 const path = '/api/v1/users/register'
226
227 return this.postBodyRequest({
228 ...options,
229
230 path,
231 fields: {
232 username,
233 password,
234 email: username + '@example.com',
235 displayName,
236 channel
237 },
238 implicitToken: false,
239 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
240 })
241 }
242
243 // ---------------------------------------------------------------------------
244
245 getMyInfo (options: OverrideCommandOptions = {}) {
246 const path = '/api/v1/users/me'
247
248 return this.getRequestBody<MyUser>({
249 ...options,
250
251 path,
252 implicitToken: true,
253 defaultExpectedStatus: HttpStatusCode.OK_200
254 })
255 }
256
257 getMyQuotaUsed (options: OverrideCommandOptions = {}) {
258 const path = '/api/v1/users/me/video-quota-used'
259
260 return this.getRequestBody<UserVideoQuota>({
261 ...options,
262
263 path,
264 implicitToken: true,
265 defaultExpectedStatus: HttpStatusCode.OK_200
266 })
267 }
268
269 getMyRating (options: OverrideCommandOptions & {
270 videoId: number | string
271 }) {
272 const { videoId } = options
273 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
274
275 return this.getRequestBody<UserVideoRate>({
276 ...options,
277
278 path,
279 implicitToken: true,
280 defaultExpectedStatus: HttpStatusCode.OK_200
281 })
282 }
283
284 deleteMe (options: OverrideCommandOptions = {}) {
285 const path = '/api/v1/users/me'
286
287 return this.deleteRequest({
288 ...options,
289
290 path,
291 implicitToken: true,
292 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
293 })
294 }
295
296 updateMe (options: OverrideCommandOptions & UserUpdateMe) {
297 const path = '/api/v1/users/me'
298
299 const toSend: UserUpdateMe = omit(options, 'url', 'accessToken')
300
301 return this.putBodyRequest({
302 ...options,
303
304 path,
305 fields: toSend,
306 implicitToken: true,
307 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
308 })
309 }
310
311 updateMyAvatar (options: OverrideCommandOptions & {
312 fixture: string
313 }) {
314 const { fixture } = options
315 const path = '/api/v1/users/me/avatar/pick'
316
317 return this.updateImageRequest({
318 ...options,
319
320 path,
321 fixture,
322 fieldname: 'avatarfile',
323
324 implicitToken: true,
325 defaultExpectedStatus: HttpStatusCode.OK_200
326 })
327 }
328
329 // ---------------------------------------------------------------------------
330
331 get (options: OverrideCommandOptions & {
332 userId: number
333 withStats?: boolean // default false
334 }) {
335 const { userId, withStats } = options
336 const path = '/api/v1/users/' + userId
337
338 return this.getRequestBody<User>({
339 ...options,
340
341 path,
342 query: { withStats },
343 implicitToken: true,
344 defaultExpectedStatus: HttpStatusCode.OK_200
345 })
346 }
347
348 list (options: OverrideCommandOptions & {
349 start?: number
350 count?: number
351 sort?: string
352 search?: string
353 blocked?: boolean
354 } = {}) {
355 const path = '/api/v1/users'
356
357 return this.getRequestBody<ResultList<User>>({
358 ...options,
359
360 path,
361 query: pick(options, [ 'start', 'count', 'sort', 'search', 'blocked' ]),
362 implicitToken: true,
363 defaultExpectedStatus: HttpStatusCode.OK_200
364 })
365 }
366
367 remove (options: OverrideCommandOptions & {
368 userId: number
369 }) {
370 const { userId } = options
371 const path = '/api/v1/users/' + userId
372
373 return this.deleteRequest({
374 ...options,
375
376 path,
377 implicitToken: true,
378 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
379 })
380 }
381
382 update (options: OverrideCommandOptions & {
383 userId: number
384 email?: string
385 emailVerified?: boolean
386 videoQuota?: number
387 videoQuotaDaily?: number
388 password?: string
389 adminFlags?: UserAdminFlag
390 pluginAuth?: string
391 role?: UserRole
392 }) {
393 const path = '/api/v1/users/' + options.userId
394
395 const toSend: UserUpdate = {}
396 if (options.password !== undefined && options.password !== null) toSend.password = options.password
397 if (options.email !== undefined && options.email !== null) toSend.email = options.email
398 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend.emailVerified = options.emailVerified
399 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend.videoQuota = options.videoQuota
400 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend.videoQuotaDaily = options.videoQuotaDaily
401 if (options.role !== undefined && options.role !== null) toSend.role = options.role
402 if (options.adminFlags !== undefined && options.adminFlags !== null) toSend.adminFlags = options.adminFlags
403 if (options.pluginAuth !== undefined) toSend.pluginAuth = options.pluginAuth
404
405 return this.putBodyRequest({
406 ...options,
407
408 path,
409 fields: toSend,
410 implicitToken: true,
411 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
412 })
413 }
414}