]>
Commit | Line | Data |
---|---|---|
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' | |
13 | ||
14 | async function processFlagActivity (options: APProcessorOptions<ActivityCreate | ActivityFlag>) { | |
15 | const { activity, byActor } = options | |
16 | ||
17 | return retryTransactionWrapper(processCreateAbuse, activity, byActor) | |
18 | } | |
19 | ||
20 | // --------------------------------------------------------------------------- | |
21 | ||
22 | export { | |
23 | processFlagActivity | |
24 | } | |
25 | ||
26 | // --------------------------------------------------------------------------- | |
27 | ||
28 | async function processCreateAbuse (activity: ActivityCreate | ActivityFlag, byActor: MActorSignature) { | |
29 | const flag = activity.type === 'Flag' ? activity : (activity.object as AbuseObject) | |
30 | ||
31 | const account = byActor.Account | |
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) | |
35 | ||
36 | const objects = Array.isArray(flag.object) ? flag.object : [ flag.object ] | |
37 | ||
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 | ||
45 | for (const object of objects) { | |
46 | try { | |
47 | const uri = getAPId(object) | |
48 | ||
49 | logger.debug('Reporting remote abuse for object %s.', uri) | |
50 | ||
51 | await sequelizeTypescript.transaction(async t => { | |
52 | const video = await VideoModel.loadByUrlAndPopulateAccount(uri, t) | |
53 | let videoComment: MCommentOwnerVideo | |
54 | let flaggedAccount: MAccountDefault | |
55 | ||
56 | if (!video) videoComment = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(uri, t) | |
57 | if (!videoComment) flaggedAccount = await AccountModel.loadByUrl(uri, t) | |
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 | } | |
70 | ||
71 | if (video) { | |
72 | return createVideoAbuse({ | |
73 | baseAbuse, | |
74 | startAt, | |
75 | endAt, | |
76 | reporterAccount, | |
77 | transaction: t, | |
78 | videoInstance: video, | |
79 | skipNotification: false | |
80 | }) | |
81 | } | |
82 | ||
83 | if (videoComment) { | |
84 | return createVideoCommentAbuse({ | |
85 | baseAbuse, | |
86 | reporterAccount, | |
87 | transaction: t, | |
88 | commentInstance: videoComment, | |
89 | skipNotification: false | |
90 | }) | |
91 | } | |
92 | ||
93 | return await createAccountAbuse({ | |
94 | baseAbuse, | |
95 | reporterAccount, | |
96 | transaction: t, | |
97 | accountInstance: flaggedAccount, | |
98 | skipNotification: false | |
99 | }) | |
100 | }) | |
101 | } catch (err) { | |
102 | logger.debug('Cannot process report of %s', getAPId(object), { err }) | |
103 | } | |
104 | } | |
105 | } |