import * as sharp from 'sharp'
import * as uuidv4 from 'uuid/v4'
import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared'
-import { renamePromise, unlinkPromise } from '../../helpers/core-utils'
+import { unlinkPromise } from '../../helpers/core-utils'
import { retryTransactionWrapper } from '../../helpers/database-utils'
import { logger } from '../../helpers/logger'
import { createReqFiles, getFormattedObjects } from '../../helpers/utils'
import { AVATAR_MIMETYPE_EXT, AVATARS_SIZE, CONFIG, sequelizeTypescript } from '../../initializers'
+import { updateActorAvatarInstance } from '../../lib/activitypub'
+import { sendUpdateUser } from '../../lib/activitypub/send'
import { createUserAccountAndChannel } from '../../lib/user'
import {
- asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setPagination, setUsersSort,
- setVideosSort, token, usersAddValidator, usersGetValidator, usersRegisterValidator, usersRemoveValidator, usersSortValidator,
+ asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setDefaultSort,
+ setPagination, token, usersAddValidator, usersGetValidator, usersRegisterValidator, usersRemoveValidator, usersSortValidator,
usersUpdateMeValidator, usersUpdateValidator, usersVideoRatingValidator
} from '../../middlewares'
import { usersUpdateMyAvatarValidator, videosSortValidator } from '../../middlewares/validators'
import { AccountVideoRateModel } from '../../models/account/account-video-rate'
import { UserModel } from '../../models/account/user'
-import { AvatarModel } from '../../models/avatar/avatar'
import { VideoModel } from '../../models/video/video'
const reqAvatarFile = createReqFiles('avatarfile', CONFIG.STORAGE.AVATARS_DIR, AVATAR_MIMETYPE_EXT)
asyncMiddleware(getUserInformation)
)
+usersRouter.get('/me/video-quota-used',
+ authenticate,
+ asyncMiddleware(getUserVideoQuotaUsed)
+)
+
usersRouter.get('/me/videos',
authenticate,
paginationValidator,
videosSortValidator,
- setVideosSort,
+ setDefaultSort,
setPagination,
asyncMiddleware(getUserVideos)
)
ensureUserHasRight(UserRight.MANAGE_USERS),
paginationValidator,
usersSortValidator,
- setUsersSort,
+ setDefaultSort,
setPagination,
asyncMiddleware(listUsers)
)
return res.json(user.toFormattedJSON())
}
+async function getUserVideoQuotaUsed (req: express.Request, res: express.Response, next: express.NextFunction) {
+ // We did not load channels in res.locals.user
+ const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username)
+ const videoQuotaUsed = await UserModel.getOriginalVideoFileTotalFromUser(user)
+
+ return res.json({
+ videoQuotaUsed
+ })
+}
+
function getUser (req: express.Request, res: express.Response, next: express.NextFunction) {
- return res.json(res.locals.user.toFormattedJSON())
+ return res.json((res.locals.user as UserModel).toFormattedJSON())
}
async function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
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.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo
await user.save()
+ await sendUpdateUser(user, undefined)
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 user = res.locals.oauth.token.user
+ const actor = user.Account.Actor
const avatarDir = CONFIG.STORAGE.AVATARS_DIR
const source = join(avatarDir, avatarPhysicalFile.filename)
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 })
- }
+ const avatar = await sequelizeTypescript.transaction(async t => {
+ const updatedActor = await updateActorAvatarInstance(actor, avatarName, t)
+ await updatedActor.save({ transaction: t })
- actor.set('avatarId', avatar.id)
- await actor.save({ transaction: t })
+ await sendUpdateUser(user, t)
- return { actor, avatar }
+ return updatedActor.Avatar
})
return res
await user.save()
+ // Don't need to send this update to followers, these attributes are not propagated
+
return res.sendStatus(204)
}