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