]>
Commit | Line | Data |
---|---|---|
1 | import { ActivityCreate, VideoChannelObject } from '../../../../shared' | |
2 | import { VideoAbuseObject } from '../../../../shared/models/activitypub/objects/video-abuse-object' | |
3 | import { logger, retryTransactionWrapper } from '../../../helpers' | |
4 | import { database as db } from '../../../initializers' | |
5 | import { AccountInstance } from '../../../models/account/account-interface' | |
6 | import { getOrCreateAccount } from '../account' | |
7 | import { getVideoChannelActivityPubUrl } from '../url' | |
8 | import { videoChannelActivityObjectToDBAttributes } from './misc' | |
9 | ||
10 | async function processCreateActivity (activity: ActivityCreate) { | |
11 | const activityObject = activity.object | |
12 | const activityType = activityObject.type | |
13 | const account = await getOrCreateAccount(activity.actor) | |
14 | ||
15 | if (activityType === 'VideoChannel') { | |
16 | return processCreateVideoChannel(account, activityObject as VideoChannelObject) | |
17 | } else if (activityType === 'Flag') { | |
18 | return processCreateVideoAbuse(account, activityObject as VideoAbuseObject) | |
19 | } | |
20 | ||
21 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | |
22 | return Promise.resolve(undefined) | |
23 | } | |
24 | ||
25 | // --------------------------------------------------------------------------- | |
26 | ||
27 | export { | |
28 | processCreateActivity | |
29 | } | |
30 | ||
31 | // --------------------------------------------------------------------------- | |
32 | ||
33 | function processCreateVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) { | |
34 | const options = { | |
35 | arguments: [ account, videoChannelToCreateData ], | |
36 | errorMessage: 'Cannot insert the remote video channel with many retries.' | |
37 | } | |
38 | ||
39 | return retryTransactionWrapper(addRemoteVideoChannel, options) | |
40 | } | |
41 | ||
42 | function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) { | |
43 | logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid) | |
44 | ||
45 | return db.sequelize.transaction(async t => { | |
46 | let videoChannel = await db.VideoChannel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t) | |
47 | if (videoChannel) throw new Error('Video channel with this URL/UUID already exists.') | |
48 | ||
49 | const videoChannelData = videoChannelActivityObjectToDBAttributes(videoChannelToCreateData, account) | |
50 | videoChannel = db.VideoChannel.build(videoChannelData) | |
51 | videoChannel.url = getVideoChannelActivityPubUrl(videoChannel) | |
52 | ||
53 | videoChannel = await videoChannel.save({ transaction: t }) | |
54 | logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid) | |
55 | ||
56 | return videoChannel | |
57 | }) | |
58 | } | |
59 | ||
60 | function processCreateVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) { | |
61 | const options = { | |
62 | arguments: [ account, videoAbuseToCreateData ], | |
63 | errorMessage: 'Cannot insert the remote video abuse with many retries.' | |
64 | } | |
65 | ||
66 | return retryTransactionWrapper(addRemoteVideoAbuse, options) | |
67 | } | |
68 | ||
69 | function addRemoteVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) { | |
70 | logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) | |
71 | ||
72 | return db.sequelize.transaction(async t => { | |
73 | const video = await db.Video.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t) | |
74 | if (!video) { | |
75 | logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object) | |
76 | return undefined | |
77 | } | |
78 | ||
79 | const videoAbuseData = { | |
80 | reporterAccountId: account.id, | |
81 | reason: videoAbuseToCreateData.content, | |
82 | videoId: video.id | |
83 | } | |
84 | ||
85 | await db.VideoAbuse.create(videoAbuseData) | |
86 | ||
87 | logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) | |
88 | }) | |
89 | } |