diff options
Diffstat (limited to 'server/controllers/api/users.ts')
-rw-r--r-- | server/controllers/api/users.ts | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts index 1ecaaf93f..6576e4333 100644 --- a/server/controllers/api/users.ts +++ b/server/controllers/api/users.ts | |||
@@ -2,7 +2,7 @@ import * as express from 'express' | |||
2 | 2 | ||
3 | import { database as db } from '../../initializers/database' | 3 | import { database as db } from '../../initializers/database' |
4 | import { USER_ROLES, CONFIG } from '../../initializers' | 4 | import { USER_ROLES, CONFIG } from '../../initializers' |
5 | import { logger, getFormattedObjects } from '../../helpers' | 5 | import { logger, getFormattedObjects, retryTransactionWrapper } from '../../helpers' |
6 | import { | 6 | import { |
7 | authenticate, | 7 | authenticate, |
8 | ensureIsAdmin, | 8 | ensureIsAdmin, |
@@ -26,6 +26,7 @@ import { | |||
26 | UserUpdate, | 26 | UserUpdate, |
27 | UserUpdateMe | 27 | UserUpdateMe |
28 | } from '../../../shared' | 28 | } from '../../../shared' |
29 | import { createUserAuthorAndChannel } from '../../lib' | ||
29 | import { UserInstance } from '../../models' | 30 | import { UserInstance } from '../../models' |
30 | 31 | ||
31 | const usersRouter = express.Router() | 32 | const usersRouter = express.Router() |
@@ -58,7 +59,7 @@ usersRouter.post('/', | |||
58 | authenticate, | 59 | authenticate, |
59 | ensureIsAdmin, | 60 | ensureIsAdmin, |
60 | usersAddValidator, | 61 | usersAddValidator, |
61 | createUser | 62 | createUserRetryWrapper |
62 | ) | 63 | ) |
63 | 64 | ||
64 | usersRouter.post('/register', | 65 | usersRouter.post('/register', |
@@ -98,9 +99,22 @@ export { | |||
98 | 99 | ||
99 | // --------------------------------------------------------------------------- | 100 | // --------------------------------------------------------------------------- |
100 | 101 | ||
102 | function createUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
103 | const options = { | ||
104 | arguments: [ req, res ], | ||
105 | errorMessage: 'Cannot insert the user with many retries.' | ||
106 | } | ||
107 | |||
108 | retryTransactionWrapper(createUser, options) | ||
109 | .then(() => { | ||
110 | // TODO : include Location of the new user -> 201 | ||
111 | res.type('json').status(204).end() | ||
112 | }) | ||
113 | .catch(err => next(err)) | ||
114 | } | ||
115 | |||
101 | function createUser (req: express.Request, res: express.Response, next: express.NextFunction) { | 116 | function createUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
102 | const body: UserCreate = req.body | 117 | const body: UserCreate = req.body |
103 | |||
104 | const user = db.User.build({ | 118 | const user = db.User.build({ |
105 | username: body.username, | 119 | username: body.username, |
106 | password: body.password, | 120 | password: body.password, |
@@ -110,9 +124,12 @@ function createUser (req: express.Request, res: express.Response, next: express. | |||
110 | videoQuota: body.videoQuota | 124 | videoQuota: body.videoQuota |
111 | }) | 125 | }) |
112 | 126 | ||
113 | user.save() | 127 | return createUserAuthorAndChannel(user) |
114 | .then(() => res.type('json').status(204).end()) | 128 | .then(() => logger.info('User %s with its channel and author created.', body.username)) |
115 | .catch(err => next(err)) | 129 | .catch((err: Error) => { |
130 | logger.debug('Cannot insert the user.', err) | ||
131 | throw err | ||
132 | }) | ||
116 | } | 133 | } |
117 | 134 | ||
118 | function registerUser (req: express.Request, res: express.Response, next: express.NextFunction) { | 135 | function registerUser (req: express.Request, res: express.Response, next: express.NextFunction) { |
@@ -127,13 +144,13 @@ function registerUser (req: express.Request, res: express.Response, next: expres | |||
127 | videoQuota: CONFIG.USER.VIDEO_QUOTA | 144 | videoQuota: CONFIG.USER.VIDEO_QUOTA |
128 | }) | 145 | }) |
129 | 146 | ||
130 | user.save() | 147 | return createUserAuthorAndChannel(user) |
131 | .then(() => res.type('json').status(204).end()) | 148 | .then(() => res.type('json').status(204).end()) |
132 | .catch(err => next(err)) | 149 | .catch(err => next(err)) |
133 | } | 150 | } |
134 | 151 | ||
135 | function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) { | 152 | function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) { |
136 | db.User.loadByUsername(res.locals.oauth.token.user.username) | 153 | db.User.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username) |
137 | .then(user => res.json(user.toFormattedJSON())) | 154 | .then(user => res.json(user.toFormattedJSON())) |
138 | .catch(err => next(err)) | 155 | .catch(err => next(err)) |
139 | } | 156 | } |