X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fvideos%2Fabuse.ts;h=d0c81804bfe538036938e69bb4432880704554da;hb=6e46de095d7169355dd83030f6ce4a582304153a;hp=29f901f605bb90d3b83d8a131bd790afc2fa71e9;hpb=608624252466acf9f1d9ee1c1170bd4fe4d18d18;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/videos/abuse.ts b/server/controllers/api/videos/abuse.ts index 29f901f60..d0c81804b 100644 --- a/server/controllers/api/videos/abuse.ts +++ b/server/controllers/api/videos/abuse.ts @@ -1,24 +1,29 @@ import * as express from 'express' - -import { database as db } from '../../../initializers/database' -import { - logger, - getFormattedObjects, - retryTransactionWrapper -} from '../../../helpers' +import { UserRight, VideoAbuseCreate, VideoAbuseState } from '../../../../shared' +import { logger } from '../../../helpers/logger' +import { getFormattedObjects } from '../../../helpers/utils' +import { sequelizeTypescript } from '../../../initializers' +import { sendVideoAbuse } from '../../../lib/activitypub/send' import { + asyncMiddleware, + asyncRetryTransactionMiddleware, authenticate, ensureUserHasRight, paginationValidator, + setDefaultPagination, + setDefaultSort, + videoAbuseGetValidator, videoAbuseReportValidator, videoAbusesSortValidator, - setVideoAbusesSort, - setPagination, - asyncMiddleware + videoAbuseUpdateValidator } from '../../../middlewares' -import { VideoInstance } from '../../../models' -import { VideoAbuseCreate, UserRight } from '../../../../shared' +import { AccountModel } from '../../../models/account/account' +import { VideoModel } from '../../../models/video/video' +import { VideoAbuseModel } from '../../../models/video/video-abuse' +import { auditLoggerFactory, VideoAbuseAuditView } from '../../../helpers/audit-logger' +import { UserModel } from '../../../models/account/user' +const auditLogger = auditLoggerFactory('abuse') const abuseVideoRouter = express.Router() abuseVideoRouter.get('/abuse', @@ -26,14 +31,26 @@ abuseVideoRouter.get('/abuse', ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES), paginationValidator, videoAbusesSortValidator, - setVideoAbusesSort, - setPagination, + setDefaultSort, + setDefaultPagination, asyncMiddleware(listVideoAbuses) ) -abuseVideoRouter.post('/:id/abuse', +abuseVideoRouter.put('/:videoId/abuse/:id', authenticate, - videoAbuseReportValidator, - asyncMiddleware(reportVideoAbuseRetryWrapper) + ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES), + asyncMiddleware(videoAbuseUpdateValidator), + asyncRetryTransactionMiddleware(updateVideoAbuse) +) +abuseVideoRouter.post('/:videoId/abuse', + authenticate, + asyncMiddleware(videoAbuseReportValidator), + asyncRetryTransactionMiddleware(reportVideoAbuse) +) +abuseVideoRouter.delete('/:videoId/abuse/:id', + authenticate, + ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES), + asyncMiddleware(videoAbuseGetValidator), + asyncRetryTransactionMiddleware(deleteVideoAbuse) ) // --------------------------------------------------------------------------- @@ -44,49 +61,68 @@ export { // --------------------------------------------------------------------------- -async function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) { - const resultList = await db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort) +async function listVideoAbuses (req: express.Request, res: express.Response) { + const resultList = await VideoAbuseModel.listForApi(req.query.start, req.query.count, req.query.sort) return res.json(getFormattedObjects(resultList.data, resultList.total)) } -async function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { - const options = { - arguments: [ req, res ], - errorMessage: 'Cannot report abuse to the video with many retries.' - } +async function updateVideoAbuse (req: express.Request, res: express.Response) { + const videoAbuse: VideoAbuseModel = res.locals.videoAbuse + + if (req.body.moderationComment !== undefined) videoAbuse.moderationComment = req.body.moderationComment + if (req.body.state !== undefined) videoAbuse.state = req.body.state - await retryTransactionWrapper(reportVideoAbuse, options) + await sequelizeTypescript.transaction(t => { + return videoAbuse.save({ transaction: t }) + }) + + // Do not send the delete to other instances, we updated OUR copy of this video abuse + + return res.type('json').status(204).end() +} + +async function deleteVideoAbuse (req: express.Request, res: express.Response) { + const videoAbuse: VideoAbuseModel = res.locals.videoAbuse + + await sequelizeTypescript.transaction(t => { + return videoAbuse.destroy({ transaction: t }) + }) + + // Do not send the delete to other instances, we delete OUR copy of this video abuse return res.type('json').status(204).end() } async function reportVideoAbuse (req: express.Request, res: express.Response) { - const videoInstance = res.locals.video as VideoInstance - const reporterUsername = res.locals.oauth.token.User.username + const videoInstance = res.locals.video as VideoModel const body: VideoAbuseCreate = req.body - const abuseToCreate = { - reporterUsername, - reason: body.reason, - videoId: videoInstance.id, - reporterServerId: null // This is our server that reported this abuse - } + const videoAbuse: VideoAbuseModel = await sequelizeTypescript.transaction(async t => { + const reporterAccount = await AccountModel.load((res.locals.oauth.token.User as UserModel).Account.id, t) - await db.sequelize.transaction(async t => { - const abuse = await db.VideoAbuse.create(abuseToCreate, { transaction: t }) - // We send the information to the destination server + const abuseToCreate = { + reporterAccountId: reporterAccount.id, + reason: body.reason, + videoId: videoInstance.id, + state: VideoAbuseState.PENDING + } + + const videoAbuseInstance = await VideoAbuseModel.create(abuseToCreate, { transaction: t }) + videoAbuseInstance.Video = videoInstance + videoAbuseInstance.Account = reporterAccount + + // We send the video abuse to the origin server if (videoInstance.isOwned() === false) { - const reportData = { - reporterUsername, - reportReason: abuse.reason, - videoUUID: videoInstance.uuid - } - - // await friends.reportAbuseVideoToFriend(reportData, videoInstance, t) - // TODO: send abuse to origin server + await sendVideoAbuse(reporterAccount.Actor, videoAbuseInstance, videoInstance) } + + auditLogger.create(reporterAccount.Actor.getIdentifier(), new VideoAbuseAuditView(videoAbuseInstance.toFormattedJSON())) + + return videoAbuseInstance }) logger.info('Abuse report for video %s created.', videoInstance.name) + + return res.json({ videoAbuse: videoAbuse.toFormattedJSON() }).end() }