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 { getOrCreateAccountAndServer } from '../account'
7 import { getVideoChannelActivityPubUrl } from '../url'
8 import { videoChannelActivityObjectToDBAttributes } from './misc'
10 async function processCreateActivity (activity: ActivityCreate) {
11 const activityObject = activity.object
12 const activityType = activityObject.type
13 const account = await getOrCreateAccountAndServer(activity.actor)
15 if (activityType === 'VideoChannel') {
16 return processCreateVideoChannel(account, activityObject as VideoChannelObject)
17 } else if (activityType === 'Flag') {
18 return processCreateVideoAbuse(account, activityObject as VideoAbuseObject)
21 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
22 return Promise.resolve(undefined)
25 // ---------------------------------------------------------------------------
31 // ---------------------------------------------------------------------------
33 function processCreateVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) {
35 arguments: [ account, videoChannelToCreateData ],
36 errorMessage: 'Cannot insert the remote video channel with many retries.'
39 return retryTransactionWrapper(addRemoteVideoChannel, options)
42 function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) {
43 logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid)
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.')
49 const videoChannelData = videoChannelActivityObjectToDBAttributes(videoChannelToCreateData, account)
50 videoChannel = db.VideoChannel.build(videoChannelData)
51 videoChannel.url = getVideoChannelActivityPubUrl(videoChannel)
53 videoChannel = await videoChannel.save({ transaction: t })
54 logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid)
60 function processCreateVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) {
62 arguments: [ account, videoAbuseToCreateData ],
63 errorMessage: 'Cannot insert the remote video abuse with many retries.'
66 return retryTransactionWrapper(addRemoteVideoAbuse, options)
69 function addRemoteVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) {
70 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
72 return db.sequelize.transaction(async t => {
73 const video = await db.Video.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t)
75 logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object)
79 const videoAbuseData = {
80 reporterAccountId: account.id,
81 reason: videoAbuseToCreateData.content,
85 await db.VideoAbuse.create(videoAbuseData)
87 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)