aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/send/send-flag.ts
blob: 138eb5adc8d1043142bd04af816eda8428401c86 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { Transaction } from 'sequelize'
import { ActivityAudience, ActivityFlag } from '@shared/models'
import { logger } from '../../../helpers/logger'
import { MAbuseAP, MAccountLight, MActor } from '../../../types/models'
import { audiencify, getAudience } from '../audience'
import { getLocalAbuseActivityPubUrl } from '../url'
import { unicastTo } from './shared/send-utils'

function sendAbuse (byActor: MActor, abuse: MAbuseAP, flaggedAccount: MAccountLight, t: Transaction) {
  if (!flaggedAccount.Actor.serverId) return // Local user

  const url = getLocalAbuseActivityPubUrl(abuse)

  logger.info('Creating job to send abuse %s.', url)

  // Custom audience, we only send the abuse to the origin instance
  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: MActor, abuse: MAbuseAP, audience: ActivityAudience): ActivityFlag {
  if (!audience) audience = getAudience(byActor)

  const activity = { id: url, actor: byActor.url, ...abuse.toActivityPubObject() }

  return audiencify(activity, audience)
}

// ---------------------------------------------------------------------------

export {
  sendAbuse
}