aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/process/process-flag.ts
blob: 7337f337c6e487dabf569d8041820955a1771167 (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
import { ActivityCreate, ActivityFlag, VideoAbuseState } from '../../../../shared'
import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects'
import { retryTransactionWrapper } from '../../../helpers/database-utils'
import { logger } from '../../../helpers/logger'
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, MVideoAbuseAccountVideo } from '../../../typings/models'
import { AccountModel } from '@server/models/account/account'

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

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

export {
  processFlagActivity
}

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

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

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

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

  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 videoAbuseInstance = await sequelizeTypescript.transaction(async t => {
        const videoAbuseData = {
          reporterAccountId: account.id,
          reason: flag.content,
          videoId: video.id,
          state: VideoAbuseState.PENDING
        }

        const videoAbuseInstance: MVideoAbuseAccountVideo = await VideoAbuseModel.create(videoAbuseData, { transaction: t })
        videoAbuseInstance.Video = video
        videoAbuseInstance.Account = reporterAccount

        logger.info('Remote abuse for video uuid %s created', flag.object)

        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 })
    }
  }
}