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