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 { abusePredefinedReasonsMap } from '@shared/core-utils/abuse'
6 import { AbuseObject, AbuseState, ActivityCreate, ActivityFlag } from '../../../../shared'
7 import { getAPId } from '../../../helpers/activitypub'
8 import { retryTransactionWrapper } from '../../../helpers/database-utils'
9 import { logger } from '../../../helpers/logger'
10 import { sequelizeTypescript } from '../../../initializers/database'
11 import { APProcessorOptions } from '../../../types/activitypub-processor.model'
12 import { MAccountDefault, MActorSignature, MCommentOwnerVideo } from '../../../types/models'
14 async function processFlagActivity (options: APProcessorOptions<ActivityCreate | ActivityFlag>) {
15 const { activity, byActor } = options
17 return retryTransactionWrapper(processCreateAbuse, activity, byActor)
20 // ---------------------------------------------------------------------------
26 // ---------------------------------------------------------------------------
28 async function processCreateAbuse (activity: ActivityCreate | ActivityFlag, byActor: MActorSignature) {
29 const flag = activity.type === 'Flag' ? activity : (activity.object as AbuseObject)
31 const account = byActor.Account
32 if (!account) throw new Error('Cannot create abuse with the non account actor ' + byActor.url)
34 const reporterAccount = await AccountModel.load(account.id)
36 const objects = Array.isArray(flag.object) ? flag.object : [ flag.object ]
38 const tags = Array.isArray(flag.tag) ? flag.tag : []
39 const predefinedReasons = tags.map(tag => abusePredefinedReasonsMap[tag.name])
40 .filter(v => !isNaN(v))
42 const startAt = flag.startAt
43 const endAt = flag.endAt
45 for (const object of objects) {
47 const uri = getAPId(object)
49 logger.debug('Reporting remote abuse for object %s.', uri)
51 await sequelizeTypescript.transaction(async t => {
53 const video = await VideoModel.loadByUrlAndPopulateAccount(uri)
54 let videoComment: MCommentOwnerVideo
55 let flaggedAccount: MAccountDefault
57 if (!video) videoComment = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(uri)
58 if (!videoComment) flaggedAccount = await AccountModel.loadByUrl(uri)
60 if (!video && !videoComment && !flaggedAccount) {
61 logger.warn('Cannot flag unknown entity %s.', object)
66 reporterAccountId: reporterAccount.id,
68 state: AbuseState.PENDING,
73 return createVideoAbuse({
84 return createVideoCommentAbuse({
88 commentInstance: videoComment
92 return await createAccountAbuse({
96 accountInstance: flaggedAccount
100 logger.debug('Cannot process report of %s', getAPId(object), { err })