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