X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fusers%2Fme.ts;h=5a3e9e51a3a63c5179370d42333c1888646e6307;hb=968aaed2066873fc1c39f95168284122d9d15e21;hp=914c52e27add2ca0033f37a962001711abaafba9;hpb=67ed6552b831df66713bac9e672738796128d33f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/users/me.ts b/server/controllers/api/users/me.ts index 914c52e27..5a3e9e51a 100644 --- a/server/controllers/api/users/me.ts +++ b/server/controllers/api/users/me.ts @@ -1,6 +1,9 @@ import 'multer' import * as express from 'express' -import { UserUpdateMe, UserVideoRate as FormattedUserVideoRate, VideoSortField } from '../../../../shared' +import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '@server/helpers/audit-logger' +import { Hooks } from '@server/lib/plugins/hooks' +import { UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../../shared' +import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' import { UserVideoQuota } from '../../../../shared/models/users/user-video-quota.model' import { createReqFiles } from '../../../helpers/express-utils' import { getFormattedObjects } from '../../../helpers/utils' @@ -8,8 +11,8 @@ import { CONFIG } from '../../../initializers/config' import { MIMETYPES } from '../../../initializers/constants' import { sequelizeTypescript } from '../../../initializers/database' import { sendUpdateActor } from '../../../lib/activitypub/send' -import { updateActorAvatarFile } from '../../../lib/avatar' -import { sendVerifyUserEmail } from '../../../lib/user' +import { deleteLocalActorAvatarFile, updateLocalActorAvatarFile } from '../../../lib/avatar' +import { getOriginalVideoFileTotalDailyFromUser, getOriginalVideoFileTotalFromUser, sendVerifyUserEmail } from '../../../lib/user' import { asyncMiddleware, asyncRetryTransactionMiddleware, @@ -17,6 +20,7 @@ import { paginationValidator, setDefaultPagination, setDefaultSort, + setDefaultVideosSort, usersUpdateMeValidator, usersVideoRatingValidator } from '../../../middlewares' @@ -28,6 +32,8 @@ import { UserModel } from '../../../models/account/user' import { VideoModel } from '../../../models/video/video' import { VideoImportModel } from '../../../models/video/video-import' +const auditLogger = auditLoggerFactory('users') + const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR }) const meRouter = express.Router() @@ -60,7 +66,7 @@ meRouter.get('/me/videos', authenticate, paginationValidator, videosSortValidator, - setDefaultSort, + setDefaultVideosSort, setDefaultPagination, asyncMiddleware(getUserVideos) ) @@ -84,6 +90,11 @@ meRouter.post('/me/avatar/pick', asyncRetryTransactionMiddleware(updateMyAvatar) ) +meRouter.delete('/me/avatar', + authenticate, + asyncRetryTransactionMiddleware(deleteMyAvatar) +) + // --------------------------------------------------------------------------- export { @@ -94,12 +105,19 @@ export { async function getUserVideos (req: express.Request, res: express.Response) { const user = res.locals.oauth.token.User - const resultList = await VideoModel.listUserVideosForApi( - user.Account.id, - req.query.start as number, - req.query.count as number, - req.query.sort as VideoSortField, - req.query.search as string + + const apiOptions = await Hooks.wrapObject({ + accountId: user.Account.id, + start: req.query.start, + count: req.query.count, + sort: req.query.sort, + search: req.query.search + }, 'filter:api.user.me.videos.list.params') + + const resultList = await Hooks.wrapPromiseFun( + VideoModel.listUserVideosForApi, + apiOptions, + 'filter:api.user.me.videos.list.result' ) const additionalAttributes = { @@ -125,15 +143,15 @@ async function getUserVideoImports (req: express.Request, res: express.Response) async function getUserInformation (req: express.Request, res: express.Response) { // We did not load channels in res.locals.user - const user = await UserModel.loadForMeAPI(res.locals.oauth.token.user.username) + const user = await UserModel.loadForMeAPI(res.locals.oauth.token.user.id) return res.json(user.toMeFormattedJSON()) } async function getUserVideoQuotaUsed (req: express.Request, res: express.Response) { const user = res.locals.oauth.token.user - const videoQuotaUsed = await UserModel.getOriginalVideoFileTotalFromUser(user) - const videoQuotaUsedDaily = await UserModel.getOriginalVideoFileTotalDailyFromUser(user) + const videoQuotaUsed = await getOriginalVideoFileTotalFromUser(user) + const videoQuotaUsedDaily = await getOriginalVideoFileTotalDailyFromUser(user) const data: UserVideoQuota = { videoQuotaUsed, @@ -157,11 +175,13 @@ async function getUserVideoRating (req: express.Request, res: express.Response) } async function deleteMe (req: express.Request, res: express.Response) { - const user = res.locals.oauth.token.User + const user = await UserModel.loadByIdWithChannels(res.locals.oauth.token.User.id) + + auditLogger.delete(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON())) await user.destroy() - return res.sendStatus(204) + return res.sendStatus(HttpStatusCode.NO_CONTENT_204) } async function updateMe (req: express.Request, res: express.Response) { @@ -209,7 +229,7 @@ async function updateMe (req: express.Request, res: express.Response) { await sendVerifyUserEmail(user, true) } - return res.sendStatus(204) + return res.sendStatus(HttpStatusCode.NO_CONTENT_204) } async function updateMyAvatar (req: express.Request, res: express.Response) { @@ -218,7 +238,16 @@ async function updateMyAvatar (req: express.Request, res: express.Response) { const userAccount = await AccountModel.load(user.Account.id) - const avatar = await updateActorAvatarFile(avatarPhysicalFile, userAccount) + const avatar = await updateLocalActorAvatarFile(userAccount, avatarPhysicalFile) return res.json({ avatar: avatar.toFormattedJSON() }) } + +async function deleteMyAvatar (req: express.Request, res: express.Response) { + const user = res.locals.oauth.token.user + + const userAccount = await AccountModel.load(user.Account.id) + await deleteLocalActorAvatarFile(userAccount) + + return res.sendStatus(HttpStatusCode.NO_CONTENT_204) +}