]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-flag.ts
Use 3 tables to represent abuses
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-flag.ts
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'
7 import { retryTransactionWrapper } from '../../../helpers/database-utils'
8 import { logger } from '../../../helpers/logger'
9 import { sequelizeTypescript } from '../../../initializers/database'
10 import { APProcessorOptions } from '../../../types/activitypub-processor.model'
11 import { MAccountDefault, MActorSignature, MCommentOwnerVideo } from '../../../types/models'
12
13 async function processFlagActivity (options: APProcessorOptions<ActivityCreate | ActivityFlag>) {
14 const { activity, byActor } = options
15
16 return retryTransactionWrapper(processCreateAbuse, activity, byActor)
17 }
18
19 // ---------------------------------------------------------------------------
20
21 export {
22 processFlagActivity
23 }
24
25 // ---------------------------------------------------------------------------
26
27 async function processCreateAbuse (activity: ActivityCreate | ActivityFlag, byActor: MActorSignature) {
28 const flag = activity.type === 'Flag' ? activity : (activity.object as AbuseObject)
29
30 const account = byActor.Account
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)
34
35 const objects = Array.isArray(flag.object) ? flag.object : [ flag.object ]
36
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
44 for (const object of objects) {
45 try {
46 const uri = getAPId(object)
47
48 logger.debug('Reporting remote abuse for object %s.', uri)
49
50 await sequelizeTypescript.transaction(async t => {
51
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 }
70
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 }
90
91 return await createAccountAbuse({
92 baseAbuse,
93 reporterAccount,
94 transaction: t,
95 accountInstance: flaggedAccount
96 })
97 })
98 } catch (err) {
99 logger.debug('Cannot process report of %s', getAPId(object), { err })
100 }
101 }
102 }