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