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