2017-11-15 09:12:23 -05:00
|
|
|
import { ActivityCreate, VideoChannelObject } from '../../../shared'
|
|
|
|
import { VideoAbuseObject } from '../../../shared/models/activitypub/objects/video-abuse-object'
|
|
|
|
import { logger, retryTransactionWrapper } from '../../helpers'
|
|
|
|
import { getActivityPubUrl, getOrCreateAccount } from '../../helpers/activitypub'
|
2017-11-09 11:51:58 -05:00
|
|
|
import { database as db } from '../../initializers'
|
2017-11-10 08:34:45 -05:00
|
|
|
import { AccountInstance } from '../../models/account/account-interface'
|
2017-11-09 11:51:58 -05:00
|
|
|
|
2017-11-10 08:34:45 -05:00
|
|
|
async function processCreateActivity (activity: ActivityCreate) {
|
2017-11-09 11:51:58 -05:00
|
|
|
const activityObject = activity.object
|
|
|
|
const activityType = activityObject.type
|
2017-11-10 08:34:45 -05:00
|
|
|
const account = await getOrCreateAccount(activity.actor)
|
2017-11-09 11:51:58 -05:00
|
|
|
|
2017-11-10 08:34:45 -05:00
|
|
|
if (activityType === 'VideoChannel') {
|
|
|
|
return processCreateVideoChannel(account, activityObject as VideoChannelObject)
|
2017-11-15 09:12:23 -05:00
|
|
|
} else if (activityType === 'Flag') {
|
|
|
|
return processCreateVideoAbuse(account, activityObject as VideoAbuseObject)
|
2017-11-09 11:51:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
|
2017-11-10 08:34:45 -05:00
|
|
|
return Promise.resolve(undefined)
|
2017-11-09 11:51:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processCreateActivity
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-11-10 08:34:45 -05:00
|
|
|
function processCreateVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) {
|
2017-11-09 11:51:58 -05:00
|
|
|
const options = {
|
2017-11-10 08:34:45 -05:00
|
|
|
arguments: [ account, videoChannelToCreateData ],
|
|
|
|
errorMessage: 'Cannot insert the remote video channel with many retries.'
|
2017-11-09 11:51:58 -05:00
|
|
|
}
|
|
|
|
|
2017-11-10 08:34:45 -05:00
|
|
|
return retryTransactionWrapper(addRemoteVideoChannel, options)
|
2017-11-09 11:51:58 -05:00
|
|
|
}
|
|
|
|
|
2017-11-10 08:34:45 -05:00
|
|
|
async function addRemoteVideoChannel (account: AccountInstance, videoChannelToCreateData: VideoChannelObject) {
|
|
|
|
logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid)
|
2017-11-09 11:51:58 -05:00
|
|
|
|
|
|
|
await db.sequelize.transaction(async t => {
|
2017-11-10 08:34:45 -05:00
|
|
|
let videoChannel = await db.VideoChannel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t)
|
|
|
|
if (videoChannel) throw new Error('Video channel with this URL/UUID already exists.')
|
|
|
|
|
|
|
|
const videoChannelData = {
|
|
|
|
name: videoChannelToCreateData.name,
|
|
|
|
description: videoChannelToCreateData.content,
|
|
|
|
uuid: videoChannelToCreateData.uuid,
|
|
|
|
createdAt: videoChannelToCreateData.published,
|
|
|
|
updatedAt: videoChannelToCreateData.updated,
|
2017-11-09 11:51:58 -05:00
|
|
|
remote: true,
|
2017-11-10 08:34:45 -05:00
|
|
|
accountId: account.id
|
2017-11-09 11:51:58 -05:00
|
|
|
}
|
|
|
|
|
2017-11-10 08:34:45 -05:00
|
|
|
videoChannel = db.VideoChannel.build(videoChannelData)
|
|
|
|
videoChannel.url = getActivityPubUrl('videoChannel', videoChannel.uuid)
|
2017-11-09 11:51:58 -05:00
|
|
|
|
2017-11-10 08:34:45 -05:00
|
|
|
await videoChannel.save({ transaction: t })
|
2017-11-09 11:51:58 -05:00
|
|
|
})
|
|
|
|
|
2017-11-10 08:34:45 -05:00
|
|
|
logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid)
|
2017-11-09 11:51:58 -05:00
|
|
|
}
|
2017-11-15 09:12:23 -05:00
|
|
|
|
|
|
|
function processCreateVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) {
|
|
|
|
const options = {
|
|
|
|
arguments: [ account, videoAbuseToCreateData ],
|
|
|
|
errorMessage: 'Cannot insert the remote video abuse with many retries.'
|
|
|
|
}
|
|
|
|
|
|
|
|
return retryTransactionWrapper(addRemoteVideoAbuse, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addRemoteVideoAbuse (account: AccountInstance, videoAbuseToCreateData: VideoAbuseObject) {
|
|
|
|
logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
|
|
|
|
|
|
|
|
return db.sequelize.transaction(async t => {
|
|
|
|
const video = await db.Video.loadByUrl(videoAbuseToCreateData.object, t)
|
|
|
|
if (!video) {
|
|
|
|
logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const videoAbuseData = {
|
|
|
|
reporterAccountId: account.id,
|
|
|
|
reason: videoAbuseToCreateData.content,
|
|
|
|
videoId: video.id
|
|
|
|
}
|
|
|
|
|
|
|
|
await db.VideoAbuse.create(videoAbuseData)
|
|
|
|
|
|
|
|
logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
|
|
|
|
})
|
|
|
|
}
|