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'
6 import { ActorModel } from '../../../models/activitypub/actor'
7 import { VideoAbuseModel } from '../../../models/video/video-abuse'
8 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
9 import { Notifier } from '../../notifier'
10 import { getAPId } from '../../../helpers/activitypub'
11 import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
13 async function processFlagActivity (options: APProcessorOptions<ActivityCreate | ActivityFlag>) {
14 const { activity, byActor } = options
15 return retryTransactionWrapper(processCreateVideoAbuse, activity, byActor)
18 // ---------------------------------------------------------------------------
24 // ---------------------------------------------------------------------------
26 async function processCreateVideoAbuse (activity: ActivityCreate | ActivityFlag, byActor: ActorModel) {
27 const flag = activity.type === 'Flag' ? activity : (activity.object as VideoAbuseObject)
29 logger.debug('Reporting remote abuse for video %s.', getAPId(flag.object))
31 const account = byActor.Account
32 if (!account) throw new Error('Cannot create video abuse with the non account actor ' + byActor.url)
34 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: flag.object })
36 const videoAbuse = await sequelizeTypescript.transaction(async t => {
37 const videoAbuseData = {
38 reporterAccountId: account.id,
41 state: VideoAbuseState.PENDING
44 const videoAbuseInstance = await VideoAbuseModel.create(videoAbuseData, { transaction: t })
45 videoAbuseInstance.Video = video
47 logger.info('Remote abuse for video uuid %s created', flag.object)
49 return videoAbuseInstance
52 Notifier.Instance.notifyOnNewVideoAbuse(videoAbuse)