aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/users.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/users.ts')
-rw-r--r--server/controllers/api/users.ts108
1 files changed, 49 insertions, 59 deletions
diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts
index 6576e4333..a7528328a 100644
--- a/server/controllers/api/users.ts
+++ b/server/controllers/api/users.ts
@@ -18,7 +18,8 @@ import {
18 setPagination, 18 setPagination,
19 usersSortValidator, 19 usersSortValidator,
20 setUsersSort, 20 setUsersSort,
21 token 21 token,
22 asyncMiddleware
22} from '../../middlewares' 23} from '../../middlewares'
23import { 24import {
24 UserVideoRate as FormattedUserVideoRate, 25 UserVideoRate as FormattedUserVideoRate,
@@ -33,13 +34,13 @@ const usersRouter = express.Router()
33 34
34usersRouter.get('/me', 35usersRouter.get('/me',
35 authenticate, 36 authenticate,
36 getUserInformation 37 asyncMiddleware(getUserInformation)
37) 38)
38 39
39usersRouter.get('/me/videos/:videoId/rating', 40usersRouter.get('/me/videos/:videoId/rating',
40 authenticate, 41 authenticate,
41 usersVideoRatingValidator, 42 usersVideoRatingValidator,
42 getUserVideoRating 43 asyncMiddleware(getUserVideoRating)
43) 44)
44 45
45usersRouter.get('/', 46usersRouter.get('/',
@@ -47,7 +48,7 @@ usersRouter.get('/',
47 usersSortValidator, 48 usersSortValidator,
48 setUsersSort, 49 setUsersSort,
49 setPagination, 50 setPagination,
50 listUsers 51 asyncMiddleware(listUsers)
51) 52)
52 53
53usersRouter.get('/:id', 54usersRouter.get('/:id',
@@ -65,27 +66,27 @@ usersRouter.post('/',
65usersRouter.post('/register', 66usersRouter.post('/register',
66 ensureUserRegistrationAllowed, 67 ensureUserRegistrationAllowed,
67 usersRegisterValidator, 68 usersRegisterValidator,
68 registerUser 69 asyncMiddleware(registerUser)
69) 70)
70 71
71usersRouter.put('/me', 72usersRouter.put('/me',
72 authenticate, 73 authenticate,
73 usersUpdateMeValidator, 74 usersUpdateMeValidator,
74 updateMe 75 asyncMiddleware(updateMe)
75) 76)
76 77
77usersRouter.put('/:id', 78usersRouter.put('/:id',
78 authenticate, 79 authenticate,
79 ensureIsAdmin, 80 ensureIsAdmin,
80 usersUpdateValidator, 81 usersUpdateValidator,
81 updateUser 82 asyncMiddleware(updateUser)
82) 83)
83 84
84usersRouter.delete('/:id', 85usersRouter.delete('/:id',
85 authenticate, 86 authenticate,
86 ensureIsAdmin, 87 ensureIsAdmin,
87 usersRemoveValidator, 88 usersRemoveValidator,
88 removeUser 89 asyncMiddleware(removeUser)
89) 90)
90 91
91usersRouter.post('/token', token, success) 92usersRouter.post('/token', token, success)
@@ -99,21 +100,19 @@ export {
99 100
100// --------------------------------------------------------------------------- 101// ---------------------------------------------------------------------------
101 102
102function createUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { 103async function createUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
103 const options = { 104 const options = {
104 arguments: [ req, res ], 105 arguments: [ req, res ],
105 errorMessage: 'Cannot insert the user with many retries.' 106 errorMessage: 'Cannot insert the user with many retries.'
106 } 107 }
107 108
108 retryTransactionWrapper(createUser, options) 109 await retryTransactionWrapper(createUser, options)
109 .then(() => { 110
110 // TODO : include Location of the new user -> 201 111 // TODO : include Location of the new user -> 201
111 res.type('json').status(204).end() 112 return res.type('json').status(204).end()
112 })
113 .catch(err => next(err))
114} 113}
115 114
116function createUser (req: express.Request, res: express.Response, next: express.NextFunction) { 115async function createUser (req: express.Request, res: express.Response, next: express.NextFunction) {
117 const body: UserCreate = req.body 116 const body: UserCreate = req.body
118 const user = db.User.build({ 117 const user = db.User.build({
119 username: body.username, 118 username: body.username,
@@ -124,15 +123,12 @@ function createUser (req: express.Request, res: express.Response, next: express.
124 videoQuota: body.videoQuota 123 videoQuota: body.videoQuota
125 }) 124 })
126 125
127 return createUserAuthorAndChannel(user) 126 await createUserAuthorAndChannel(user)
128 .then(() => logger.info('User %s with its channel and author created.', body.username)) 127
129 .catch((err: Error) => { 128 logger.info('User %s with its channel and author created.', body.username)
130 logger.debug('Cannot insert the user.', err)
131 throw err
132 })
133} 129}
134 130
135function registerUser (req: express.Request, res: express.Response, next: express.NextFunction) { 131async function registerUser (req: express.Request, res: express.Response, next: express.NextFunction) {
136 const body: UserCreate = req.body 132 const body: UserCreate = req.body
137 133
138 const user = db.User.build({ 134 const user = db.User.build({
@@ -144,22 +140,21 @@ function registerUser (req: express.Request, res: express.Response, next: expres
144 videoQuota: CONFIG.USER.VIDEO_QUOTA 140 videoQuota: CONFIG.USER.VIDEO_QUOTA
145 }) 141 })
146 142
147 return createUserAuthorAndChannel(user) 143 await createUserAuthorAndChannel(user)
148 .then(() => res.type('json').status(204).end()) 144 return res.type('json').status(204).end()
149 .catch(err => next(err))
150} 145}
151 146
152function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) { 147async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {
153 db.User.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username) 148 const user = await db.User.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username)
154 .then(user => res.json(user.toFormattedJSON())) 149
155 .catch(err => next(err)) 150 return res.json(user.toFormattedJSON())
156} 151}
157 152
158function getUser (req: express.Request, res: express.Response, next: express.NextFunction) { 153function getUser (req: express.Request, res: express.Response, next: express.NextFunction) {
159 return res.json(res.locals.user.toFormattedJSON()) 154 return res.json(res.locals.user.toFormattedJSON())
160} 155}
161 156
162function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) { 157async function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
163 const videoId = +req.params.videoId 158 const videoId = +req.params.videoId
164 const userId = +res.locals.oauth.token.User.id 159 const userId = +res.locals.oauth.token.User.id
165 160
@@ -175,50 +170,45 @@ function getUserVideoRating (req: express.Request, res: express.Response, next:
175 .catch(err => next(err)) 170 .catch(err => next(err))
176} 171}
177 172
178function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) { 173async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) {
179 db.User.listForApi(req.query.start, req.query.count, req.query.sort) 174 const resultList = await db.User.listForApi(req.query.start, req.query.count, req.query.sort)
180 .then(resultList => { 175
181 res.json(getFormattedObjects(resultList.data, resultList.total)) 176 return res.json(getFormattedObjects(resultList.data, resultList.total))
182 })
183 .catch(err => next(err))
184} 177}
185 178
186function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) { 179async function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
187 db.User.loadById(req.params.id) 180 const user = await db.User.loadById(req.params.id)
188 .then(user => user.destroy()) 181
189 .then(() => res.sendStatus(204)) 182 await user.destroy()
190 .catch(err => { 183
191 logger.error('Errors when removed the user.', err) 184 return res.sendStatus(204)
192 return next(err)
193 })
194} 185}
195 186
196function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) { 187async function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) {
197 const body: UserUpdateMe = req.body 188 const body: UserUpdateMe = req.body
198 189
199 // FIXME: user is not already a Sequelize instance? 190 // FIXME: user is not already a Sequelize instance?
200 db.User.loadByUsername(res.locals.oauth.token.user.username) 191 const user = res.locals.oauth.token.user
201 .then(user => {
202 if (body.password !== undefined) user.password = body.password
203 if (body.email !== undefined) user.email = body.email
204 if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW
205 192
206 return user.save() 193 if (body.password !== undefined) user.password = body.password
207 }) 194 if (body.email !== undefined) user.email = body.email
208 .then(() => res.sendStatus(204)) 195 if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW
209 .catch(err => next(err)) 196
197 await user.save()
198
199 return await res.sendStatus(204)
210} 200}
211 201
212function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) { 202async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
213 const body: UserUpdate = req.body 203 const body: UserUpdate = req.body
214 const user: UserInstance = res.locals.user 204 const user: UserInstance = res.locals.user
215 205
216 if (body.email !== undefined) user.email = body.email 206 if (body.email !== undefined) user.email = body.email
217 if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota 207 if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota
218 208
219 return user.save() 209 await user.save()
220 .then(() => res.sendStatus(204)) 210
221 .catch(err => next(err)) 211 return res.sendStatus(204)
222} 212}
223 213
224function success (req: express.Request, res: express.Response, next: express.NextFunction) { 214function success (req: express.Request, res: express.Response, next: express.NextFunction) {