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