]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-flag.ts
Fix embed url
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-flag.ts
index 422386540fc635184a8eadb6053726344d098193..1d7132a3a7010f8a04ac94747ae12af42e20070f 100644 (file)
@@ -1,14 +1,20 @@
-import { ActivityCreate, ActivityFlag, VideoAbuseState } from '../../../../shared'
+import {
+  ActivityCreate,
+  ActivityFlag,
+  VideoAbuseState,
+  videoAbusePredefinedReasonsMap
+} from '../../../../shared'
 import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects'
 import { retryTransactionWrapper } from '../../../helpers/database-utils'
 import { logger } from '../../../helpers/logger'
-import { sequelizeTypescript } from '../../../initializers'
+import { sequelizeTypescript } from '../../../initializers/database'
 import { VideoAbuseModel } from '../../../models/video/video-abuse'
 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
 import { Notifier } from '../../notifier'
 import { getAPId } from '../../../helpers/activitypub'
-import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
-import { MActorSignature, MVideoAbuseVideo } from '../../../typings/models'
+import { APProcessorOptions } from '../../../types/activitypub-processor.model'
+import { MActorSignature, MVideoAbuseAccountVideo } from '../../../types/models'
+import { AccountModel } from '@server/models/account/account'
 
 async function processFlagActivity (options: APProcessorOptions<ActivityCreate | ActivityFlag>) {
   const { activity, byActor } = options
@@ -26,28 +32,52 @@ export {
 async function processCreateVideoAbuse (activity: ActivityCreate | ActivityFlag, byActor: MActorSignature) {
   const flag = activity.type === 'Flag' ? activity : (activity.object as VideoAbuseObject)
 
-  logger.debug('Reporting remote abuse for video %s.', getAPId(flag.object))
-
   const account = byActor.Account
   if (!account) throw new Error('Cannot create video abuse with the non account actor ' + byActor.url)
 
-  const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: flag.object })
+  const objects = Array.isArray(flag.object) ? flag.object : [ flag.object ]
 
-  const videoAbuse = await sequelizeTypescript.transaction(async t => {
-    const videoAbuseData = {
-      reporterAccountId: account.id,
-      reason: flag.content,
-      videoId: video.id,
-      state: VideoAbuseState.PENDING
-    }
+  for (const object of objects) {
+    try {
+      logger.debug('Reporting remote abuse for video %s.', getAPId(object))
+
+      const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: object })
+      const reporterAccount = await sequelizeTypescript.transaction(async t => AccountModel.load(account.id, t))
+      const tags = Array.isArray(flag.tag) ? flag.tag : []
+      const predefinedReasons = tags.map(tag => videoAbusePredefinedReasonsMap[tag.name])
+                                    .filter(v => !isNaN(v))
+      const startAt = flag.startAt
+      const endAt = flag.endAt
 
-    const videoAbuseInstance = await VideoAbuseModel.create(videoAbuseData, { transaction: t }) as MVideoAbuseVideo
-    videoAbuseInstance.Video = video
+      const videoAbuseInstance = await sequelizeTypescript.transaction(async t => {
+        const videoAbuseData = {
+          reporterAccountId: account.id,
+          reason: flag.content,
+          videoId: video.id,
+          state: VideoAbuseState.PENDING,
+          predefinedReasons,
+          startAt,
+          endAt
+        }
 
-    logger.info('Remote abuse for video uuid %s created', flag.object)
+        const videoAbuseInstance: MVideoAbuseAccountVideo = await VideoAbuseModel.create(videoAbuseData, { transaction: t })
+        videoAbuseInstance.Video = video
+        videoAbuseInstance.Account = reporterAccount
 
-    return videoAbuseInstance
-  })
+        logger.info('Remote abuse for video uuid %s created', flag.object)
 
-  Notifier.Instance.notifyOnNewVideoAbuse(videoAbuse)
+        return videoAbuseInstance
+      })
+
+      const videoAbuseJSON = videoAbuseInstance.toFormattedJSON()
+
+      Notifier.Instance.notifyOnNewVideoAbuse({
+        videoAbuse: videoAbuseJSON,
+        videoAbuseInstance,
+        reporter: reporterAccount.Actor.getIdentifier()
+      })
+    } catch (err) {
+      logger.debug('Cannot process report of %s. (Maybe not a video abuse).', getAPId(object), { err })
+    }
+  }
 }