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