1 import { omit } from 'lodash'
2 import { pick } from '@shared/core-utils'
15 } from '@shared/models'
16 import { ScopedToken } from '@shared/models/users/user-scoped-token'
17 import { unwrapBody } from '../requests'
18 import { AbstractCommand, OverrideCommandOptions } from '../shared'
20 export class UsersCommand extends AbstractCommand {
22 askResetPassword (options: OverrideCommandOptions & {
25 const { email } = options
26 const path = '/api/v1/users/ask-reset-password'
28 return this.postBodyRequest({
34 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
38 resetPassword (options: OverrideCommandOptions & {
40 verificationString: string
43 const { userId, verificationString, password } = options
44 const path = '/api/v1/users/' + userId + '/reset-password'
46 return this.postBodyRequest({
50 fields: { password, verificationString },
52 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
56 // ---------------------------------------------------------------------------
58 askSendVerifyEmail (options: OverrideCommandOptions & {
61 const { email } = options
62 const path = '/api/v1/users/ask-send-verify-email'
64 return this.postBodyRequest({
70 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
74 verifyEmail (options: OverrideCommandOptions & {
76 verificationString: string
77 isPendingEmail?: boolean // default false
79 const { userId, verificationString, isPendingEmail = false } = options
80 const path = '/api/v1/users/' + userId + '/verify-email'
82 return this.postBodyRequest({
91 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
95 // ---------------------------------------------------------------------------
97 banUser (options: OverrideCommandOptions & {
101 const { userId, reason } = options
102 const path = '/api/v1/users' + '/' + userId + '/block'
104 return this.postBodyRequest({
110 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
114 unbanUser (options: OverrideCommandOptions & {
117 const { userId } = options
118 const path = '/api/v1/users' + '/' + userId + '/unblock'
120 return this.postBodyRequest({
125 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
129 // ---------------------------------------------------------------------------
131 getMyScopedTokens (options: OverrideCommandOptions = {}) {
132 const path = '/api/v1/users/scoped-tokens'
134 return this.getRequestBody<ScopedToken>({
139 defaultExpectedStatus: HttpStatusCode.OK_200
143 renewMyScopedTokens (options: OverrideCommandOptions = {}) {
144 const path = '/api/v1/users/scoped-tokens'
146 return this.postBodyRequest({
151 defaultExpectedStatus: HttpStatusCode.OK_200
155 // ---------------------------------------------------------------------------
157 create (options: OverrideCommandOptions & {
161 videoQuotaDaily?: number
163 adminFlags?: UserAdminFlag
168 password = 'password',
169 videoQuota = 42000000,
170 videoQuotaDaily = -1,
174 const path = '/api/v1/users'
176 return unwrapBody<{ user: UserCreateResult }>(this.postBodyRequest({
185 email: username + '@example.com',
190 defaultExpectedStatus: HttpStatusCode.OK_200
191 })).then(res => res.user)
194 async generate (username: string, role?: UserRole) {
195 const password = 'password'
196 const user = await this.create({ username, password, role })
198 const token = await this.server.login.getAccessToken({ username, password })
200 const me = await this.getMyInfo({ token })
205 userChannelId: me.videoChannels[0].id,
206 userChannelName: me.videoChannels[0].name
210 async generateUserAndToken (username: string, role?: UserRole) {
211 const password = 'password'
212 await this.create({ username, password, role })
214 return this.server.login.getAccessToken({ username, password })
217 register (options: OverrideCommandOptions & {
226 const { username, password = 'password', displayName, channel } = options
227 const path = '/api/v1/users/register'
229 return this.postBodyRequest({
236 email: username + '@example.com',
240 implicitToken: false,
241 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
245 // ---------------------------------------------------------------------------
247 getMyInfo (options: OverrideCommandOptions = {}) {
248 const path = '/api/v1/users/me'
250 return this.getRequestBody<MyUser>({
255 defaultExpectedStatus: HttpStatusCode.OK_200
259 getMyQuotaUsed (options: OverrideCommandOptions = {}) {
260 const path = '/api/v1/users/me/video-quota-used'
262 return this.getRequestBody<UserVideoQuota>({
267 defaultExpectedStatus: HttpStatusCode.OK_200
271 getMyRating (options: OverrideCommandOptions & {
272 videoId: number | string
274 const { videoId } = options
275 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
277 return this.getRequestBody<UserVideoRate>({
282 defaultExpectedStatus: HttpStatusCode.OK_200
286 deleteMe (options: OverrideCommandOptions = {}) {
287 const path = '/api/v1/users/me'
289 return this.deleteRequest({
294 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
298 updateMe (options: OverrideCommandOptions & UserUpdateMe) {
299 const path = '/api/v1/users/me'
301 const toSend: UserUpdateMe = omit(options, 'url', 'accessToken')
303 return this.putBodyRequest({
309 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
313 updateMyAvatar (options: OverrideCommandOptions & {
316 const { fixture } = options
317 const path = '/api/v1/users/me/avatar/pick'
319 return this.updateImageRequest({
324 fieldname: 'avatarfile',
327 defaultExpectedStatus: HttpStatusCode.OK_200
331 // ---------------------------------------------------------------------------
333 get (options: OverrideCommandOptions & {
335 withStats?: boolean // default false
337 const { userId, withStats } = options
338 const path = '/api/v1/users/' + userId
340 return this.getRequestBody<User>({
344 query: { withStats },
346 defaultExpectedStatus: HttpStatusCode.OK_200
350 list (options: OverrideCommandOptions & {
357 const path = '/api/v1/users'
359 return this.getRequestBody<ResultList<User>>({
363 query: pick(options, [ 'start', 'count', 'sort', 'search', 'blocked' ]),
365 defaultExpectedStatus: HttpStatusCode.OK_200
369 remove (options: OverrideCommandOptions & {
372 const { userId } = options
373 const path = '/api/v1/users/' + userId
375 return this.deleteRequest({
380 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
384 update (options: OverrideCommandOptions & {
387 emailVerified?: boolean
389 videoQuotaDaily?: number
391 adminFlags?: UserAdminFlag
395 const path = '/api/v1/users/' + options.userId
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
407 return this.putBodyRequest({
413 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204