]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/abuse.ts
Add ability for uploaders to schedule video update
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / abuse.ts
index 04349042b02b1cb6535b3a354fe37c1ae022186d..3413ae894d40fcf32b170a18e5a4e569533f7a14 100644 (file)
@@ -1,24 +1,23 @@
 import * as express from 'express'
-
-import { database as db } from '../../../initializers/database'
-import * as friends from '../../../lib/friends'
-import {
-  logger,
-  getFormattedObjects,
-  retryTransactionWrapper
-} from '../../../helpers'
+import { UserRight, VideoAbuseCreate } 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,
   videoAbuseReportValidator,
-  videoAbusesSortValidator,
-  setVideoAbusesSort,
-  setPagination,
-  asyncMiddleware
+  videoAbusesSortValidator
 } 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'
 
 const abuseVideoRouter = express.Router()
 
@@ -27,14 +26,14 @@ abuseVideoRouter.get('/abuse',
   ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES),
   paginationValidator,
   videoAbusesSortValidator,
-  setVideoAbusesSort,
-  setPagination,
+  setDefaultSort,
+  setDefaultPagination,
   asyncMiddleware(listVideoAbuses)
 )
 abuseVideoRouter.post('/:id/abuse',
   authenticate,
-  videoAbuseReportValidator,
-  asyncMiddleware(reportVideoAbuseRetryWrapper)
+  asyncMiddleware(videoAbuseReportValidator),
+  asyncRetryTransactionMiddleware(reportVideoAbuse)
 )
 
 // ---------------------------------------------------------------------------
@@ -46,47 +45,33 @@ 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)
+  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.'
-  }
-
-  await retryTransactionWrapper(reportVideoAbuse, options)
-
-  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 reporterAccount = res.locals.oauth.token.User.Account as AccountModel
   const body: VideoAbuseCreate = req.body
 
   const abuseToCreate = {
-    reporterUsername,
+    reporterAccountId: reporterAccount.id,
     reason: body.reason,
-    videoId: videoInstance.id,
-    reporterPodId: null // This is our pod that reported this abuse
+    videoId: videoInstance.id
   }
 
-  await db.sequelize.transaction(async t => {
-    const abuse = await db.VideoAbuse.create(abuseToCreate, { transaction: t })
-    // We send the information to the destination pod
-    if (videoInstance.isOwned() === false) {
-      const reportData = {
-        reporterUsername,
-        reportReason: abuse.reason,
-        videoUUID: videoInstance.uuid
-      }
+  await sequelizeTypescript.transaction(async t => {
+    const videoAbuseInstance = await VideoAbuseModel.create(abuseToCreate, { transaction: t })
+    videoAbuseInstance.Video = videoInstance
 
-      await friends.reportAbuseVideoToFriend(reportData, videoInstance, t)
+    // We send the video abuse to the origin server
+    if (videoInstance.isOwned() === false) {
+      await sendVideoAbuse(reporterAccount.Actor, videoAbuseInstance, videoInstance, t)
     }
   })
 
   logger.info('Abuse report for video %s created.', videoInstance.name)
+
+  return res.type('json').status(204).end()
 }