]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-flag.ts
Add more AP stats to stats endpoint
[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 => {
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,
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}