PeerTube/server/lib/activitypub/process/process-announce.ts

112 lines
3.7 KiB
TypeScript
Raw Normal View History

2017-12-12 17:53:50 +01:00
import { ActivityAdd, ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub'
import { logger, retryTransactionWrapper } from '../../../helpers'
import { sequelizeTypescript } from '../../../initializers'
import { AccountModel } from '../../../models/account/account'
import { VideoModel } from '../../../models/video/video'
import { VideoChannelModel } from '../../../models/video/video-channel'
import { VideoChannelShareModel } from '../../../models/video/video-channel-share'
import { VideoShareModel } from '../../../models/video/video-share'
import { getOrCreateAccountAndServer } from '../account'
import { forwardActivity } from '../send/misc'
2017-11-17 15:52:26 +01:00
import { processAddActivity } from './process-add'
import { processCreateActivity } from './process-create'
2017-11-15 17:56:21 +01:00
async function processAnnounceActivity (activity: ActivityAnnounce) {
2017-11-16 15:22:39 +01:00
const announcedActivity = activity.object
2017-11-21 13:43:29 +01:00
const accountAnnouncer = await getOrCreateAccountAndServer(activity.actor)
2017-11-15 17:56:21 +01:00
2017-11-16 15:22:39 +01:00
if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'VideoChannel') {
return processVideoChannelShare(accountAnnouncer, activity)
2017-11-16 15:22:39 +01:00
} else if (announcedActivity.type === 'Add' && announcedActivity.object.type === 'Video') {
return processVideoShare(accountAnnouncer, activity)
2017-11-15 17:56:21 +01:00
}
2017-11-16 15:22:39 +01:00
logger.warn(
'Unknown activity object type %s -> %s when announcing activity.', announcedActivity.type, announcedActivity.object.type,
{ activity: activity.id }
)
2017-11-20 09:43:39 +01:00
return undefined
2017-11-15 17:56:21 +01:00
}
// ---------------------------------------------------------------------------
export {
processAnnounceActivity
}
// ---------------------------------------------------------------------------
2017-12-12 17:53:50 +01:00
function processVideoChannelShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
const options = {
arguments: [ accountAnnouncer, activity ],
errorMessage: 'Cannot share the video channel with many retries.'
}
return retryTransactionWrapper(shareVideoChannel, options)
}
2017-12-12 17:53:50 +01:00
async function shareVideoChannel (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
const announcedActivity = activity.object as ActivityCreate
2017-12-12 17:53:50 +01:00
return sequelizeTypescript.transaction(async t => {
// Add share entry
2017-12-12 17:53:50 +01:00
const videoChannel: VideoChannelModel = await processCreateActivity(announcedActivity)
const share = {
accountId: accountAnnouncer.id,
videoChannelId: videoChannel.id
}
2017-12-12 17:53:50 +01:00
const [ , created ] = await VideoChannelShareModel.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
})
}
2017-12-12 17:53:50 +01:00
function processVideoShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
const options = {
arguments: [ accountAnnouncer, activity ],
errorMessage: 'Cannot share the video with many retries.'
}
return retryTransactionWrapper(shareVideo, options)
}
2017-12-12 17:53:50 +01:00
function shareVideo (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
const announcedActivity = activity.object as ActivityAdd
2017-12-12 17:53:50 +01:00
return sequelizeTypescript.transaction(async t => {
// Add share entry
2017-12-12 17:53:50 +01:00
const video: VideoModel = await processAddActivity(announcedActivity)
const share = {
accountId: accountAnnouncer.id,
videoId: video.id
}
2017-12-12 17:53:50 +01:00
const [ , created ] = await VideoShareModel.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
})
}