]> 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 68db025b7f3bf67a0ec079b61039bdd5fdd01171..3413ae894d40fcf32b170a18e5a4e569533f7a14 100644 (file)
@@ -1,41 +1,39 @@
 import * as express from 'express'
-import { waterfall } from 'async'
-
-import { database as db } from '../../../initializers/database'
-import * as friends from '../../../lib/friends'
-import {
-  logger,
-  getFormatedObjects,
-  retryTransactionWrapper,
-  startSerializableTransaction,
-  commitTransaction,
-  rollbackTransaction
-} 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,
-  ensureIsAdmin,
+  ensureUserHasRight,
   paginationValidator,
+  setDefaultPagination,
+  setDefaultSort,
   videoAbuseReportValidator,
-  videoAbusesSortValidator,
-  setVideoAbusesSort,
-  setPagination
+  videoAbusesSortValidator
 } from '../../../middlewares'
+import { AccountModel } from '../../../models/account/account'
+import { VideoModel } from '../../../models/video/video'
+import { VideoAbuseModel } from '../../../models/video/video-abuse'
 
 const abuseVideoRouter = express.Router()
 
 abuseVideoRouter.get('/abuse',
   authenticate,
-  ensureIsAdmin,
+  ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES),
   paginationValidator,
   videoAbusesSortValidator,
-  setVideoAbusesSort,
-  setPagination,
-  listVideoAbuses
+  setDefaultSort,
+  setDefaultPagination,
+  asyncMiddleware(listVideoAbuses)
 )
 abuseVideoRouter.post('/:id/abuse',
   authenticate,
-  videoAbuseReportValidator,
-  reportVideoAbuseRetryWrapper
+  asyncMiddleware(videoAbuseReportValidator),
+  asyncRetryTransactionMiddleware(reportVideoAbuse)
 )
 
 // ---------------------------------------------------------------------------
@@ -46,72 +44,34 @@ export {
 
 // ---------------------------------------------------------------------------
 
-function listVideoAbuses (req, res, next) {
-  db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort, function (err, abusesList, abusesTotal) {
-    if (err) return next(err)
-
-    res.json(getFormatedObjects(abusesList, abusesTotal))
-  })
-}
-
-function reportVideoAbuseRetryWrapper (req, res, next) {
-  const options = {
-    arguments: [ req, res ],
-    errorMessage: 'Cannot report abuse to the video with many retries.'
-  }
-
-  retryTransactionWrapper(reportVideoAbuse, options, function (err) {
-    if (err) return next(err)
+async function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) {
+  const resultList = await VideoAbuseModel.listForApi(req.query.start, req.query.count, req.query.sort)
 
-    return res.type('json').status(204).end()
-  })
+  return res.json(getFormattedObjects(resultList.data, resultList.total))
 }
 
-function reportVideoAbuse (req, res, finalCallback) {
-  const videoInstance = res.locals.video
-  const reporterUsername = res.locals.oauth.token.User.username
+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
+  const body: VideoAbuseCreate = req.body
 
-  const abuse = {
-    reporterUsername,
-    reason: req.body.reason,
-    videoId: videoInstance.id,
-    reporterPodId: null // This is our pod that reported this abuse
+  const abuseToCreate = {
+    reporterAccountId: reporterAccount.id,
+    reason: body.reason,
+    videoId: videoInstance.id
   }
 
-  waterfall([
-
-    startSerializableTransaction,
-
-    function createAbuse (t, callback) {
-      db.VideoAbuse.create(abuse).asCallback(function (err, abuse) {
-        return callback(err, t, abuse)
-      })
-    },
-
-    function sendToFriendsIfNeeded (t, abuse, callback) {
-      // We send the information to the destination pod
-      if (videoInstance.isOwned() === false) {
-        const reportData = {
-          reporterUsername,
-          reportReason: abuse.reason,
-          videoRemoteId: videoInstance.remoteId
-        }
-
-        friends.reportAbuseVideoToFriend(reportData, videoInstance)
-      }
+  await sequelizeTypescript.transaction(async t => {
+    const videoAbuseInstance = await VideoAbuseModel.create(abuseToCreate, { transaction: t })
+    videoAbuseInstance.Video = videoInstance
 
-      return callback(null, t)
-    },
-
-    commitTransaction
-
-  ], function andFinally (err, t) {
-    if (err) {
-      logger.debug('Cannot update the video.', { error: err })
-      return rollbackTransaction(err, t, finalCallback)
+    // 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 finalCallback(null)
   })
+
+  logger.info('Abuse report for video %s created.', videoInstance.name)
+
+  return res.type('json').status(204).end()
 }