]>
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 | |
2baea0c7 C |
177 | const additionalAttributes = { |
178 | waitTranscoding: true, | |
179 | state: true, | |
180 | scheduledUpdate: true | |
181 | } | |
2186386c | 182 | return res.json(getFormattedObjects(resultList.data, resultList.total, { additionalAttributes })) |
fd45e8f4 C |
183 | } |
184 | ||
90d4bb81 | 185 | async function createUser (req: express.Request, res: express.Response) { |
4771e000 | 186 | const body: UserCreate = req.body |
f05a1c30 | 187 | const userToCreate = new UserModel({ |
4771e000 C |
188 | username: body.username, |
189 | password: body.password, | |
190 | email: body.email, | |
0883b324 | 191 | nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, |
7efe153b | 192 | autoPlayVideo: true, |
954605a8 | 193 | role: body.role, |
b0f9f39e | 194 | videoQuota: body.videoQuota |
9bd26629 C |
195 | }) |
196 | ||
f05a1c30 | 197 | const { user, account } = await createUserAccountAndChannel(userToCreate) |
eb080476 | 198 | |
38fa2065 | 199 | logger.info('User %s with its channel and account created.', body.username) |
f05a1c30 | 200 | |
90d4bb81 C |
201 | return res.json({ |
202 | user: { | |
203 | id: user.id, | |
204 | account: { | |
205 | id: account.id, | |
206 | uuid: account.Actor.uuid | |
207 | } | |
208 | } | |
209 | }).end() | |
47e0652b C |
210 | } |
211 | ||
90d4bb81 | 212 | async function registerUser (req: express.Request, res: express.Response) { |
77a5501f C |
213 | const body: UserCreate = req.body |
214 | ||
3fd3ab2d | 215 | const user = new UserModel({ |
77a5501f C |
216 | username: body.username, |
217 | password: body.password, | |
218 | email: body.email, | |
0883b324 | 219 | nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, |
7efe153b | 220 | autoPlayVideo: true, |
954605a8 | 221 | role: UserRole.USER, |
77a5501f C |
222 | videoQuota: CONFIG.USER.VIDEO_QUOTA |
223 | }) | |
224 | ||
38fa2065 | 225 | await createUserAccountAndChannel(user) |
47e0652b C |
226 | |
227 | logger.info('User %s with its channel and account registered.', body.username) | |
90d4bb81 C |
228 | |
229 | return res.type('json').status(204).end() | |
77a5501f C |
230 | } |
231 | ||
eb080476 | 232 | async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) { |
fd45e8f4 | 233 | // We did not load channels in res.locals.user |
3fd3ab2d | 234 | const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username) |
eb080476 C |
235 | |
236 | return res.json(user.toFormattedJSON()) | |
99a64bfe C |
237 | } |
238 | ||
ce5496d6 C |
239 | async function getUserVideoQuotaUsed (req: express.Request, res: express.Response, next: express.NextFunction) { |
240 | // We did not load channels in res.locals.user | |
241 | const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username) | |
242 | const videoQuotaUsed = await UserModel.getOriginalVideoFileTotalFromUser(user) | |
243 | ||
5fcbd898 | 244 | const data: UserVideoQuota = { |
ce5496d6 | 245 | videoQuotaUsed |
5fcbd898 C |
246 | } |
247 | return res.json(data) | |
ce5496d6 C |
248 | } |
249 | ||
8094a898 | 250 | function getUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
ce5496d6 | 251 | return res.json((res.locals.user as UserModel).toFormattedJSON()) |
8094a898 C |
252 | } |
253 | ||
eb080476 | 254 | async function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) { |
0a6658fd | 255 | const videoId = +req.params.videoId |
571389d4 | 256 | const accountId = +res.locals.oauth.token.User.Account.id |
d38b8281 | 257 | |
3fd3ab2d | 258 | const ratingObj = await AccountVideoRateModel.load(accountId, videoId, null) |
faab3a84 C |
259 | const rating = ratingObj ? ratingObj.type : 'none' |
260 | ||
261 | const json: FormattedUserVideoRate = { | |
262 | videoId, | |
263 | rating | |
264 | } | |
265 | res.json(json) | |
d38b8281 C |
266 | } |
267 | ||
eb080476 | 268 | async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 269 | const resultList = await UserModel.listForApi(req.query.start, req.query.count, req.query.sort) |
eb080476 C |
270 | |
271 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
9bd26629 C |
272 | } |
273 | ||
eb080476 | 274 | async function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 275 | const user = await UserModel.loadById(req.params.id) |
eb080476 C |
276 | |
277 | await user.destroy() | |
278 | ||
279 | return res.sendStatus(204) | |
9bd26629 C |
280 | } |
281 | ||
eb080476 | 282 | async function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) { |
8094a898 | 283 | const body: UserUpdateMe = req.body |
4771e000 | 284 | |
2422c46b | 285 | const user: UserModel = res.locals.oauth.token.user |
1d49e1e2 | 286 | |
eb080476 C |
287 | if (body.password !== undefined) user.password = body.password |
288 | if (body.email !== undefined) user.email = body.email | |
0883b324 | 289 | if (body.nsfwPolicy !== undefined) user.nsfwPolicy = body.nsfwPolicy |
7efe153b | 290 | if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo |
eb080476 | 291 | |
2422c46b C |
292 | await sequelizeTypescript.transaction(async t => { |
293 | await user.save({ transaction: t }) | |
294 | ||
ed56ad11 | 295 | if (body.displayName !== undefined) user.Account.name = body.displayName |
2422c46b C |
296 | if (body.description !== undefined) user.Account.description = body.description |
297 | await user.Account.save({ transaction: t }) | |
298 | ||
299 | await sendUpdateActor(user.Account, t) | |
300 | }) | |
eb080476 | 301 | |
d412e80e | 302 | return res.sendStatus(204) |
9bd26629 C |
303 | } |
304 | ||
c5911fd3 | 305 | async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) { |
2186386c | 306 | const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ] |
265ba139 C |
307 | const user = res.locals.oauth.token.user |
308 | const actor = user.Account.Actor | |
c5911fd3 | 309 | |
c5911fd3 C |
310 | const extension = extname(avatarPhysicalFile.filename) |
311 | const avatarName = uuidv4() + extension | |
ac81d1a0 C |
312 | const destination = join(CONFIG.STORAGE.AVATARS_DIR, avatarName) |
313 | await processImage(avatarPhysicalFile, destination, AVATARS_SIZE) | |
c5911fd3 | 314 | |
a5625b41 | 315 | const avatar = await sequelizeTypescript.transaction(async t => { |
bb82394c C |
316 | const updatedActor = await updateActorAvatarInstance(actor, avatarName, t) |
317 | await updatedActor.save({ transaction: t }) | |
c5911fd3 | 318 | |
2422c46b | 319 | await sendUpdateActor(user.Account, t) |
c5911fd3 | 320 | |
bb82394c | 321 | return updatedActor.Avatar |
c5911fd3 C |
322 | }) |
323 | ||
324 | return res | |
325 | .json({ | |
326 | avatar: avatar.toFormattedJSON() | |
327 | }) | |
328 | .end() | |
329 | } | |
330 | ||
eb080476 | 331 | async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
8094a898 | 332 | const body: UserUpdate = req.body |
3fd3ab2d | 333 | const user = res.locals.user as UserModel |
f8b8c36b | 334 | const roleChanged = body.role !== undefined && body.role !== user.role |
8094a898 C |
335 | |
336 | if (body.email !== undefined) user.email = body.email | |
337 | if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota | |
954605a8 | 338 | if (body.role !== undefined) user.role = body.role |
8094a898 | 339 | |
eb080476 C |
340 | await user.save() |
341 | ||
f8b8c36b C |
342 | // Destroy user token to refresh rights |
343 | if (roleChanged) { | |
344 | await OAuthTokenModel.deleteUserToken(user.id) | |
345 | } | |
346 | ||
265ba139 C |
347 | // Don't need to send this update to followers, these attributes are not propagated |
348 | ||
eb080476 | 349 | return res.sendStatus(204) |
8094a898 C |
350 | } |
351 | ||
ecb4e35f C |
352 | async function askResetUserPassword (req: express.Request, res: express.Response, next: express.NextFunction) { |
353 | const user = res.locals.user as UserModel | |
354 | ||
355 | const verificationString = await Redis.Instance.setResetPasswordVerificationString(user.id) | |
356 | const url = CONFIG.WEBSERVER.URL + '/reset-password?userId=' + user.id + '&verificationString=' + verificationString | |
357 | await Emailer.Instance.addForgetPasswordEmailJob(user.email, url) | |
358 | ||
359 | return res.status(204).end() | |
360 | } | |
361 | ||
362 | async function resetUserPassword (req: express.Request, res: express.Response, next: express.NextFunction) { | |
363 | const user = res.locals.user as UserModel | |
364 | user.password = req.body.password | |
365 | ||
366 | await user.save() | |
367 | ||
368 | return res.status(204).end() | |
369 | } | |
370 | ||
69818c93 | 371 | function success (req: express.Request, res: express.Response, next: express.NextFunction) { |
9457bf88 C |
372 | res.end() |
373 | } |