]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/send/send-flag.ts
Add test on AP hooks
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-flag.ts
index 61ee389a61157a84cee8d35ed9c1ad9f95e7f62f..138eb5adc8d1043142bd04af816eda8428401c86 100644 (file)
@@ -1,34 +1,36 @@
-import { ActorModel } from '../../../models/activitypub/actor'
-import { VideoModel } from '../../../models/video/video'
-import { VideoAbuseModel } from '../../../models/video/video-abuse'
-import { getVideoAbuseActivityPubUrl } from '../url'
-import { unicastTo } from './utils'
+import { Transaction } from 'sequelize'
+import { ActivityAudience, ActivityFlag } from '@shared/models'
 import { logger } from '../../../helpers/logger'
-import { ActivityAudience, ActivityFlag } from '../../../../shared/models/activitypub'
+import { MAbuseAP, MAccountLight, MActor } from '../../../types/models'
 import { audiencify, getAudience } from '../audience'
-import { Transaction } from 'sequelize'
+import { getLocalAbuseActivityPubUrl } from '../url'
+import { unicastTo } from './shared/send-utils'
 
-async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) {
-  if (!video.VideoChannel.Account.Actor.serverId) return // Local user
+function sendAbuse (byActor: MActor, abuse: MAbuseAP, flaggedAccount: MAccountLight, t: Transaction) {
+  if (!flaggedAccount.Actor.serverId) return // Local user
 
-  const url = getVideoAbuseActivityPubUrl(videoAbuse)
+  const url = getLocalAbuseActivityPubUrl(abuse)
 
-  logger.info('Creating job to send video abuse %s.', url)
+  logger.info('Creating job to send abuse %s.', url)
 
   // Custom audience, we only send the abuse to the origin instance
-  const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
-  const flagActivity = buildFlagActivity(url, byActor, videoAbuse, audience)
-
-  t.afterCommit(() => unicastTo(flagActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl))
+  const audience = { to: [ flaggedAccount.Actor.url ], cc: [] }
+  const flagActivity = buildFlagActivity(url, byActor, abuse, audience)
+
+  return t.afterCommit(() => {
+    return unicastTo({
+      data: flagActivity,
+      byActor,
+      toActorUrl: flaggedAccount.Actor.getSharedInbox(),
+      contextType: 'Flag'
+    })
+  })
 }
 
-function buildFlagActivity (url: string, byActor: ActorModel, videoAbuse: VideoAbuseModel, audience: ActivityAudience): ActivityFlag {
+function buildFlagActivity (url: string, byActor: MActor, abuse: MAbuseAP, audience: ActivityAudience): ActivityFlag {
   if (!audience) audience = getAudience(byActor)
 
-  const activity = Object.assign(
-    { id: url, actor: byActor.url },
-    videoAbuse.toActivityPubObject()
-  )
+  const activity = { id: url, actor: byActor.url, ...abuse.toActivityPubObject() }
 
   return audiencify(activity, audience)
 }
@@ -36,5 +38,5 @@ function buildFlagActivity (url: string, byActor: ActorModel, videoAbuse: VideoA
 // ---------------------------------------------------------------------------
 
 export {
-  sendVideoAbuse
+  sendAbuse
 }