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