1 import { omit, pick } from 'lodash'
2 import { HttpStatusCode } from '@shared/core-utils'
14 } from '@shared/models'
15 import { ScopedToken } from '@shared/models/users/user-scoped-token'
16 import { unwrapBody } from '../requests'
17 import { AbstractCommand, OverrideCommandOptions } from '../shared'
19 export class UsersCommand extends AbstractCommand {
21 askResetPassword (options: OverrideCommandOptions & {
24 const { email } = options
25 const path = '/api/v1/users/ask-reset-password'
27 return this.postBodyRequest({
33 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
37 resetPassword (options: OverrideCommandOptions & {
39 verificationString: string
42 const { userId, verificationString, password } = options
43 const path = '/api/v1/users/' + userId + '/reset-password'
45 return this.postBodyRequest({
49 fields: { password, verificationString },
51 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
55 // ---------------------------------------------------------------------------
57 askSendVerifyEmail (options: OverrideCommandOptions & {
60 const { email } = options
61 const path = '/api/v1/users/ask-send-verify-email'
63 return this.postBodyRequest({
69 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
73 verifyEmail (options: OverrideCommandOptions & {
75 verificationString: string
76 isPendingEmail?: boolean // default false
78 const { userId, verificationString, isPendingEmail = false } = options
79 const path = '/api/v1/users/' + userId + '/verify-email'
81 return this.postBodyRequest({
90 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
94 // ---------------------------------------------------------------------------
96 banUser (options: OverrideCommandOptions & {
100 const { userId, reason } = options
101 const path = '/api/v1/users' + '/' + userId + '/block'
103 return this.postBodyRequest({
109 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
113 unbanUser (options: OverrideCommandOptions & {
116 const { userId } = options
117 const path = '/api/v1/users' + '/' + userId + '/unblock'
119 return this.postBodyRequest({
124 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
128 // ---------------------------------------------------------------------------
130 getMyScopedTokens (options: OverrideCommandOptions = {}) {
131 const path = '/api/v1/users/scoped-tokens'
133 return this.getRequestBody<ScopedToken>({
138 defaultExpectedStatus: HttpStatusCode.OK_200
142 renewMyScopedTokens (options: OverrideCommandOptions = {}) {
143 const path = '/api/v1/users/scoped-tokens'
145 return this.postBodyRequest({
150 defaultExpectedStatus: HttpStatusCode.OK_200
154 // ---------------------------------------------------------------------------
156 create (options: OverrideCommandOptions & {
160 videoQuotaDaily?: number
162 adminFlags?: UserAdminFlag
167 password = 'password',
168 videoQuota = 42000000,
169 videoQuotaDaily = -1,
173 const path = '/api/v1/users'
175 return unwrapBody<{ user: UserCreateResult }>(this.postBodyRequest({
184 email: username + '@example.com',
189 defaultExpectedStatus: HttpStatusCode.OK_200
190 })).then(res => res.user)
193 async generate (username: string) {
194 const password = 'password'
195 const user = await this.create({ username, password })
197 const token = await this.server.loginCommand.getAccessToken({ username, password })
199 const me = await this.getMyInfo({ token })
204 userChannelId: me.videoChannels[0].id
208 async generateUserAndToken (username: string) {
209 const password = 'password'
210 await this.create({ username, password })
212 return this.server.loginCommand.getAccessToken({ username, password })
215 register (options: OverrideCommandOptions & {
224 const { username, password = 'password', displayName, channel } = options
225 const path = '/api/v1/users/register'
227 return this.postBodyRequest({
234 email: username + '@example.com',
238 implicitToken: false,
239 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
243 // ---------------------------------------------------------------------------
245 getMyInfo (options: OverrideCommandOptions = {}) {
246 const path = '/api/v1/users/me'
248 return this.getRequestBody<MyUser>({
253 defaultExpectedStatus: HttpStatusCode.OK_200
257 getMyQuotaUsed (options: OverrideCommandOptions = {}) {
258 const path = '/api/v1/users/me/video-quota-used'
260 return this.getRequestBody<UserVideoQuota>({
265 defaultExpectedStatus: HttpStatusCode.OK_200
269 getMyRating (options: OverrideCommandOptions & {
270 videoId: number | string
272 const { videoId } = options
273 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
275 return this.getRequestBody<UserVideoRate>({
280 defaultExpectedStatus: HttpStatusCode.OK_200
284 deleteMe (options: OverrideCommandOptions = {}) {
285 const path = '/api/v1/users/me'
287 return this.deleteRequest({
292 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
296 updateMe (options: OverrideCommandOptions & UserUpdateMe) {
297 const path = '/api/v1/users/me'
299 const toSend: UserUpdateMe = omit(options, 'url', 'accessToken')
301 return this.putBodyRequest({
307 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
311 updateMyAvatar (options: OverrideCommandOptions & {
314 const { fixture } = options
315 const path = '/api/v1/users/me/avatar/pick'
317 return this.updateImageRequest({
322 fieldname: 'avatarfile',
325 defaultExpectedStatus: HttpStatusCode.OK_200
329 // ---------------------------------------------------------------------------
331 get (options: OverrideCommandOptions & {
333 withStats?: boolean // default false
335 const { userId, withStats } = options
336 const path = '/api/v1/users/' + userId
338 return this.getRequestBody<User>({
342 query: { withStats },
344 defaultExpectedStatus: HttpStatusCode.OK_200
348 list (options: OverrideCommandOptions & {
355 const path = '/api/v1/users'
357 return this.getRequestBody<ResultList<User>>({
361 query: pick(options, [ 'start', 'count', 'sort', 'search', 'blocked' ]),
363 defaultExpectedStatus: HttpStatusCode.OK_200
367 remove (options: OverrideCommandOptions & {
370 const { userId } = options
371 const path = '/api/v1/users/' + userId
373 return this.deleteRequest({
378 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
382 update (options: OverrideCommandOptions & {
385 emailVerified?: boolean
387 videoQuotaDaily?: number
389 adminFlags?: UserAdminFlag
393 const path = '/api/v1/users/' + options.userId
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
405 return this.putBodyRequest({
411 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204