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