1
0
Fork 0
peertube/server/lib/activitypub/process/process-announce.ts

80 lines
2.6 KiB
TypeScript
Raw Normal View History

2017-12-20 09:36:29 -05:00
import { ActivityAnnounce } from '../../../../shared/models/activitypub'
2017-12-12 11:53:50 -05:00
import { logger, retryTransactionWrapper } from '../../../helpers'
import { sequelizeTypescript } from '../../../initializers'
2017-12-14 11:38:41 -05:00
import { ActorModel } from '../../../models/activitypub/actor'
2017-12-12 11:53:50 -05:00
import { VideoModel } from '../../../models/video/video'
import { VideoShareModel } from '../../../models/video/video-share'
2017-12-14 11:38:41 -05:00
import { getOrCreateActorAndServerAndModel } from '../actor'
import { forwardActivity } from '../send/misc'
2017-11-17 09:52:26 -05:00
import { processCreateActivity } from './process-create'
2017-11-15 11:56:21 -05:00
async function processAnnounceActivity (activity: ActivityAnnounce) {
2017-11-16 09:22:39 -05:00
const announcedActivity = activity.object
2017-12-14 11:38:41 -05:00
const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor)
2017-11-15 11:56:21 -05:00
2017-12-19 11:07:58 -05:00
if (typeof announcedActivity === 'string') {
return processVideoShare(actorAnnouncer, activity)
} else if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'Video') {
2017-12-14 11:38:41 -05:00
return processVideoShare(actorAnnouncer, activity)
2017-11-15 11:56:21 -05:00
}
2017-11-16 09:22:39 -05:00
logger.warn(
'Unknown activity object type %s -> %s when announcing activity.', announcedActivity.type, announcedActivity.object.type,
{ activity: activity.id }
)
2017-11-20 03:43:39 -05:00
return undefined
2017-11-15 11:56:21 -05:00
}
// ---------------------------------------------------------------------------
export {
processAnnounceActivity
}
// ---------------------------------------------------------------------------
2017-12-14 11:38:41 -05:00
function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
const options = {
2017-12-14 11:38:41 -05:00
arguments: [ actorAnnouncer, activity ],
2017-12-19 11:07:58 -05:00
errorMessage: 'Cannot share the video activity with many retries.'
}
return retryTransactionWrapper(shareVideo, options)
}
2017-12-14 11:38:41 -05:00
function shareVideo (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
2017-12-19 11:07:58 -05:00
const announced = activity.object
2017-12-12 11:53:50 -05:00
return sequelizeTypescript.transaction(async t => {
// Add share entry
2017-12-19 11:07:58 -05:00
let video: VideoModel
if (typeof announced === 'string') {
video = await VideoModel.loadByUrlAndPopulateAccount(announced)
2017-12-19 11:07:58 -05:00
if (!video) throw new Error('Unknown video to share ' + announced)
} else {
video = await processCreateActivity(announced)
2017-12-19 11:07:58 -05:00
}
const share = {
2017-12-14 11:38:41 -05:00
actorId: actorAnnouncer.id,
videoId: video.id
}
2017-12-12 11:53:50 -05: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
2017-12-14 11:38:41 -05:00
const exceptions = [ actorAnnouncer ]
await forwardActivity(activity, t, exceptions)
}
return undefined
})
}