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