]>
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 C |
10 | usersAddValidator, |
11 | usersUpdateValidator, | |
12 | usersRemoveValidator, | |
13 | usersVideoRatingValidator, | |
14 | paginationValidator, | |
15 | setPagination, | |
16 | usersSortValidator, | |
17 | setUsersSort, | |
18 | token | |
19 | } from '../../middlewares' | |
0aef76c4 | 20 | import { UserVideoRate as FormattedUserVideoRate, UserCreate, UserUpdate } from '../../../shared' |
65fcc311 C |
21 | |
22 | const usersRouter = express.Router() | |
23 | ||
24 | usersRouter.get('/me', | |
25 | authenticate, | |
d38b8281 C |
26 | getUserInformation |
27 | ) | |
28 | ||
65fcc311 C |
29 | usersRouter.get('/me/videos/:videoId/rating', |
30 | authenticate, | |
31 | usersVideoRatingValidator, | |
d38b8281 C |
32 | getUserVideoRating |
33 | ) | |
9bd26629 | 34 | |
65fcc311 C |
35 | usersRouter.get('/', |
36 | paginationValidator, | |
37 | usersSortValidator, | |
38 | setUsersSort, | |
39 | setPagination, | |
5c39adb7 C |
40 | listUsers |
41 | ) | |
42 | ||
65fcc311 C |
43 | usersRouter.post('/', |
44 | authenticate, | |
45 | ensureIsAdmin, | |
46 | usersAddValidator, | |
9bd26629 C |
47 | createUser |
48 | ) | |
49 | ||
65fcc311 | 50 | usersRouter.post('/register', |
291e8d3e | 51 | ensureUserRegistrationAllowed, |
65fcc311 | 52 | usersAddValidator, |
2c2e9092 C |
53 | createUser |
54 | ) | |
55 | ||
65fcc311 C |
56 | usersRouter.put('/:id', |
57 | authenticate, | |
58 | usersUpdateValidator, | |
9bd26629 C |
59 | updateUser |
60 | ) | |
61 | ||
65fcc311 C |
62 | usersRouter.delete('/:id', |
63 | authenticate, | |
64 | ensureIsAdmin, | |
65 | usersRemoveValidator, | |
9bd26629 C |
66 | removeUser |
67 | ) | |
6606150c | 68 | |
65fcc311 | 69 | usersRouter.post('/token', token, success) |
9bd26629 | 70 | // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route |
9457bf88 C |
71 | |
72 | // --------------------------------------------------------------------------- | |
73 | ||
65fcc311 C |
74 | export { |
75 | usersRouter | |
76 | } | |
9457bf88 C |
77 | |
78 | // --------------------------------------------------------------------------- | |
79 | ||
69818c93 | 80 | function createUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
4771e000 C |
81 | const body: UserCreate = req.body |
82 | ||
b0f9f39e C |
83 | // On registration, we set the user video quota |
84 | if (body.videoQuota === undefined) { | |
85 | body.videoQuota = CONFIG.USER.VIDEO_QUOTA | |
86 | } | |
87 | ||
feb4bdfd | 88 | const user = db.User.build({ |
4771e000 C |
89 | username: body.username, |
90 | password: body.password, | |
91 | email: body.email, | |
1d49e1e2 | 92 | displayNSFW: false, |
b0f9f39e C |
93 | role: USER_ROLES.USER, |
94 | videoQuota: body.videoQuota | |
9bd26629 C |
95 | }) |
96 | ||
6fcd19ba C |
97 | user.save() |
98 | .then(() => res.type('json').status(204).end()) | |
99 | .catch(err => next(err)) | |
9bd26629 C |
100 | } |
101 | ||
69818c93 | 102 | function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) { |
6fcd19ba | 103 | db.User.loadByUsername(res.locals.oauth.token.user.username) |
0aef76c4 | 104 | .then(user => res.json(user.toFormattedJSON())) |
6fcd19ba | 105 | .catch(err => next(err)) |
99a64bfe C |
106 | } |
107 | ||
69818c93 | 108 | function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) { |
0a6658fd | 109 | const videoId = +req.params.videoId |
69818c93 | 110 | const userId = +res.locals.oauth.token.User.id |
d38b8281 | 111 | |
6fcd19ba C |
112 | db.UserVideoRate.load(userId, videoId, null) |
113 | .then(ratingObj => { | |
114 | const rating = ratingObj ? ratingObj.type : 'none' | |
0aef76c4 | 115 | const json: FormattedUserVideoRate = { |
6fcd19ba C |
116 | videoId, |
117 | rating | |
118 | } | |
119 | res.json(json) | |
120 | }) | |
121 | .catch(err => next(err)) | |
d38b8281 C |
122 | } |
123 | ||
69818c93 | 124 | function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) { |
6fcd19ba C |
125 | db.User.listForApi(req.query.start, req.query.count, req.query.sort) |
126 | .then(resultList => { | |
0aef76c4 | 127 | res.json(getFormattedObjects(resultList.data, resultList.total)) |
6fcd19ba C |
128 | }) |
129 | .catch(err => next(err)) | |
9bd26629 C |
130 | } |
131 | ||
69818c93 | 132 | function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
6fcd19ba C |
133 | db.User.loadById(req.params.id) |
134 | .then(user => user.destroy()) | |
135 | .then(() => res.sendStatus(204)) | |
136 | .catch(err => { | |
ad0997ad | 137 | logger.error('Errors when removed the user.', err) |
9bd26629 | 138 | return next(err) |
6fcd19ba | 139 | }) |
9bd26629 C |
140 | } |
141 | ||
69818c93 | 142 | function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
4771e000 C |
143 | const body: UserUpdate = req.body |
144 | ||
6fcd19ba C |
145 | db.User.loadByUsername(res.locals.oauth.token.user.username) |
146 | .then(user => { | |
4771e000 C |
147 | if (body.password) user.password = body.password |
148 | if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW | |
b0f9f39e | 149 | if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota |
1d49e1e2 | 150 | |
6fcd19ba | 151 | return user.save() |
9bd26629 | 152 | }) |
6fcd19ba C |
153 | .then(() => res.sendStatus(204)) |
154 | .catch(err => next(err)) | |
9bd26629 C |
155 | } |
156 | ||
69818c93 | 157 | function success (req: express.Request, res: express.Response, next: express.NextFunction) { |
9457bf88 C |
158 | res.end() |
159 | } |