]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-flag.ts
Remove uneccessary details to link titles
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-flag.ts
CommitLineData
848f499d
C
1import { ActivityCreate, ActivityFlag, VideoAbuseState } from '../../../../shared'
2import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects'
3import { retryTransactionWrapper } from '../../../helpers/database-utils'
4import { logger } from '../../../helpers/logger'
80fdaf06 5import { sequelizeTypescript } from '../../../initializers/database'
848f499d
C
6import { VideoAbuseModel } from '../../../models/video/video-abuse'
7import { getOrCreateVideoAndAccountAndChannel } from '../videos'
8import { Notifier } from '../../notifier'
9import { getAPId } from '../../../helpers/activitypub'
1198edf4 10import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
df4c603d
RK
11import { MActorSignature, MVideoAbuseAccountVideo } from '../../../typings/models'
12import { AccountModel } from '@server/models/account/account'
848f499d 13
1198edf4
C
14async function processFlagActivity (options: APProcessorOptions<ActivityCreate | ActivityFlag>) {
15 const { activity, byActor } = options
848f499d
C
16 return retryTransactionWrapper(processCreateVideoAbuse, activity, byActor)
17}
18
19// ---------------------------------------------------------------------------
20
21export {
22 processFlagActivity
23}
24
25// ---------------------------------------------------------------------------
26
453e83ea 27async function processCreateVideoAbuse (activity: ActivityCreate | ActivityFlag, byActor: MActorSignature) {
848f499d
C
28 const flag = activity.type === 'Flag' ? activity : (activity.object as VideoAbuseObject)
29
848f499d 30 const account = byActor.Account
d89954ff 31 if (!account) throw new Error('Cannot create video abuse with the non account actor ' + byActor.url)
848f499d 32
0b5c385b 33 const objects = Array.isArray(flag.object) ? flag.object : [ flag.object ]
848f499d 34
0b5c385b
C
35 for (const object of objects) {
36 try {
37 logger.debug('Reporting remote abuse for video %s.', getAPId(object))
38
39 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: object })
df4c603d 40 const reporterAccount = await sequelizeTypescript.transaction(async t => AccountModel.load(account.id, t))
848f499d 41
df4c603d 42 const videoAbuseInstance = await sequelizeTypescript.transaction(async t => {
0b5c385b
C
43 const videoAbuseData = {
44 reporterAccountId: account.id,
45 reason: flag.content,
46 videoId: video.id,
47 state: VideoAbuseState.PENDING
48 }
848f499d 49
df4c603d 50 const videoAbuseInstance: MVideoAbuseAccountVideo = await VideoAbuseModel.create(videoAbuseData, { transaction: t })
0b5c385b 51 videoAbuseInstance.Video = video
df4c603d 52 videoAbuseInstance.Account = reporterAccount
2284f202 53
0b5c385b 54 logger.info('Remote abuse for video uuid %s created', flag.object)
2284f202 55
0b5c385b
C
56 return videoAbuseInstance
57 })
58
df4c603d
RK
59 const videoAbuseJSON = videoAbuseInstance.toFormattedJSON()
60
61 Notifier.Instance.notifyOnNewVideoAbuse({
62 videoAbuse: videoAbuseJSON,
63 videoAbuseInstance,
64 reporter: reporterAccount.Actor.getIdentifier()
65 })
0b5c385b
C
66 } catch (err) {
67 logger.debug('Cannot process report of %s. (Maybe not a video abuse).', getAPId(object), { err })
68 }
69 }
848f499d 70}