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