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