]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/process/process-flag.ts
Add last login date to users
[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'
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, MVideoAbuseVideo } from '../../../typings/models'
12
13async function processFlagActivity (options: APProcessorOptions<ActivityCreate | ActivityFlag>) {
14 const { activity, byActor } = options
15 return retryTransactionWrapper(processCreateVideoAbuse, activity, byActor)
16}
17
18// ---------------------------------------------------------------------------
19
20export {
21 processFlagActivity
22}
23
24// ---------------------------------------------------------------------------
25
26async function processCreateVideoAbuse (activity: ActivityCreate | ActivityFlag, byActor: MActorSignature) {
27 const flag = activity.type === 'Flag' ? activity : (activity.object as VideoAbuseObject)
28
29 const account = byActor.Account
30 if (!account) throw new Error('Cannot create video abuse with the non account actor ' + byActor.url)
31
32 const objects = Array.isArray(flag.object) ? flag.object : [ flag.object ]
33
34 for (const object of objects) {
35 try {
36 logger.debug('Reporting remote abuse for video %s.', getAPId(object))
37
38 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: object })
39
40 const videoAbuse = await sequelizeTypescript.transaction(async t => {
41 const videoAbuseData = {
42 reporterAccountId: account.id,
43 reason: flag.content,
44 videoId: video.id,
45 state: VideoAbuseState.PENDING
46 }
47
48 const videoAbuseInstance = await VideoAbuseModel.create(videoAbuseData, { transaction: t }) as MVideoAbuseVideo
49 videoAbuseInstance.Video = video
50
51 logger.info('Remote abuse for video uuid %s created', flag.object)
52
53 return videoAbuseInstance
54 })
55
56 Notifier.Instance.notifyOnNewVideoAbuse(videoAbuse)
57 } catch (err) {
58 logger.debug('Cannot process report of %s. (Maybe not a video abuse).', getAPId(object), { err })
59 }
60 }
61}