]>
Commit | Line | Data |
---|---|---|
848f499d C |
1 | import { ActivityCreate, ActivityFlag, VideoAbuseState } from '../../../../shared' |
2 | import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects' | |
3 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
4 | import { logger } from '../../../helpers/logger' | |
5 | import { sequelizeTypescript } from '../../../initializers' | |
848f499d C |
6 | import { VideoAbuseModel } from '../../../models/video/video-abuse' |
7 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' | |
8 | import { Notifier } from '../../notifier' | |
9 | import { getAPId } from '../../../helpers/activitypub' | |
1198edf4 | 10 | import { APProcessorOptions } from '../../../typings/activitypub-processor.model' |
5224c394 | 11 | import { SignatureActorModel } from '../../../typings/models' |
848f499d | 12 | |
1198edf4 C |
13 | async function processFlagActivity (options: APProcessorOptions<ActivityCreate | ActivityFlag>) { |
14 | const { activity, byActor } = options | |
848f499d C |
15 | return retryTransactionWrapper(processCreateVideoAbuse, activity, byActor) |
16 | } | |
17 | ||
18 | // --------------------------------------------------------------------------- | |
19 | ||
20 | export { | |
21 | processFlagActivity | |
22 | } | |
23 | ||
24 | // --------------------------------------------------------------------------- | |
25 | ||
5224c394 | 26 | async function processCreateVideoAbuse (activity: ActivityCreate | ActivityFlag, byActor: SignatureActorModel) { |
848f499d C |
27 | const flag = activity.type === 'Flag' ? activity : (activity.object as VideoAbuseObject) |
28 | ||
29 | logger.debug('Reporting remote abuse for video %s.', getAPId(flag.object)) | |
30 | ||
31 | const account = byActor.Account | |
d89954ff | 32 | if (!account) throw new Error('Cannot create video abuse with the non account actor ' + byActor.url) |
848f499d C |
33 | |
34 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: flag.object }) | |
35 | ||
2284f202 | 36 | const videoAbuse = await sequelizeTypescript.transaction(async t => { |
848f499d C |
37 | const videoAbuseData = { |
38 | reporterAccountId: account.id, | |
39 | reason: flag.content, | |
40 | videoId: video.id, | |
41 | state: VideoAbuseState.PENDING | |
42 | } | |
43 | ||
44 | const videoAbuseInstance = await VideoAbuseModel.create(videoAbuseData, { transaction: t }) | |
45 | videoAbuseInstance.Video = video | |
46 | ||
848f499d | 47 | logger.info('Remote abuse for video uuid %s created', flag.object) |
2284f202 C |
48 | |
49 | return videoAbuseInstance | |
848f499d | 50 | }) |
2284f202 C |
51 | |
52 | Notifier.Instance.notifyOnNewVideoAbuse(videoAbuse) | |
848f499d | 53 | } |