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