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'
14 async function processFlagActivity (options: APProcessorOptions<ActivityCreate | ActivityFlag>) {
15 const { activity, byActor } = options
16 return retryTransactionWrapper(processCreateVideoAbuse, activity, byActor)
19 // ---------------------------------------------------------------------------
25 // ---------------------------------------------------------------------------
27 async function processCreateVideoAbuse (activity: ActivityCreate | ActivityFlag, byActor: MActorSignature) {
28 const flag = activity.type === 'Flag' ? activity : (activity.object as VideoAbuseObject)
30 const account = byActor.Account
31 if (!account) throw new Error('Cannot create video abuse with the non account actor ' + byActor.url)
33 const objects = Array.isArray(flag.object) ? flag.object : [ flag.object ]
35 for (const object of objects) {
37 logger.debug('Reporting remote abuse for video %s.', getAPId(object))
39 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: object })
40 const reporterAccount = await sequelizeTypescript.transaction(async t => AccountModel.load(account.id, t))
42 const videoAbuseInstance = await sequelizeTypescript.transaction(async t => {
43 const videoAbuseData = {
44 reporterAccountId: account.id,
47 state: VideoAbuseState.PENDING
50 const videoAbuseInstance: MVideoAbuseAccountVideo = await VideoAbuseModel.create(videoAbuseData, { transaction: t })
51 videoAbuseInstance.Video = video
52 videoAbuseInstance.Account = reporterAccount
54 logger.info('Remote abuse for video uuid %s created', flag.object)
56 return videoAbuseInstance
59 const videoAbuseJSON = videoAbuseInstance.toFormattedJSON()
61 Notifier.Instance.notifyOnNewVideoAbuse({
62 videoAbuse: videoAbuseJSON,
64 reporter: reporterAccount.Actor.getIdentifier()
67 logger.debug('Cannot process report of %s. (Maybe not a video abuse).', getAPId(object), { err })