From 268eebed921ac13a9ce0f4717f4923aa24190657 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 10 Aug 2018 16:54:01 +0200 Subject: Add state and moderationComment for abuses on server side --- server/controllers/api/videos/abuse.ts | 62 +++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 8 deletions(-) (limited to 'server/controllers/api') diff --git a/server/controllers/api/videos/abuse.ts b/server/controllers/api/videos/abuse.ts index 7782fc639..59bdf6257 100644 --- a/server/controllers/api/videos/abuse.ts +++ b/server/controllers/api/videos/abuse.ts @@ -1,5 +1,5 @@ import * as express from 'express' -import { UserRight, VideoAbuseCreate } from '../../../../shared' +import { UserRight, VideoAbuseCreate, VideoAbuseState } from '../../../../shared' import { logger } from '../../../helpers/logger' import { getFormattedObjects } from '../../../helpers/utils' import { sequelizeTypescript } from '../../../initializers' @@ -12,8 +12,10 @@ import { paginationValidator, setDefaultPagination, setDefaultSort, + videoAbuseGetValidator, videoAbuseReportValidator, - videoAbusesSortValidator + videoAbusesSortValidator, + videoAbuseUpdateValidator } from '../../../middlewares' import { AccountModel } from '../../../models/account/account' import { VideoModel } from '../../../models/video/video' @@ -32,11 +34,23 @@ abuseVideoRouter.get('/abuse', setDefaultPagination, asyncMiddleware(listVideoAbuses) ) -abuseVideoRouter.post('/:id/abuse', +abuseVideoRouter.put('/:videoId/abuse/:id', + authenticate, + 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) +) // --------------------------------------------------------------------------- @@ -46,12 +60,39 @@ export { // --------------------------------------------------------------------------- -async function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) { +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 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 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 VideoModel const reporterAccount = res.locals.oauth.token.User.Account as AccountModel @@ -60,10 +101,11 @@ async function reportVideoAbuse (req: express.Request, res: express.Response) { const abuseToCreate = { reporterAccountId: reporterAccount.id, reason: body.reason, - videoId: videoInstance.id + videoId: videoInstance.id, + state: VideoAbuseState.PENDING } - await sequelizeTypescript.transaction(async t => { + const videoAbuse: VideoAbuseModel = await sequelizeTypescript.transaction(async t => { const videoAbuseInstance = await VideoAbuseModel.create(abuseToCreate, { transaction: t }) videoAbuseInstance.Video = videoInstance videoAbuseInstance.Account = reporterAccount @@ -74,8 +116,12 @@ async function reportVideoAbuse (req: express.Request, res: express.Response) { } auditLogger.create(reporterAccount.Actor.getIdentifier(), new VideoAbuseAuditView(videoAbuseInstance.toFormattedJSON())) - logger.info('Abuse report for video %s created.', videoInstance.name) + + return videoAbuseInstance }) - return res.type('json').status(204).end() + logger.info('Abuse report for video %s created.', videoInstance.name) + return res.json({ + videoAbuse: videoAbuse.toFormattedJSON() + }).end() } -- cgit v1.2.3