]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
571389d4 C |
2 | import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared' |
3 | import { getFormattedObjects, logger, retryTransactionWrapper } from '../../helpers' | |
3fd3ab2d | 4 | import { CONFIG } from '../../initializers' |
571389d4 | 5 | import { createUserAccountAndChannel } from '../../lib' |
65fcc311 | 6 | import { |
571389d4 | 7 | asyncMiddleware, |
65fcc311 | 8 | authenticate, |
954605a8 | 9 | ensureUserHasRight, |
291e8d3e | 10 | ensureUserRegistrationAllowed, |
65fcc311 C |
11 | paginationValidator, |
12 | setPagination, | |
65fcc311 | 13 | setUsersSort, |
3fd3ab2d | 14 | setVideosSort, |
eb080476 | 15 | token, |
571389d4 C |
16 | usersAddValidator, |
17 | usersGetValidator, | |
18 | usersRegisterValidator, | |
19 | usersRemoveValidator, | |
20 | usersSortValidator, | |
21 | usersUpdateMeValidator, | |
22 | usersUpdateValidator, | |
23 | usersVideoRatingValidator | |
65fcc311 | 24 | } from '../../middlewares' |
3fd3ab2d C |
25 | import { videosSortValidator } from '../../middlewares/validators' |
26 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' | |
27 | import { UserModel } from '../../models/account/user' | |
28 | import { VideoModel } from '../../models/video/video' | |
65fcc311 C |
29 | |
30 | const usersRouter = express.Router() | |
31 | ||
32 | usersRouter.get('/me', | |
33 | authenticate, | |
eb080476 | 34 | asyncMiddleware(getUserInformation) |
d38b8281 C |
35 | ) |
36 | ||
fd45e8f4 C |
37 | usersRouter.get('/me/videos', |
38 | authenticate, | |
39 | paginationValidator, | |
40 | videosSortValidator, | |
41 | setVideosSort, | |
42 | setPagination, | |
43 | asyncMiddleware(getUserVideos) | |
44 | ) | |
45 | ||
65fcc311 C |
46 | usersRouter.get('/me/videos/:videoId/rating', |
47 | authenticate, | |
a2431b7d | 48 | asyncMiddleware(usersVideoRatingValidator), |
eb080476 | 49 | asyncMiddleware(getUserVideoRating) |
d38b8281 | 50 | ) |
9bd26629 | 51 | |
65fcc311 | 52 | usersRouter.get('/', |
86d13ec2 C |
53 | authenticate, |
54 | ensureUserHasRight(UserRight.MANAGE_USERS), | |
65fcc311 C |
55 | paginationValidator, |
56 | usersSortValidator, | |
57 | setUsersSort, | |
58 | setPagination, | |
eb080476 | 59 | asyncMiddleware(listUsers) |
5c39adb7 C |
60 | ) |
61 | ||
8094a898 | 62 | usersRouter.get('/:id', |
a2431b7d | 63 | asyncMiddleware(usersGetValidator), |
8094a898 C |
64 | getUser |
65 | ) | |
66 | ||
65fcc311 C |
67 | usersRouter.post('/', |
68 | authenticate, | |
954605a8 | 69 | ensureUserHasRight(UserRight.MANAGE_USERS), |
a2431b7d C |
70 | asyncMiddleware(usersAddValidator), |
71 | asyncMiddleware(createUserRetryWrapper) | |
9bd26629 C |
72 | ) |
73 | ||
65fcc311 | 74 | usersRouter.post('/register', |
a2431b7d C |
75 | asyncMiddleware(ensureUserRegistrationAllowed), |
76 | asyncMiddleware(usersRegisterValidator), | |
47e0652b | 77 | asyncMiddleware(registerUserRetryWrapper) |
2c2e9092 C |
78 | ) |
79 | ||
8094a898 C |
80 | usersRouter.put('/me', |
81 | authenticate, | |
82 | usersUpdateMeValidator, | |
eb080476 | 83 | asyncMiddleware(updateMe) |
8094a898 C |
84 | ) |
85 | ||
65fcc311 C |
86 | usersRouter.put('/:id', |
87 | authenticate, | |
954605a8 | 88 | ensureUserHasRight(UserRight.MANAGE_USERS), |
a2431b7d | 89 | asyncMiddleware(usersUpdateValidator), |
eb080476 | 90 | asyncMiddleware(updateUser) |
9bd26629 C |
91 | ) |
92 | ||
65fcc311 C |
93 | usersRouter.delete('/:id', |
94 | authenticate, | |
954605a8 | 95 | ensureUserHasRight(UserRight.MANAGE_USERS), |
a2431b7d | 96 | asyncMiddleware(usersRemoveValidator), |
eb080476 | 97 | asyncMiddleware(removeUser) |
9bd26629 | 98 | ) |
6606150c | 99 | |
65fcc311 | 100 | usersRouter.post('/token', token, success) |
9bd26629 | 101 | // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route |
9457bf88 C |
102 | |
103 | // --------------------------------------------------------------------------- | |
104 | ||
65fcc311 C |
105 | export { |
106 | usersRouter | |
107 | } | |
9457bf88 C |
108 | |
109 | // --------------------------------------------------------------------------- | |
110 | ||
fd45e8f4 | 111 | async function getUserVideos (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d C |
112 | const user = res.locals.oauth.token.User as UserModel |
113 | const resultList = await VideoModel.listUserVideosForApi(user.id ,req.query.start, req.query.count, req.query.sort) | |
fd45e8f4 C |
114 | |
115 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
116 | } | |
117 | ||
eb080476 | 118 | async function createUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { |
72c7248b | 119 | const options = { |
47e0652b | 120 | arguments: [ req ], |
72c7248b C |
121 | errorMessage: 'Cannot insert the user with many retries.' |
122 | } | |
123 | ||
eb080476 C |
124 | await retryTransactionWrapper(createUser, options) |
125 | ||
126 | // TODO : include Location of the new user -> 201 | |
127 | return res.type('json').status(204).end() | |
72c7248b C |
128 | } |
129 | ||
47e0652b | 130 | async function createUser (req: express.Request) { |
4771e000 | 131 | const body: UserCreate = req.body |
3fd3ab2d | 132 | const user = new UserModel({ |
4771e000 C |
133 | username: body.username, |
134 | password: body.password, | |
135 | email: body.email, | |
1d49e1e2 | 136 | displayNSFW: false, |
954605a8 | 137 | role: body.role, |
b0f9f39e | 138 | videoQuota: body.videoQuota |
9bd26629 C |
139 | }) |
140 | ||
38fa2065 | 141 | await createUserAccountAndChannel(user) |
eb080476 | 142 | |
38fa2065 | 143 | logger.info('User %s with its channel and account created.', body.username) |
9bd26629 C |
144 | } |
145 | ||
47e0652b C |
146 | async function registerUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { |
147 | const options = { | |
148 | arguments: [ req ], | |
149 | errorMessage: 'Cannot insert the user with many retries.' | |
150 | } | |
151 | ||
152 | await retryTransactionWrapper(registerUser, options) | |
153 | ||
154 | return res.type('json').status(204).end() | |
155 | } | |
156 | ||
157 | async function registerUser (req: express.Request) { | |
77a5501f C |
158 | const body: UserCreate = req.body |
159 | ||
3fd3ab2d | 160 | const user = new UserModel({ |
77a5501f C |
161 | username: body.username, |
162 | password: body.password, | |
163 | email: body.email, | |
164 | displayNSFW: false, | |
954605a8 | 165 | role: UserRole.USER, |
77a5501f C |
166 | videoQuota: CONFIG.USER.VIDEO_QUOTA |
167 | }) | |
168 | ||
38fa2065 | 169 | await createUserAccountAndChannel(user) |
47e0652b C |
170 | |
171 | logger.info('User %s with its channel and account registered.', body.username) | |
77a5501f C |
172 | } |
173 | ||
eb080476 | 174 | async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) { |
fd45e8f4 | 175 | // We did not load channels in res.locals.user |
3fd3ab2d | 176 | const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username) |
eb080476 C |
177 | |
178 | return res.json(user.toFormattedJSON()) | |
99a64bfe C |
179 | } |
180 | ||
8094a898 | 181 | function getUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
11474c3c | 182 | return res.json(res.locals.user.toFormattedJSON()) |
8094a898 C |
183 | } |
184 | ||
eb080476 | 185 | async function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) { |
0a6658fd | 186 | const videoId = +req.params.videoId |
571389d4 | 187 | const accountId = +res.locals.oauth.token.User.Account.id |
d38b8281 | 188 | |
3fd3ab2d | 189 | const ratingObj = await AccountVideoRateModel.load(accountId, videoId, null) |
faab3a84 C |
190 | const rating = ratingObj ? ratingObj.type : 'none' |
191 | ||
192 | const json: FormattedUserVideoRate = { | |
193 | videoId, | |
194 | rating | |
195 | } | |
196 | res.json(json) | |
d38b8281 C |
197 | } |
198 | ||
eb080476 | 199 | async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 200 | const resultList = await UserModel.listForApi(req.query.start, req.query.count, req.query.sort) |
eb080476 C |
201 | |
202 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
9bd26629 C |
203 | } |
204 | ||
eb080476 | 205 | async function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 206 | const user = await UserModel.loadById(req.params.id) |
eb080476 C |
207 | |
208 | await user.destroy() | |
209 | ||
210 | return res.sendStatus(204) | |
9bd26629 C |
211 | } |
212 | ||
eb080476 | 213 | async function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) { |
8094a898 | 214 | const body: UserUpdateMe = req.body |
4771e000 | 215 | |
8094a898 | 216 | // FIXME: user is not already a Sequelize instance? |
eb080476 | 217 | const user = res.locals.oauth.token.user |
1d49e1e2 | 218 | |
eb080476 C |
219 | if (body.password !== undefined) user.password = body.password |
220 | if (body.email !== undefined) user.email = body.email | |
221 | if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW | |
222 | ||
223 | await user.save() | |
224 | ||
d412e80e | 225 | return res.sendStatus(204) |
9bd26629 C |
226 | } |
227 | ||
eb080476 | 228 | async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
8094a898 | 229 | const body: UserUpdate = req.body |
3fd3ab2d | 230 | const user = res.locals.user as UserModel |
8094a898 C |
231 | |
232 | if (body.email !== undefined) user.email = body.email | |
233 | if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota | |
954605a8 | 234 | if (body.role !== undefined) user.role = body.role |
8094a898 | 235 | |
eb080476 C |
236 | await user.save() |
237 | ||
238 | return res.sendStatus(204) | |
8094a898 C |
239 | } |
240 | ||
69818c93 | 241 | function success (req: express.Request, res: express.Response, next: express.NextFunction) { |
9457bf88 C |
242 | res.end() |
243 | } |