]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/abuse.ts
Cleanup express locals typings
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / abuse.ts
index 29e1175c5a0d3adba97df17ff9550a6745504ed1..ca70230a2f475eb05e7a7f40c9821a62fdf15b12 100644 (file)
@@ -1,25 +1,28 @@
 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 {
+  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 { sendVideoAbuse } from '../../../lib/index'
+import { AccountModel } from '../../../models/account/account'
+import { VideoAbuseModel } from '../../../models/video/video-abuse'
+import { auditLoggerFactory, VideoAbuseAuditView } from '../../../helpers/audit-logger'
+import { Notifier } from '../../../lib/notifier'
+import { sendVideoAbuse } from '../../../lib/activitypub/send/send-flag'
 
+const auditLogger = auditLoggerFactory('abuse')
 const abuseVideoRouter = express.Router()
 
 abuseVideoRouter.get('/abuse',
@@ -27,14 +30,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,
+  ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES),
+  asyncMiddleware(videoAbuseUpdateValidator),
+  asyncRetryTransactionMiddleware(updateVideoAbuse)
+)
+abuseVideoRouter.post('/:videoId/abuse',
   authenticate,
   asyncMiddleware(videoAbuseReportValidator),
-  asyncMiddleware(reportVideoAbuseRetryWrapper)
+  asyncRetryTransactionMiddleware(reportVideoAbuse)
+)
+abuseVideoRouter.delete('/:videoId/abuse/:id',
+  authenticate,
+  ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES),
+  asyncMiddleware(videoAbuseGetValidator),
+  asyncRetryTransactionMiddleware(deleteVideoAbuse)
 )
 
 // ---------------------------------------------------------------------------
@@ -45,43 +60,70 @@ 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 = 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 = res.locals.videoAbuse
+
+  await sequelizeTypescript.transaction(t => {
+    return videoAbuse.destroy({ transaction: t })
+  })
 
-  await retryTransactionWrapper(reportVideoAbuse, options)
+  // 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 reporterAccount = res.locals.oauth.token.User.Account
+  const videoInstance = res.locals.video
   const body: VideoAbuseCreate = req.body
 
-  const abuseToCreate = {
-    reporterAccountId: reporterAccount.id,
-    reason: body.reason,
-    videoId: videoInstance.id
-  }
+  const videoAbuse: VideoAbuseModel = await sequelizeTypescript.transaction(async t => {
+    const reporterAccount = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
 
-  await db.sequelize.transaction(async t => {
-    const videoAbuseInstance = await db.VideoAbuse.create(abuseToCreate, { transaction: t })
+    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) {
-      await sendVideoAbuse(reporterAccount, videoAbuseInstance, videoInstance, t)
+      await sendVideoAbuse(reporterAccount.Actor, videoAbuseInstance, videoInstance)
     }
+
+    Notifier.Instance.notifyOnNewVideoAbuse(videoAbuseInstance)
+
+    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()
 }