Split types and typings
[oweals/peertube.git] / server / lib / activitypub / process / process-flag.ts
1 import { ActivityCreate, ActivityFlag, VideoAbuseState } from '../../../../shared'
2 import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects'
3 import { retryTransactionWrapper } from '../../../helpers/database-utils'
4 import { logger } from '../../../helpers/logger'
5 import { sequelizeTypescript } from '../../../initializers/database'
6 import { VideoAbuseModel } from '../../../models/video/video-abuse'
7 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
8 import { Notifier } from '../../notifier'
9 import { getAPId } from '../../../helpers/activitypub'
10 import { APProcessorOptions } from '../../../types/activitypub-processor.model'
11 import { MActorSignature, MVideoAbuseAccountVideo } from '../../../types/models'
12 import { AccountModel } from '@server/models/account/account'
13
14 async function processFlagActivity (options: APProcessorOptions<ActivityCreate | ActivityFlag>) {
15   const { activity, byActor } = options
16   return retryTransactionWrapper(processCreateVideoAbuse, activity, byActor)
17 }
18
19 // ---------------------------------------------------------------------------
20
21 export {
22   processFlagActivity
23 }
24
25 // ---------------------------------------------------------------------------
26
27 async function processCreateVideoAbuse (activity: ActivityCreate | ActivityFlag, byActor: MActorSignature) {
28   const flag = activity.type === 'Flag' ? activity : (activity.object as VideoAbuseObject)
29
30   const account = byActor.Account
31   if (!account) throw new Error('Cannot create video abuse with the non account actor ' + byActor.url)
32
33   const objects = Array.isArray(flag.object) ? flag.object : [ flag.object ]
34
35   for (const object of objects) {
36     try {
37       logger.debug('Reporting remote abuse for video %s.', getAPId(object))
38
39       const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: object })
40       const reporterAccount = await sequelizeTypescript.transaction(async t => AccountModel.load(account.id, t))
41
42       const videoAbuseInstance = await sequelizeTypescript.transaction(async t => {
43         const videoAbuseData = {
44           reporterAccountId: account.id,
45           reason: flag.content,
46           videoId: video.id,
47           state: VideoAbuseState.PENDING
48         }
49
50         const videoAbuseInstance: MVideoAbuseAccountVideo = await VideoAbuseModel.create(videoAbuseData, { transaction: t })
51         videoAbuseInstance.Video = video
52         videoAbuseInstance.Account = reporterAccount
53
54         logger.info('Remote abuse for video uuid %s created', flag.object)
55
56         return videoAbuseInstance
57       })
58
59       const videoAbuseJSON = videoAbuseInstance.toFormattedJSON()
60
61       Notifier.Instance.notifyOnNewVideoAbuse({
62         videoAbuse: videoAbuseJSON,
63         videoAbuseInstance,
64         reporter: reporterAccount.Actor.getIdentifier()
65       })
66     } catch (err) {
67       logger.debug('Cannot process report of %s. (Maybe not a video abuse).', getAPId(object), { err })
68     }
69   }
70 }