diff options
Diffstat (limited to 'server/controllers/api/users.ts')
-rw-r--r-- | server/controllers/api/users.ts | 52 |
1 files changed, 16 insertions, 36 deletions
diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts index 2b40c44d9..0aeb77964 100644 --- a/server/controllers/api/users.ts +++ b/server/controllers/api/users.ts | |||
@@ -4,7 +4,6 @@ import { extname, join } from 'path' | |||
4 | import * as uuidv4 from 'uuid/v4' | 4 | import * as uuidv4 from 'uuid/v4' |
5 | import * as RateLimit from 'express-rate-limit' | 5 | import * as RateLimit from 'express-rate-limit' |
6 | import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared' | 6 | import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared' |
7 | import { retryTransactionWrapper } from '../../helpers/database-utils' | ||
8 | import { processImage } from '../../helpers/image-utils' | 7 | import { processImage } from '../../helpers/image-utils' |
9 | import { logger } from '../../helpers/logger' | 8 | import { logger } from '../../helpers/logger' |
10 | import { getFormattedObjects } from '../../helpers/utils' | 9 | import { getFormattedObjects } from '../../helpers/utils' |
@@ -16,6 +15,7 @@ import { Redis } from '../../lib/redis' | |||
16 | import { createUserAccountAndChannel } from '../../lib/user' | 15 | import { createUserAccountAndChannel } from '../../lib/user' |
17 | import { | 16 | import { |
18 | asyncMiddleware, | 17 | asyncMiddleware, |
18 | asyncRetryTransactionMiddleware, | ||
19 | authenticate, | 19 | authenticate, |
20 | ensureUserHasRight, | 20 | ensureUserHasRight, |
21 | ensureUserRegistrationAllowed, | 21 | ensureUserRegistrationAllowed, |
@@ -102,14 +102,14 @@ usersRouter.post('/', | |||
102 | authenticate, | 102 | authenticate, |
103 | ensureUserHasRight(UserRight.MANAGE_USERS), | 103 | ensureUserHasRight(UserRight.MANAGE_USERS), |
104 | asyncMiddleware(usersAddValidator), | 104 | asyncMiddleware(usersAddValidator), |
105 | asyncMiddleware(createUserRetryWrapper) | 105 | asyncRetryTransactionMiddleware(createUser) |
106 | ) | 106 | ) |
107 | 107 | ||
108 | usersRouter.post('/register', | 108 | usersRouter.post('/register', |
109 | asyncMiddleware(ensureUserRegistrationAllowed), | 109 | asyncMiddleware(ensureUserRegistrationAllowed), |
110 | ensureUserRegistrationAllowedForIP, | 110 | ensureUserRegistrationAllowedForIP, |
111 | asyncMiddleware(usersRegisterValidator), | 111 | asyncMiddleware(usersRegisterValidator), |
112 | asyncMiddleware(registerUserRetryWrapper) | 112 | asyncRetryTransactionMiddleware(registerUser) |
113 | ) | 113 | ) |
114 | 114 | ||
115 | usersRouter.put('/me', | 115 | usersRouter.put('/me', |
@@ -178,26 +178,7 @@ async function getUserVideos (req: express.Request, res: express.Response, next: | |||
178 | return res.json(getFormattedObjects(resultList.data, resultList.total, { additionalAttributes })) | 178 | return res.json(getFormattedObjects(resultList.data, resultList.total, { additionalAttributes })) |
179 | } | 179 | } |
180 | 180 | ||
181 | async function createUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | 181 | async function createUser (req: express.Request, res: express.Response) { |
182 | const options = { | ||
183 | arguments: [ req ], | ||
184 | errorMessage: 'Cannot insert the user with many retries.' | ||
185 | } | ||
186 | |||
187 | const { user, account } = await retryTransactionWrapper(createUser, options) | ||
188 | |||
189 | return res.json({ | ||
190 | user: { | ||
191 | id: user.id, | ||
192 | account: { | ||
193 | id: account.id, | ||
194 | uuid: account.Actor.uuid | ||
195 | } | ||
196 | } | ||
197 | }).end() | ||
198 | } | ||
199 | |||
200 | async function createUser (req: express.Request) { | ||
201 | const body: UserCreate = req.body | 182 | const body: UserCreate = req.body |
202 | const userToCreate = new UserModel({ | 183 | const userToCreate = new UserModel({ |
203 | username: body.username, | 184 | username: body.username, |
@@ -213,21 +194,18 @@ async function createUser (req: express.Request) { | |||
213 | 194 | ||
214 | logger.info('User %s with its channel and account created.', body.username) | 195 | logger.info('User %s with its channel and account created.', body.username) |
215 | 196 | ||
216 | return { user, account } | 197 | return res.json({ |
217 | } | 198 | user: { |
218 | 199 | id: user.id, | |
219 | async function registerUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | 200 | account: { |
220 | const options = { | 201 | id: account.id, |
221 | arguments: [ req ], | 202 | uuid: account.Actor.uuid |
222 | errorMessage: 'Cannot insert the user with many retries.' | 203 | } |
223 | } | 204 | } |
224 | 205 | }).end() | |
225 | await retryTransactionWrapper(registerUser, options) | ||
226 | |||
227 | return res.type('json').status(204).end() | ||
228 | } | 206 | } |
229 | 207 | ||
230 | async function registerUser (req: express.Request) { | 208 | async function registerUser (req: express.Request, res: express.Response) { |
231 | const body: UserCreate = req.body | 209 | const body: UserCreate = req.body |
232 | 210 | ||
233 | const user = new UserModel({ | 211 | const user = new UserModel({ |
@@ -243,6 +221,8 @@ async function registerUser (req: express.Request) { | |||
243 | await createUserAccountAndChannel(user) | 221 | await createUserAccountAndChannel(user) |
244 | 222 | ||
245 | logger.info('User %s with its channel and account registered.', body.username) | 223 | logger.info('User %s with its channel and account registered.', body.username) |
224 | |||
225 | return res.type('json').status(204).end() | ||
246 | } | 226 | } |
247 | 227 | ||
248 | async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) { | 228 | async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) { |