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

112 lines
3.7 KiB
TypeScript
Raw Normal View History

2017-12-12 10:53:50 -06: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 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') {
return processVideoChannelShare(accountAnnouncer, activity)
2017-11-16 08:22:39 -06:00
} else if (announcedActivity.type === 'Add' && announcedActivity.object.type === 'Video') {
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-12-12 10:53:50 -06: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 10:53:50 -06:00
async function shareVideoChannel (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
const announcedActivity = activity.object as ActivityCreate
2017-12-12 10:53:50 -06:00
return sequelizeTypescript.transaction(async t => {
// Add share entry
2017-12-12 10:53:50 -06:00
const videoChannel: VideoChannelModel = await processCreateActivity(announcedActivity)
const share = {
accountId: accountAnnouncer.id,
videoChannelId: videoChannel.id
}
2017-12-12 10:53:50 -06: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 10:53:50 -06: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 10:53:50 -06:00
function shareVideo (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
const announcedActivity = activity.object as ActivityAdd
2017-12-12 10:53:50 -06:00
return sequelizeTypescript.transaction(async t => {
// Add share entry
2017-12-12 10:53:50 -06:00
const video: VideoModel = await processAddActivity(announcedActivity)
const share = {
accountId: accountAnnouncer.id,
videoId: video.id
}
2017-12-12 10:53:50 -06: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
})
}