]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
ac81d1a0 | 2 | import 'multer' |
490b595a | 3 | import * as RateLimit from 'express-rate-limit' |
571389d4 | 4 | import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared' |
da854ddd | 5 | import { logger } from '../../helpers/logger' |
0626e7af | 6 | import { getFormattedObjects } from '../../helpers/utils' |
4bbfc6c6 | 7 | import { CONFIG, IMAGE_MIMETYPE_EXT, RATES_LIMIT, sequelizeTypescript } from '../../initializers' |
2422c46b | 8 | import { sendUpdateActor } from '../../lib/activitypub/send' |
ecb4e35f | 9 | import { Emailer } from '../../lib/emailer' |
ecb4e35f | 10 | import { Redis } from '../../lib/redis' |
50d6de9c | 11 | import { createUserAccountAndChannel } from '../../lib/user' |
65fcc311 | 12 | import { |
f076daa7 | 13 | asyncMiddleware, |
90d4bb81 | 14 | asyncRetryTransactionMiddleware, |
f076daa7 C |
15 | authenticate, |
16 | ensureUserHasRight, | |
17 | ensureUserRegistrationAllowed, | |
ff2c1fe8 | 18 | ensureUserRegistrationAllowedForIP, |
f076daa7 C |
19 | paginationValidator, |
20 | setDefaultPagination, | |
21 | setDefaultSort, | |
22 | token, | |
23 | usersAddValidator, | |
24 | usersGetValidator, | |
25 | usersRegisterValidator, | |
26 | usersRemoveValidator, | |
27 | usersSortValidator, | |
28 | usersUpdateMeValidator, | |
29 | usersUpdateValidator, | |
30 | usersVideoRatingValidator | |
65fcc311 | 31 | } from '../../middlewares' |
4bbfc6c6 | 32 | import { usersAskResetPasswordValidator, usersResetPasswordValidator, videosSortValidator } from '../../middlewares/validators' |
3fd3ab2d C |
33 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' |
34 | import { UserModel } from '../../models/account/user' | |
f8b8c36b | 35 | import { OAuthTokenModel } from '../../models/oauth/oauth-token' |
3fd3ab2d | 36 | import { VideoModel } from '../../models/video/video' |
0883b324 | 37 | import { VideoSortField } from '../../../client/src/app/shared/video/sort-field.type' |
0626e7af | 38 | import { createReqFiles } from '../../helpers/express-utils' |
5fcbd898 | 39 | import { UserVideoQuota } from '../../../shared/models/users/user-video-quota.model' |
4bbfc6c6 C |
40 | import { updateAvatarValidator } from '../../middlewares/validators/avatar' |
41 | import { updateActorAvatarFile } from '../../lib/avatar' | |
65fcc311 | 42 | |
ac81d1a0 | 43 | const reqAvatarFile = createReqFiles([ 'avatarfile' ], IMAGE_MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.AVATARS_DIR }) |
490b595a C |
44 | const loginRateLimiter = new RateLimit({ |
45 | windowMs: RATES_LIMIT.LOGIN.WINDOW_MS, | |
46 | max: RATES_LIMIT.LOGIN.MAX, | |
47 | delayMs: 0 | |
48 | }) | |
c5911fd3 | 49 | |
65fcc311 C |
50 | const usersRouter = express.Router() |
51 | ||
52 | usersRouter.get('/me', | |
53 | authenticate, | |
eb080476 | 54 | asyncMiddleware(getUserInformation) |
d38b8281 C |
55 | ) |
56 | ||
ce5496d6 C |
57 | usersRouter.get('/me/video-quota-used', |
58 | authenticate, | |
59 | asyncMiddleware(getUserVideoQuotaUsed) | |
60 | ) | |
61 | ||
fd45e8f4 C |
62 | usersRouter.get('/me/videos', |
63 | authenticate, | |
64 | paginationValidator, | |
65 | videosSortValidator, | |
1174a847 | 66 | setDefaultSort, |
f05a1c30 | 67 | setDefaultPagination, |
fd45e8f4 C |
68 | asyncMiddleware(getUserVideos) |
69 | ) | |
70 | ||
65fcc311 C |
71 | usersRouter.get('/me/videos/:videoId/rating', |
72 | authenticate, | |
a2431b7d | 73 | asyncMiddleware(usersVideoRatingValidator), |
eb080476 | 74 | asyncMiddleware(getUserVideoRating) |
d38b8281 | 75 | ) |
9bd26629 | 76 | |
65fcc311 | 77 | usersRouter.get('/', |
86d13ec2 C |
78 | authenticate, |
79 | ensureUserHasRight(UserRight.MANAGE_USERS), | |
65fcc311 C |
80 | paginationValidator, |
81 | usersSortValidator, | |
1174a847 | 82 | setDefaultSort, |
f05a1c30 | 83 | setDefaultPagination, |
eb080476 | 84 | asyncMiddleware(listUsers) |
5c39adb7 C |
85 | ) |
86 | ||
8094a898 | 87 | usersRouter.get('/:id', |
94ff4c23 C |
88 | authenticate, |
89 | ensureUserHasRight(UserRight.MANAGE_USERS), | |
a2431b7d | 90 | asyncMiddleware(usersGetValidator), |
8094a898 C |
91 | getUser |
92 | ) | |
93 | ||
65fcc311 C |
94 | usersRouter.post('/', |
95 | authenticate, | |
954605a8 | 96 | ensureUserHasRight(UserRight.MANAGE_USERS), |
a2431b7d | 97 | asyncMiddleware(usersAddValidator), |
90d4bb81 | 98 | asyncRetryTransactionMiddleware(createUser) |
9bd26629 C |
99 | ) |
100 | ||
65fcc311 | 101 | usersRouter.post('/register', |
a2431b7d | 102 | asyncMiddleware(ensureUserRegistrationAllowed), |
ff2c1fe8 | 103 | ensureUserRegistrationAllowedForIP, |
a2431b7d | 104 | asyncMiddleware(usersRegisterValidator), |
90d4bb81 | 105 | asyncRetryTransactionMiddleware(registerUser) |
2c2e9092 C |
106 | ) |
107 | ||
8094a898 C |
108 | usersRouter.put('/me', |
109 | authenticate, | |
110 | usersUpdateMeValidator, | |
eb080476 | 111 | asyncMiddleware(updateMe) |
8094a898 C |
112 | ) |
113 | ||
c5911fd3 C |
114 | usersRouter.post('/me/avatar/pick', |
115 | authenticate, | |
116 | reqAvatarFile, | |
4bbfc6c6 | 117 | updateAvatarValidator, |
c5911fd3 C |
118 | asyncMiddleware(updateMyAvatar) |
119 | ) | |
120 | ||
65fcc311 C |
121 | usersRouter.put('/:id', |
122 | authenticate, | |
954605a8 | 123 | ensureUserHasRight(UserRight.MANAGE_USERS), |
a2431b7d | 124 | asyncMiddleware(usersUpdateValidator), |
eb080476 | 125 | asyncMiddleware(updateUser) |
9bd26629 C |
126 | ) |
127 | ||
65fcc311 C |
128 | usersRouter.delete('/:id', |
129 | authenticate, | |
954605a8 | 130 | ensureUserHasRight(UserRight.MANAGE_USERS), |
a2431b7d | 131 | asyncMiddleware(usersRemoveValidator), |
eb080476 | 132 | asyncMiddleware(removeUser) |
9bd26629 | 133 | ) |
6606150c | 134 | |
ecb4e35f C |
135 | usersRouter.post('/ask-reset-password', |
136 | asyncMiddleware(usersAskResetPasswordValidator), | |
137 | asyncMiddleware(askResetUserPassword) | |
138 | ) | |
139 | ||
140 | usersRouter.post('/:id/reset-password', | |
141 | asyncMiddleware(usersResetPasswordValidator), | |
142 | asyncMiddleware(resetUserPassword) | |
143 | ) | |
144 | ||
490b595a C |
145 | usersRouter.post('/token', |
146 | loginRateLimiter, | |
147 | token, | |
148 | success | |
149 | ) | |
9bd26629 | 150 | // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route |
9457bf88 C |
151 | |
152 | // --------------------------------------------------------------------------- | |
153 | ||
65fcc311 C |
154 | export { |
155 | usersRouter | |
156 | } | |
9457bf88 C |
157 | |
158 | // --------------------------------------------------------------------------- | |
159 | ||
fd45e8f4 | 160 | async function getUserVideos (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 161 | const user = res.locals.oauth.token.User as UserModel |
2186386c | 162 | const resultList = await VideoModel.listUserVideosForApi( |
0883b324 C |
163 | user.Account.id, |
164 | req.query.start as number, | |
165 | req.query.count as number, | |
166 | req.query.sort as VideoSortField, | |
167 | false // Display my NSFW videos | |
168 | ) | |
fd45e8f4 | 169 | |
2baea0c7 C |
170 | const additionalAttributes = { |
171 | waitTranscoding: true, | |
172 | state: true, | |
173 | scheduledUpdate: true | |
174 | } | |
2186386c | 175 | return res.json(getFormattedObjects(resultList.data, resultList.total, { additionalAttributes })) |
fd45e8f4 C |
176 | } |
177 | ||
90d4bb81 | 178 | async function createUser (req: express.Request, res: express.Response) { |
4771e000 | 179 | const body: UserCreate = req.body |
f05a1c30 | 180 | const userToCreate = new UserModel({ |
4771e000 C |
181 | username: body.username, |
182 | password: body.password, | |
183 | email: body.email, | |
0883b324 | 184 | nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, |
7efe153b | 185 | autoPlayVideo: true, |
954605a8 | 186 | role: body.role, |
b0f9f39e | 187 | videoQuota: body.videoQuota |
9bd26629 C |
188 | }) |
189 | ||
f05a1c30 | 190 | const { user, account } = await createUserAccountAndChannel(userToCreate) |
eb080476 | 191 | |
38fa2065 | 192 | logger.info('User %s with its channel and account created.', body.username) |
f05a1c30 | 193 | |
90d4bb81 C |
194 | return res.json({ |
195 | user: { | |
196 | id: user.id, | |
197 | account: { | |
198 | id: account.id, | |
199 | uuid: account.Actor.uuid | |
200 | } | |
201 | } | |
202 | }).end() | |
47e0652b C |
203 | } |
204 | ||
90d4bb81 | 205 | async function registerUser (req: express.Request, res: express.Response) { |
77a5501f C |
206 | const body: UserCreate = req.body |
207 | ||
3fd3ab2d | 208 | const user = new UserModel({ |
77a5501f C |
209 | username: body.username, |
210 | password: body.password, | |
211 | email: body.email, | |
0883b324 | 212 | nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, |
7efe153b | 213 | autoPlayVideo: true, |
954605a8 | 214 | role: UserRole.USER, |
77a5501f C |
215 | videoQuota: CONFIG.USER.VIDEO_QUOTA |
216 | }) | |
217 | ||
38fa2065 | 218 | await createUserAccountAndChannel(user) |
47e0652b C |
219 | |
220 | logger.info('User %s with its channel and account registered.', body.username) | |
90d4bb81 C |
221 | |
222 | return res.type('json').status(204).end() | |
77a5501f C |
223 | } |
224 | ||
eb080476 | 225 | async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) { |
fd45e8f4 | 226 | // We did not load channels in res.locals.user |
3fd3ab2d | 227 | const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username) |
eb080476 C |
228 | |
229 | return res.json(user.toFormattedJSON()) | |
99a64bfe C |
230 | } |
231 | ||
ce5496d6 C |
232 | async function getUserVideoQuotaUsed (req: express.Request, res: express.Response, next: express.NextFunction) { |
233 | // We did not load channels in res.locals.user | |
234 | const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username) | |
235 | const videoQuotaUsed = await UserModel.getOriginalVideoFileTotalFromUser(user) | |
236 | ||
5fcbd898 | 237 | const data: UserVideoQuota = { |
ce5496d6 | 238 | videoQuotaUsed |
5fcbd898 C |
239 | } |
240 | return res.json(data) | |
ce5496d6 C |
241 | } |
242 | ||
8094a898 | 243 | function getUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
ce5496d6 | 244 | return res.json((res.locals.user as UserModel).toFormattedJSON()) |
8094a898 C |
245 | } |
246 | ||
eb080476 | 247 | async function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) { |
0a6658fd | 248 | const videoId = +req.params.videoId |
571389d4 | 249 | const accountId = +res.locals.oauth.token.User.Account.id |
d38b8281 | 250 | |
3fd3ab2d | 251 | const ratingObj = await AccountVideoRateModel.load(accountId, videoId, null) |
faab3a84 C |
252 | const rating = ratingObj ? ratingObj.type : 'none' |
253 | ||
254 | const json: FormattedUserVideoRate = { | |
255 | videoId, | |
256 | rating | |
257 | } | |
258 | res.json(json) | |
d38b8281 C |
259 | } |
260 | ||
eb080476 | 261 | async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 262 | const resultList = await UserModel.listForApi(req.query.start, req.query.count, req.query.sort) |
eb080476 C |
263 | |
264 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
9bd26629 C |
265 | } |
266 | ||
eb080476 | 267 | async function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 268 | const user = await UserModel.loadById(req.params.id) |
eb080476 C |
269 | |
270 | await user.destroy() | |
271 | ||
272 | return res.sendStatus(204) | |
9bd26629 C |
273 | } |
274 | ||
eb080476 | 275 | async function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) { |
8094a898 | 276 | const body: UserUpdateMe = req.body |
4771e000 | 277 | |
2422c46b | 278 | const user: UserModel = res.locals.oauth.token.user |
1d49e1e2 | 279 | |
eb080476 C |
280 | if (body.password !== undefined) user.password = body.password |
281 | if (body.email !== undefined) user.email = body.email | |
0883b324 | 282 | if (body.nsfwPolicy !== undefined) user.nsfwPolicy = body.nsfwPolicy |
7efe153b | 283 | if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo |
eb080476 | 284 | |
2422c46b C |
285 | await sequelizeTypescript.transaction(async t => { |
286 | await user.save({ transaction: t }) | |
287 | ||
ed56ad11 | 288 | if (body.displayName !== undefined) user.Account.name = body.displayName |
2422c46b C |
289 | if (body.description !== undefined) user.Account.description = body.description |
290 | await user.Account.save({ transaction: t }) | |
291 | ||
292 | await sendUpdateActor(user.Account, t) | |
293 | }) | |
eb080476 | 294 | |
d412e80e | 295 | return res.sendStatus(204) |
9bd26629 C |
296 | } |
297 | ||
c5911fd3 | 298 | async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) { |
2186386c | 299 | const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ] |
4bbfc6c6 | 300 | const account = res.locals.oauth.token.user.Account |
c5911fd3 | 301 | |
4bbfc6c6 | 302 | const avatar = await updateActorAvatarFile(avatarPhysicalFile, account.Actor, account) |
c5911fd3 C |
303 | |
304 | return res | |
305 | .json({ | |
306 | avatar: avatar.toFormattedJSON() | |
307 | }) | |
308 | .end() | |
309 | } | |
310 | ||
eb080476 | 311 | async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
8094a898 | 312 | const body: UserUpdate = req.body |
3fd3ab2d | 313 | const user = res.locals.user as UserModel |
f8b8c36b | 314 | const roleChanged = body.role !== undefined && body.role !== user.role |
8094a898 C |
315 | |
316 | if (body.email !== undefined) user.email = body.email | |
317 | if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota | |
954605a8 | 318 | if (body.role !== undefined) user.role = body.role |
8094a898 | 319 | |
eb080476 C |
320 | await user.save() |
321 | ||
f8b8c36b C |
322 | // Destroy user token to refresh rights |
323 | if (roleChanged) { | |
324 | await OAuthTokenModel.deleteUserToken(user.id) | |
325 | } | |
326 | ||
265ba139 C |
327 | // Don't need to send this update to followers, these attributes are not propagated |
328 | ||
eb080476 | 329 | return res.sendStatus(204) |
8094a898 C |
330 | } |
331 | ||
ecb4e35f C |
332 | async function askResetUserPassword (req: express.Request, res: express.Response, next: express.NextFunction) { |
333 | const user = res.locals.user as UserModel | |
334 | ||
335 | const verificationString = await Redis.Instance.setResetPasswordVerificationString(user.id) | |
336 | const url = CONFIG.WEBSERVER.URL + '/reset-password?userId=' + user.id + '&verificationString=' + verificationString | |
337 | await Emailer.Instance.addForgetPasswordEmailJob(user.email, url) | |
338 | ||
339 | return res.status(204).end() | |
340 | } | |
341 | ||
342 | async function resetUserPassword (req: express.Request, res: express.Response, next: express.NextFunction) { | |
343 | const user = res.locals.user as UserModel | |
344 | user.password = req.body.password | |
345 | ||
346 | await user.save() | |
347 | ||
348 | return res.status(204).end() | |
349 | } | |
350 | ||
69818c93 | 351 | function success (req: express.Request, res: express.Response, next: express.NextFunction) { |
9457bf88 C |
352 | res.end() |
353 | } |