1 import { omit, pick } from '@shared/core-utils'
15 } from '@shared/models'
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',
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, role?: UserRole) {
194 const password = 'password'
195 const user = await this.create({ username, password, role })
197 const token = await this.server.login.getAccessToken({ username, password })
199 const me = await this.getMyInfo({ token })
204 userChannelId: me.videoChannels[0].id,
205 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 // ---------------------------------------------------------------------------
219 getMyInfo (options: OverrideCommandOptions = {}) {
220 const path = '/api/v1/users/me'
222 return this.getRequestBody<MyUser>({
227 defaultExpectedStatus: HttpStatusCode.OK_200
231 getMyQuotaUsed (options: OverrideCommandOptions = {}) {
232 const path = '/api/v1/users/me/video-quota-used'
234 return this.getRequestBody<UserVideoQuota>({
239 defaultExpectedStatus: HttpStatusCode.OK_200
243 getMyRating (options: OverrideCommandOptions & {
244 videoId: number | string
246 const { videoId } = options
247 const path = '/api/v1/users/me/videos/' + videoId + '/rating'
249 return this.getRequestBody<UserVideoRate>({
254 defaultExpectedStatus: HttpStatusCode.OK_200
258 deleteMe (options: OverrideCommandOptions = {}) {
259 const path = '/api/v1/users/me'
261 return this.deleteRequest({
266 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
270 updateMe (options: OverrideCommandOptions & UserUpdateMe) {
271 const path = '/api/v1/users/me'
273 const toSend: UserUpdateMe = omit(options, [ 'expectedStatus', 'token' ])
275 return this.putBodyRequest({
281 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
285 updateMyAvatar (options: OverrideCommandOptions & {
288 const { fixture } = options
289 const path = '/api/v1/users/me/avatar/pick'
291 return this.updateImageRequest({
296 fieldname: 'avatarfile',
299 defaultExpectedStatus: HttpStatusCode.OK_200
303 // ---------------------------------------------------------------------------
305 get (options: OverrideCommandOptions & {
307 withStats?: boolean // default false
309 const { userId, withStats } = options
310 const path = '/api/v1/users/' + userId
312 return this.getRequestBody<User>({
316 query: { withStats },
318 defaultExpectedStatus: HttpStatusCode.OK_200
322 list (options: OverrideCommandOptions & {
329 const path = '/api/v1/users'
331 return this.getRequestBody<ResultList<User>>({
335 query: pick(options, [ 'start', 'count', 'sort', 'search', 'blocked' ]),
337 defaultExpectedStatus: HttpStatusCode.OK_200
341 remove (options: OverrideCommandOptions & {
344 const { userId } = options
345 const path = '/api/v1/users/' + userId
347 return this.deleteRequest({
352 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
356 update (options: OverrideCommandOptions & {
359 emailVerified?: boolean
361 videoQuotaDaily?: number
363 adminFlags?: UserAdminFlag
367 const path = '/api/v1/users/' + options.userId
369 const toSend: UserUpdate = {}
370 if (options.password !== undefined && options.password !== null) toSend.password = options.password
371 if (options.email !== undefined && options.email !== null) toSend.email = options.email
372 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend.emailVerified = options.emailVerified
373 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend.videoQuota = options.videoQuota
374 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend.videoQuotaDaily = options.videoQuotaDaily
375 if (options.role !== undefined && options.role !== null) toSend.role = options.role
376 if (options.adminFlags !== undefined && options.adminFlags !== null) toSend.adminFlags = options.adminFlags
377 if (options.pluginAuth !== undefined) toSend.pluginAuth = options.pluginAuth
379 return this.putBodyRequest({
385 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204