-function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
- db.User.loadById(req.params.id)
- .then(user => user.destroy())
- .then(() => res.sendStatus(204))
- .catch(err => {
- logger.error('Errors when removed the user.', err)
- return next(err)
+async function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) {
+ const body: UserUpdateMe = req.body
+
+ // FIXME: user is not already a Sequelize instance?
+ const user = res.locals.oauth.token.user
+
+ if (body.password !== undefined) user.password = body.password
+ if (body.email !== undefined) user.email = body.email
+ if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW
+ if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo
+
+ await user.save()
+
+ return res.sendStatus(204)
+}
+
+async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) {
+ const avatarPhysicalFile = req.files['avatarfile'][0]
+ const actor = res.locals.oauth.token.user.Account.Actor
+
+ const avatarDir = CONFIG.STORAGE.AVATARS_DIR
+ const source = join(avatarDir, avatarPhysicalFile.filename)
+ const extension = extname(avatarPhysicalFile.filename)
+ const avatarName = uuidv4() + extension
+ const destination = join(avatarDir, avatarName)
+
+ await sharp(source)
+ .resize(AVATARS_SIZE.width, AVATARS_SIZE.height)
+ .toFile(destination)
+
+ await unlinkPromise(source)
+
+ const { avatar } = await sequelizeTypescript.transaction(async t => {
+ const avatar = await AvatarModel.create({
+ filename: avatarName
+ }, { transaction: t })
+
+ if (actor.Avatar) {
+ await actor.Avatar.destroy({ transaction: t })
+ }
+
+ actor.set('avatarId', avatar.id)
+ await actor.save({ transaction: t })
+
+ return { actor, avatar }
+ })
+
+ return res
+ .json({
+ avatar: avatar.toFormattedJSON()