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