1 import { createAccountAbuse, createVideoAbuse, createVideoCommentAbuse } from '@server/lib/moderation'
2 import { AccountModel } from '@server/models/account/account'
3 import { VideoModel } from '@server/models/video/video'
4 import { VideoCommentModel } from '@server/models/video/video-comment'
5 import { AbuseObject, abusePredefinedReasonsMap, AbuseState, ActivityCreate, ActivityFlag } from '../../../../shared'
6 import { getAPId } from '../../../helpers/activitypub'
7 import { retryTransactionWrapper } from '../../../helpers/database-utils'
8 import { logger } from '../../../helpers/logger'
9 import { sequelizeTypescript } from '../../../initializers/database'
10 import { APProcessorOptions } from '../../../types/activitypub-processor.model'
11 import { MAccountDefault, MActorSignature, MCommentOwnerVideo } from '../../../types/models'
13 async function processFlagActivity (options: APProcessorOptions<ActivityCreate | ActivityFlag>) {
14 const { activity, byActor } = options
16 return retryTransactionWrapper(processCreateAbuse, activity, byActor)
19 // ---------------------------------------------------------------------------
25 // ---------------------------------------------------------------------------
27 async function processCreateAbuse (activity: ActivityCreate | ActivityFlag, byActor: MActorSignature) {
28 const flag = activity.type === 'Flag' ? activity : (activity.object as AbuseObject)
30 const account = byActor.Account
31 if (!account) throw new Error('Cannot create abuse with the non account actor ' + byActor.url)
33 const reporterAccount = await AccountModel.load(account.id)
35 const objects = Array.isArray(flag.object) ? flag.object : [ flag.object ]
37 const tags = Array.isArray(flag.tag) ? flag.tag : []
38 const predefinedReasons = tags.map(tag => abusePredefinedReasonsMap[tag.name])
39 .filter(v => !isNaN(v))
41 const startAt = flag.startAt
42 const endAt = flag.endAt
44 for (const object of objects) {
46 const uri = getAPId(object)
48 logger.debug('Reporting remote abuse for object %s.', uri)
50 await sequelizeTypescript.transaction(async t => {
52 const video = await VideoModel.loadByUrlAndPopulateAccount(uri)
53 let videoComment: MCommentOwnerVideo
54 let flaggedAccount: MAccountDefault
56 if (!video) videoComment = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(uri)
57 if (!videoComment) flaggedAccount = await AccountModel.loadByUrl(uri)
59 if (!video && !videoComment && !flaggedAccount) {
60 logger.warn('Cannot flag unknown entity %s.', object)
65 reporterAccountId: reporterAccount.id,
67 state: AbuseState.PENDING,
72 return createVideoAbuse({
83 return createVideoCommentAbuse({
87 commentInstance: videoComment
91 return await createAccountAbuse({
95 accountInstance: flaggedAccount
99 logger.debug('Cannot process report of %s', getAPId(object), { err })