aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/users.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2017-12-29 19:10:13 +0100
committerChocobozzz <me@florianbigard.com>2017-12-29 19:10:13 +0100
commitc5911fd347c76e8bdc05ea9f3ee9efed4a58c236 (patch)
treeb8d287daca6c45305090cbec9da97d1155f275bd /server/controllers/api/users.ts
parent8b0d42ee372de6589796be26b83e5bffb1b69cdf (diff)
downloadPeerTube-c5911fd347c76e8bdc05ea9f3ee9efed4a58c236.tar.gz
PeerTube-c5911fd347c76e8bdc05ea9f3ee9efed4a58c236.tar.zst
PeerTube-c5911fd347c76e8bdc05ea9f3ee9efed4a58c236.zip
Begin to add avatar to actors
Diffstat (limited to 'server/controllers/api/users.ts')
-rw-r--r--server/controllers/api/users.ts53
1 files changed, 50 insertions, 3 deletions
diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts
index 75393ad17..57b98b84a 100644
--- a/server/controllers/api/users.ts
+++ b/server/controllers/api/users.ts
@@ -1,20 +1,26 @@
1import * as express from 'express' 1import * as express from 'express'
2import { extname, join } from 'path'
3import * as uuidv4 from 'uuid/v4'
2import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared' 4import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared'
5import { renamePromise } from '../../helpers/core-utils'
3import { retryTransactionWrapper } from '../../helpers/database-utils' 6import { retryTransactionWrapper } from '../../helpers/database-utils'
4import { logger } from '../../helpers/logger' 7import { logger } from '../../helpers/logger'
5import { getFormattedObjects } from '../../helpers/utils' 8import { createReqFiles, getFormattedObjects } from '../../helpers/utils'
6import { CONFIG } from '../../initializers' 9import { AVATAR_MIMETYPE_EXT, CONFIG, sequelizeTypescript } from '../../initializers'
7import { createUserAccountAndChannel } from '../../lib/user' 10import { createUserAccountAndChannel } from '../../lib/user'
8import { 11import {
9 asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setPagination, setUsersSort, 12 asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setPagination, setUsersSort,
10 setVideosSort, token, usersAddValidator, usersGetValidator, usersRegisterValidator, usersRemoveValidator, usersSortValidator, 13 setVideosSort, token, usersAddValidator, usersGetValidator, usersRegisterValidator, usersRemoveValidator, usersSortValidator,
11 usersUpdateMeValidator, usersUpdateValidator, usersVideoRatingValidator 14 usersUpdateMeValidator, usersUpdateValidator, usersVideoRatingValidator
12} from '../../middlewares' 15} from '../../middlewares'
13import { videosSortValidator } from '../../middlewares/validators' 16import { usersUpdateMyAvatarValidator, videosSortValidator } from '../../middlewares/validators'
14import { AccountVideoRateModel } from '../../models/account/account-video-rate' 17import { AccountVideoRateModel } from '../../models/account/account-video-rate'
15import { UserModel } from '../../models/account/user' 18import { UserModel } from '../../models/account/user'
19import { AvatarModel } from '../../models/avatar/avatar'
16import { VideoModel } from '../../models/video/video' 20import { VideoModel } from '../../models/video/video'
17 21
22const reqAvatarFile = createReqFiles('avatarfile', CONFIG.STORAGE.AVATARS_DIR, AVATAR_MIMETYPE_EXT)
23
18const usersRouter = express.Router() 24const usersRouter = express.Router()
19 25
20usersRouter.get('/me', 26usersRouter.get('/me',
@@ -71,6 +77,13 @@ usersRouter.put('/me',
71 asyncMiddleware(updateMe) 77 asyncMiddleware(updateMe)
72) 78)
73 79
80usersRouter.post('/me/avatar/pick',
81 authenticate,
82 reqAvatarFile,
83 usersUpdateMyAvatarValidator,
84 asyncMiddleware(updateMyAvatar)
85)
86
74usersRouter.put('/:id', 87usersRouter.put('/:id',
75 authenticate, 88 authenticate,
76 ensureUserHasRight(UserRight.MANAGE_USERS), 89 ensureUserHasRight(UserRight.MANAGE_USERS),
@@ -216,6 +229,40 @@ async function updateMe (req: express.Request, res: express.Response, next: expr
216 return res.sendStatus(204) 229 return res.sendStatus(204)
217} 230}
218 231
232async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) {
233 const avatarPhysicalFile = req.files['avatarfile'][0]
234 const actor = res.locals.oauth.token.user.Account.Actor
235
236 const avatarDir = CONFIG.STORAGE.AVATARS_DIR
237 const source = join(avatarDir, avatarPhysicalFile.filename)
238 const extension = extname(avatarPhysicalFile.filename)
239 const avatarName = uuidv4() + extension
240 const destination = join(avatarDir, avatarName)
241
242 await renamePromise(source, destination)
243
244 const { avatar } = await sequelizeTypescript.transaction(async t => {
245 const avatar = await AvatarModel.create({
246 filename: avatarName
247 }, { transaction: t })
248
249 if (actor.Avatar) {
250 await actor.Avatar.destroy({ transaction: t })
251 }
252
253 actor.set('avatarId', avatar.id)
254 await actor.save({ transaction: t })
255
256 return { actor, avatar }
257 })
258
259 return res
260 .json({
261 avatar: avatar.toFormattedJSON()
262 })
263 .end()
264}
265
219async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) { 266async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
220 const body: UserUpdate = req.body 267 const body: UserUpdate = req.body
221 const user = res.locals.user as UserModel 268 const user = res.locals.user as UserModel