aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/users.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-06-13 14:27:40 +0200
committerChocobozzz <me@florianbigard.com>2018-06-13 14:27:40 +0200
commit90d4bb8125e80c8060416d4d135ddeaf0a622ede (patch)
treeb3b7181329a08ecc930b54fe7b48095c4155393c /server/controllers/api/users.ts
parent3cd0734fd9b0ff21aaef02317a874e8f1c06e027 (diff)
downloadPeerTube-90d4bb8125e80c8060416d4d135ddeaf0a622ede.tar.gz
PeerTube-90d4bb8125e80c8060416d4d135ddeaf0a622ede.tar.zst
PeerTube-90d4bb8125e80c8060416d4d135ddeaf0a622ede.zip
Refractor retry transaction function
Diffstat (limited to 'server/controllers/api/users.ts')
-rw-r--r--server/controllers/api/users.ts52
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'
4import * as uuidv4 from 'uuid/v4' 4import * as uuidv4 from 'uuid/v4'
5import * as RateLimit from 'express-rate-limit' 5import * as RateLimit from 'express-rate-limit'
6import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared' 6import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared'
7import { retryTransactionWrapper } from '../../helpers/database-utils'
8import { processImage } from '../../helpers/image-utils' 7import { processImage } from '../../helpers/image-utils'
9import { logger } from '../../helpers/logger' 8import { logger } from '../../helpers/logger'
10import { getFormattedObjects } from '../../helpers/utils' 9import { getFormattedObjects } from '../../helpers/utils'
@@ -16,6 +15,7 @@ import { Redis } from '../../lib/redis'
16import { createUserAccountAndChannel } from '../../lib/user' 15import { createUserAccountAndChannel } from '../../lib/user'
17import { 16import {
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
108usersRouter.post('/register', 108usersRouter.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
115usersRouter.put('/me', 115usersRouter.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
181async function createUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { 181async 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
200async 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,
219async 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
230async function registerUser (req: express.Request) { 208async 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
248async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) { 228async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {