PeerTube/server/lib/activitypub/process/process-flag.ts

62 lines
2.5 KiB
TypeScript
Raw Normal View History

import { ActivityCreate, ActivityFlag, VideoAbuseState } from '../../../../shared'
import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects'
import { retryTransactionWrapper } from '../../../helpers/database-utils'
import { logger } from '../../../helpers/logger'
import { sequelizeTypescript } from '../../../initializers'
import { VideoAbuseModel } from '../../../models/video/video-abuse'
import { getOrCreateVideoAndAccountAndChannel } from '../videos'
import { Notifier } from '../../notifier'
import { getAPId } from '../../../helpers/activitypub'
2019-08-02 10:53:36 +02:00
import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
2019-08-15 11:53:26 +02:00
import { MActorSignature, MVideoAbuseVideo } from '../../../typings/models'
2019-08-02 10:53:36 +02:00
async function processFlagActivity (options: APProcessorOptions<ActivityCreate | ActivityFlag>) {
const { activity, byActor } = options
return retryTransactionWrapper(processCreateVideoAbuse, activity, byActor)
}
// ---------------------------------------------------------------------------
export {
processFlagActivity
}
// ---------------------------------------------------------------------------
2019-08-15 11:53:26 +02:00
async function processCreateVideoAbuse (activity: ActivityCreate | ActivityFlag, byActor: MActorSignature) {
const flag = activity.type === 'Flag' ? activity : (activity.object as VideoAbuseObject)
const account = byActor.Account
2019-07-02 11:21:14 +02:00
if (!account) throw new Error('Cannot create video abuse with the non account actor ' + byActor.url)
2019-08-30 09:40:21 +02:00
const objects = Array.isArray(flag.object) ? flag.object : [ flag.object ]
2019-08-30 09:40:21 +02:00
for (const object of objects) {
try {
logger.debug('Reporting remote abuse for video %s.', getAPId(object))
const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: object })
2019-08-30 09:40:21 +02:00
const videoAbuse = await sequelizeTypescript.transaction(async t => {
const videoAbuseData = {
reporterAccountId: account.id,
reason: flag.content,
videoId: video.id,
state: VideoAbuseState.PENDING
}
2019-08-30 09:40:21 +02:00
const videoAbuseInstance = await VideoAbuseModel.create(videoAbuseData, { transaction: t }) as MVideoAbuseVideo
videoAbuseInstance.Video = video
2019-07-29 11:59:29 +02:00
2019-08-30 09:40:21 +02:00
logger.info('Remote abuse for video uuid %s created', flag.object)
2019-07-29 11:59:29 +02:00
2019-08-30 09:40:21 +02:00
return videoAbuseInstance
})
Notifier.Instance.notifyOnNewVideoAbuse(videoAbuse)
} catch (err) {
logger.debug('Cannot process report of %s. (Maybe not a video abuse).', getAPId(object), { err })
}
}
}