]> 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 7d2e3bcfbd2c792907cc430678bc9a6f0d24e583..3413ae894d40fcf32b170a18e5a4e569533f7a14 100644 (file)
@@ -1,39 +1,39 @@
 import * as express from 'express'
-
-import { database as db } from '../../../initializers/database'
-import * as friends from '../../../lib/friends'
-import {
-  logger,
-  getFormatedObjects,
-  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,
-  ensureIsAdmin,
+  ensureUserHasRight,
   paginationValidator,
+  setDefaultPagination,
+  setDefaultSort,
   videoAbuseReportValidator,
-  videoAbusesSortValidator,
-  setVideoAbusesSort,
-  setPagination
+  videoAbusesSortValidator
 } from '../../../middlewares'
-import { VideoInstance } from '../../../models'
-import { VideoAbuseCreate } 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()
 
 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)
 )
 
 // ---------------------------------------------------------------------------
@@ -44,55 +44,34 @@ export {
 
 // ---------------------------------------------------------------------------
 
-function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) {
-  db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort)
-    .then(result => res.json(getFormatedObjects(result.data, result.total)))
-    .catch(err => 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)
 
-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.'
-  }
-
-  retryTransactionWrapper(reportVideoAbuse, options)
-    .then(() => res.type('json').status(204).end())
-    .catch(err => next(err))
+  return res.json(getFormattedObjects(resultList.data, resultList.total))
 }
 
-function reportVideoAbuse (req: express.Request, res: express.Response) {
-  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,
+  const abuseToCreate = {
+    reporterAccountId: reporterAccount.id,
     reason: body.reason,
-    videoId: videoInstance.id,
-    reporterPodId: null // This is our pod that reported this abuse
+    videoId: videoInstance.id
   }
 
-  return db.sequelize.transaction(t => {
-    return db.VideoAbuse.create(abuse, { transaction: t })
-      .then(abuse => {
-        // We send the information to the destination pod
-        if (videoInstance.isOwned() === false) {
-          const reportData = {
-            reporterUsername,
-            reportReason: abuse.reason,
-            videoRemoteId: videoInstance.remoteId
-          }
-
-          return friends.reportAbuseVideoToFriend(reportData, videoInstance, t).then(() => videoInstance)
-        }
+  await sequelizeTypescript.transaction(async t => {
+    const videoAbuseInstance = await VideoAbuseModel.create(abuseToCreate, { transaction: t })
+    videoAbuseInstance.Video = videoInstance
 
-        return videoInstance
-      })
-  })
-  .then((videoInstance: VideoInstance) => logger.info('Abuse report for video %s created.', videoInstance.name))
-  .catch(err => {
-    logger.debug('Cannot update the video.', err)
-    throw err
+    // 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()
 }