]>
Commit | Line | Data |
---|---|---|
d95d1559 C |
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' | |
848f499d C |
7 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
8 | import { logger } from '../../../helpers/logger' | |
80fdaf06 | 9 | import { sequelizeTypescript } from '../../../initializers/database' |
26d6bf65 | 10 | import { APProcessorOptions } from '../../../types/activitypub-processor.model' |
d95d1559 | 11 | import { MAccountDefault, MActorSignature, MCommentOwnerVideo } from '../../../types/models' |
848f499d | 12 | |
1198edf4 C |
13 | async function processFlagActivity (options: APProcessorOptions<ActivityCreate | ActivityFlag>) { |
14 | const { activity, byActor } = options | |
d95d1559 C |
15 | |
16 | return retryTransactionWrapper(processCreateAbuse, activity, byActor) | |
848f499d C |
17 | } |
18 | ||
19 | // --------------------------------------------------------------------------- | |
20 | ||
21 | export { | |
22 | processFlagActivity | |
23 | } | |
24 | ||
25 | // --------------------------------------------------------------------------- | |
26 | ||
d95d1559 C |
27 | async function processCreateAbuse (activity: ActivityCreate | ActivityFlag, byActor: MActorSignature) { |
28 | const flag = activity.type === 'Flag' ? activity : (activity.object as AbuseObject) | |
848f499d | 29 | |
848f499d | 30 | const account = byActor.Account |
d95d1559 C |
31 | if (!account) throw new Error('Cannot create abuse with the non account actor ' + byActor.url) |
32 | ||
33 | const reporterAccount = await AccountModel.load(account.id) | |
848f499d | 34 | |
0b5c385b | 35 | const objects = Array.isArray(flag.object) ? flag.object : [ flag.object ] |
848f499d | 36 | |
d95d1559 C |
37 | const tags = Array.isArray(flag.tag) ? flag.tag : [] |
38 | const predefinedReasons = tags.map(tag => abusePredefinedReasonsMap[tag.name]) | |
39 | .filter(v => !isNaN(v)) | |
40 | ||
41 | const startAt = flag.startAt | |
42 | const endAt = flag.endAt | |
43 | ||
0b5c385b C |
44 | for (const object of objects) { |
45 | try { | |
d95d1559 | 46 | const uri = getAPId(object) |
848f499d | 47 | |
d95d1559 | 48 | logger.debug('Reporting remote abuse for object %s.', uri) |
2284f202 | 49 | |
d95d1559 | 50 | await sequelizeTypescript.transaction(async t => { |
2284f202 | 51 | |
d95d1559 C |
52 | const video = await VideoModel.loadByUrlAndPopulateAccount(uri) |
53 | let videoComment: MCommentOwnerVideo | |
54 | let flaggedAccount: MAccountDefault | |
55 | ||
56 | if (!video) videoComment = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(uri) | |
57 | if (!videoComment) flaggedAccount = await AccountModel.loadByUrl(uri) | |
58 | ||
59 | if (!video && !videoComment && !flaggedAccount) { | |
60 | logger.warn('Cannot flag unknown entity %s.', object) | |
61 | return | |
62 | } | |
63 | ||
64 | const baseAbuse = { | |
65 | reporterAccountId: reporterAccount.id, | |
66 | reason: flag.content, | |
67 | state: AbuseState.PENDING, | |
68 | predefinedReasons | |
69 | } | |
0b5c385b | 70 | |
d95d1559 C |
71 | if (video) { |
72 | return createVideoAbuse({ | |
73 | baseAbuse, | |
74 | startAt, | |
75 | endAt, | |
76 | reporterAccount, | |
77 | transaction: t, | |
78 | videoInstance: video | |
79 | }) | |
80 | } | |
81 | ||
82 | if (videoComment) { | |
83 | return createVideoCommentAbuse({ | |
84 | baseAbuse, | |
85 | reporterAccount, | |
86 | transaction: t, | |
87 | commentInstance: videoComment | |
88 | }) | |
89 | } | |
df4c603d | 90 | |
d95d1559 C |
91 | return await createAccountAbuse({ |
92 | baseAbuse, | |
93 | reporterAccount, | |
94 | transaction: t, | |
95 | accountInstance: flaggedAccount | |
96 | }) | |
df4c603d | 97 | }) |
0b5c385b | 98 | } catch (err) { |
d95d1559 | 99 | logger.debug('Cannot process report of %s', getAPId(object), { err }) |
0b5c385b C |
100 | } |
101 | } | |
848f499d | 102 | } |