aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/process/process-flag.ts
blob: 10f58ef27be981d833e6a8291f4fc7caf7d0f25f (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { createAccountAbuse, createVideoAbuse, createVideoCommentAbuse } from '@server/lib/moderation'
import { AccountModel } from '@server/models/account/account'
import { VideoModel } from '@server/models/video/video'
import { VideoCommentModel } from '@server/models/video/video-comment'
import { abusePredefinedReasonsMap } from '@shared/core-utils/abuse'
import { AbuseObject, AbuseState, ActivityCreate, ActivityFlag } from '@shared/models'
import { retryTransactionWrapper } from '../../../helpers/database-utils'
import { logger } from '../../../helpers/logger'
import { sequelizeTypescript } from '../../../initializers/database'
import { getAPId } from '../../../lib/activitypub/activity'
import { APProcessorOptions } from '../../../types/activitypub-processor.model'
import { MAccountDefault, MActorSignature, MCommentOwnerVideo } from '../../../types/models'

async function processFlagActivity (options: APProcessorOptions<ActivityCreate | ActivityFlag>) {
  const { activity, byActor } = options

  return retryTransactionWrapper(processCreateAbuse, activity, byActor)
}

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

export {
  processFlagActivity
}

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

async function processCreateAbuse (activity: ActivityCreate | ActivityFlag, byActor: MActorSignature) {
  const flag = activity.type === 'Flag' ? activity : (activity.object as AbuseObject)

  const account = byActor.Account
  if (!account) throw new Error('Cannot create abuse with the non account actor ' + byActor.url)

  const reporterAccount = await AccountModel.load(account.id)

  const objects = Array.isArray(flag.object) ? flag.object : [ flag.object ]

  const tags = Array.isArray(flag.tag) ? flag.tag : []
  const predefinedReasons = tags.map(tag => abusePredefinedReasonsMap[tag.name])
                                .filter(v => !isNaN(v))

  const startAt = flag.startAt
  const endAt = flag.endAt

  for (const object of objects) {
    try {
      const uri = getAPId(object)

      logger.debug('Reporting remote abuse for object %s.', uri)

      await sequelizeTypescript.transaction(async t => {
        const video = await VideoModel.loadByUrlAndPopulateAccount(uri, t)
        let videoComment: MCommentOwnerVideo
        let flaggedAccount: MAccountDefault

        if (!video) videoComment = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(uri, t)
        if (!videoComment) flaggedAccount = await AccountModel.loadByUrl(uri, t)

        if (!video && !videoComment && !flaggedAccount) {
          logger.warn('Cannot flag unknown entity %s.', object)
          return
        }

        const baseAbuse = {
          reporterAccountId: reporterAccount.id,
          reason: flag.content,
          state: AbuseState.PENDING,
          predefinedReasons
        }

        if (video) {
          return createVideoAbuse({
            baseAbuse,
            startAt,
            endAt,
            reporterAccount,
            transaction: t,
            videoInstance: video,
            skipNotification: false
          })
        }

        if (videoComment) {
          return createVideoCommentAbuse({
            baseAbuse,
            reporterAccount,
            transaction: t,
            commentInstance: videoComment,
            skipNotification: false
          })
        }

        return await createAccountAbuse({
          baseAbuse,
          reporterAccount,
          transaction: t,
          accountInstance: flaggedAccount,
          skipNotification: false
        })
      })
    } catch (err) {
      logger.debug('Cannot process report of %s', getAPId(object), { err })
    }
  }
}