]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-flag.ts
Add check constraints live tests
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-flag.ts
CommitLineData
d95d1559
C
1import { createAccountAbuse, createVideoAbuse, createVideoCommentAbuse } from '@server/lib/moderation'
2import { AccountModel } from '@server/models/account/account'
3import { VideoModel } from '@server/models/video/video'
4import { VideoCommentModel } from '@server/models/video/video-comment'
bd45d503
C
5import { abusePredefinedReasonsMap } from '@shared/core-utils/abuse'
6import { AbuseObject, AbuseState, ActivityCreate, ActivityFlag } from '../../../../shared'
d95d1559 7import { getAPId } from '../../../helpers/activitypub'
848f499d
C
8import { retryTransactionWrapper } from '../../../helpers/database-utils'
9import { logger } from '../../../helpers/logger'
80fdaf06 10import { sequelizeTypescript } from '../../../initializers/database'
26d6bf65 11import { APProcessorOptions } from '../../../types/activitypub-processor.model'
d95d1559 12import { MAccountDefault, MActorSignature, MCommentOwnerVideo } from '../../../types/models'
848f499d 13
1198edf4
C
14async 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
22export {
23 processFlagActivity
24}
25
26// ---------------------------------------------------------------------------
27
d95d1559
C
28async 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 => {
2284f202 52
d95d1559
C
53 const video = await VideoModel.loadByUrlAndPopulateAccount(uri)
54 let videoComment: MCommentOwnerVideo
55 let flaggedAccount: MAccountDefault
56
57 if (!video) videoComment = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(uri)
58 if (!videoComment) flaggedAccount = await AccountModel.loadByUrl(uri)
59
60 if (!video && !videoComment && !flaggedAccount) {
61 logger.warn('Cannot flag unknown entity %s.', object)
62 return
63 }
64
65 const baseAbuse = {
66 reporterAccountId: reporterAccount.id,
67 reason: flag.content,
68 state: AbuseState.PENDING,
69 predefinedReasons
70 }
0b5c385b 71
d95d1559
C
72 if (video) {
73 return createVideoAbuse({
74 baseAbuse,
75 startAt,
76 endAt,
77 reporterAccount,
78 transaction: t,
79 videoInstance: video
80 })
81 }
82
83 if (videoComment) {
84 return createVideoCommentAbuse({
85 baseAbuse,
86 reporterAccount,
87 transaction: t,
88 commentInstance: videoComment
89 })
90 }
df4c603d 91
d95d1559
C
92 return await createAccountAbuse({
93 baseAbuse,
94 reporterAccount,
95 transaction: t,
96 accountInstance: flaggedAccount
97 })
df4c603d 98 })
0b5c385b 99 } catch (err) {
d95d1559 100 logger.debug('Cannot process report of %s', getAPId(object), { err })
0b5c385b
C
101 }
102 }
848f499d 103}