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/models'
7 import { retryTransactionWrapper } from '../../../helpers/database-utils'
8 import { logger } from '../../../helpers/logger'
9 import { sequelizeTypescript } from '../../../initializers/database'
10 import { getAPId } from '../../../lib/activitypub/activity'
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 => {
52 const video = await VideoModel.loadByUrlAndPopulateAccount(uri, t)
53 let videoComment: MCommentOwnerVideo
54 let flaggedAccount: MAccountDefault
56 if (!video) videoComment = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(uri, t)
57 if (!videoComment) flaggedAccount = await AccountModel.loadByUrl(uri, t)
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({
79 skipNotification: false
84 return createVideoCommentAbuse({
88 commentInstance: videoComment,
89 skipNotification: false
93 return await createAccountAbuse({
97 accountInstance: flaggedAccount,
98 skipNotification: false
102 logger.debug('Cannot process report of %s', getAPId(object), { err })