2017-11-27 07:44:51 -06:00
|
|
|
import { ActivityAdd, ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub/activity'
|
|
|
|
import { retryTransactionWrapper } from '../../../helpers/database-utils'
|
2017-11-20 02:43:39 -06:00
|
|
|
import { logger } from '../../../helpers/logger'
|
|
|
|
import { database as db } from '../../../initializers/index'
|
2017-11-27 07:44:51 -06:00
|
|
|
import { AccountInstance } from '../../../models/account/account-interface'
|
2017-11-20 02:43:39 -06:00
|
|
|
import { VideoInstance } from '../../../models/index'
|
|
|
|
import { VideoChannelInstance } from '../../../models/video/video-channel-interface'
|
2017-11-27 07:44:51 -06:00
|
|
|
import { getOrCreateAccountAndServer } from '../account'
|
|
|
|
import { forwardActivity } from '../send/misc'
|
2017-11-17 08:52:26 -06:00
|
|
|
import { processAddActivity } from './process-add'
|
|
|
|
import { processCreateActivity } from './process-create'
|
2017-11-15 10:56:21 -06:00
|
|
|
|
|
|
|
async function processAnnounceActivity (activity: ActivityAnnounce) {
|
2017-11-16 08:22:39 -06:00
|
|
|
const announcedActivity = activity.object
|
2017-11-21 06:43:29 -06:00
|
|
|
const accountAnnouncer = await getOrCreateAccountAndServer(activity.actor)
|
2017-11-15 10:56:21 -06:00
|
|
|
|
2017-11-16 08:22:39 -06:00
|
|
|
if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'VideoChannel') {
|
2017-11-27 07:44:51 -06:00
|
|
|
return processVideoChannelShare(accountAnnouncer, activity)
|
2017-11-16 08:22:39 -06:00
|
|
|
} else if (announcedActivity.type === 'Add' && announcedActivity.object.type === 'Video') {
|
2017-11-27 07:44:51 -06:00
|
|
|
return processVideoShare(accountAnnouncer, activity)
|
2017-11-15 10:56:21 -06:00
|
|
|
}
|
|
|
|
|
2017-11-16 08:22:39 -06:00
|
|
|
logger.warn(
|
|
|
|
'Unknown activity object type %s -> %s when announcing activity.', announcedActivity.type, announcedActivity.object.type,
|
|
|
|
{ activity: activity.id }
|
|
|
|
)
|
2017-11-20 02:43:39 -06:00
|
|
|
|
|
|
|
return undefined
|
2017-11-15 10:56:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processAnnounceActivity
|
|
|
|
}
|
2017-11-27 07:44:51 -06:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function processVideoChannelShare (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) {
|
|
|
|
const options = {
|
|
|
|
arguments: [ accountAnnouncer, activity ],
|
|
|
|
errorMessage: 'Cannot share the video channel with many retries.'
|
|
|
|
}
|
|
|
|
|
|
|
|
return retryTransactionWrapper(shareVideoChannel, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function shareVideoChannel (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) {
|
|
|
|
const announcedActivity = activity.object as ActivityCreate
|
|
|
|
|
|
|
|
return db.sequelize.transaction(async t => {
|
|
|
|
// Add share entry
|
|
|
|
const videoChannel: VideoChannelInstance = await processCreateActivity(announcedActivity)
|
|
|
|
const share = {
|
|
|
|
accountId: accountAnnouncer.id,
|
|
|
|
videoChannelId: videoChannel.id
|
|
|
|
}
|
|
|
|
|
|
|
|
const [ , created ] = await db.VideoChannelShare.findOrCreate({
|
|
|
|
where: share,
|
|
|
|
defaults: share,
|
|
|
|
transaction: t
|
|
|
|
})
|
|
|
|
|
|
|
|
if (videoChannel.isOwned() && created === true) {
|
|
|
|
// Don't resend the activity to the sender
|
|
|
|
const exceptions = [ accountAnnouncer ]
|
|
|
|
await forwardActivity(activity, t, exceptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function processVideoShare (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) {
|
|
|
|
const options = {
|
|
|
|
arguments: [ accountAnnouncer, activity ],
|
|
|
|
errorMessage: 'Cannot share the video with many retries.'
|
|
|
|
}
|
|
|
|
|
|
|
|
return retryTransactionWrapper(shareVideo, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
function shareVideo (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) {
|
|
|
|
const announcedActivity = activity.object as ActivityAdd
|
|
|
|
|
|
|
|
return db.sequelize.transaction(async t => {
|
|
|
|
// Add share entry
|
|
|
|
const video: VideoInstance = await processAddActivity(announcedActivity)
|
|
|
|
|
|
|
|
const share = {
|
|
|
|
accountId: accountAnnouncer.id,
|
|
|
|
videoId: video.id
|
|
|
|
}
|
|
|
|
|
|
|
|
const [ , created ] = await db.VideoShare.findOrCreate({
|
|
|
|
where: share,
|
|
|
|
defaults: share,
|
|
|
|
transaction: t
|
|
|
|
})
|
|
|
|
|
|
|
|
if (video.isOwned() && created === true) {
|
|
|
|
// Don't resend the activity to the sender
|
|
|
|
const exceptions = [ accountAnnouncer ]
|
|
|
|
await forwardActivity(activity, t, exceptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
})
|
|
|
|
}
|