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

68 lines
2.1 KiB
TypeScript
Raw Normal View History

2017-12-20 15:36:29 +01:00
import { ActivityAnnounce } from '../../../../shared/models/activitypub'
2017-12-28 11:16:08 +01:00
import { retryTransactionWrapper } from '../../../helpers/database-utils'
2017-12-12 17:53:50 +01:00
import { sequelizeTypescript } from '../../../initializers'
2017-12-14 17:38:41 +01:00
import { ActorModel } from '../../../models/activitypub/actor'
2017-12-12 17:53:50 +01:00
import { VideoModel } from '../../../models/video/video'
import { VideoShareModel } from '../../../models/video/video-share'
2017-12-14 17:38:41 +01:00
import { getOrCreateActorAndServerAndModel } from '../actor'
import { forwardVideoRelatedActivity } from '../send/utils'
2018-01-10 17:18:12 +01:00
import { getOrCreateAccountAndVideoAndChannel } from '../videos'
2017-11-15 17:56:21 +01:00
async function processAnnounceActivity (activity: ActivityAnnounce) {
2017-12-14 17:38:41 +01:00
const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor)
2017-11-15 17:56:21 +01:00
2018-01-26 12:02:18 +01:00
return processVideoShare(actorAnnouncer, activity)
2017-11-15 17:56:21 +01:00
}
// ---------------------------------------------------------------------------
export {
processAnnounceActivity
}
// ---------------------------------------------------------------------------
2017-12-14 17:38:41 +01:00
function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
const options = {
2017-12-14 17:38:41 +01:00
arguments: [ actorAnnouncer, activity ],
2017-12-19 17:07:58 +01:00
errorMessage: 'Cannot share the video activity with many retries.'
}
return retryTransactionWrapper(shareVideo, options)
}
2018-01-10 17:18:12 +01:00
async function shareVideo (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
2018-01-26 12:02:18 +01:00
const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id
2018-01-10 17:18:12 +01:00
let video: VideoModel
2018-01-26 12:02:18 +01:00
const res = await getOrCreateAccountAndVideoAndChannel(objectUri)
video = res.video
2017-12-12 17:53:50 +01:00
return sequelizeTypescript.transaction(async t => {
// Add share entry
const share = {
2017-12-14 17:38:41 +01:00
actorId: actorAnnouncer.id,
videoId: video.id,
url: activity.id
}
2017-12-12 17:53:50 +01:00
const [ , created ] = await VideoShareModel.findOrCreate({
where: {
url: activity.id
},
defaults: share,
transaction: t
})
if (video.isOwned() && created === true) {
// Don't resend the activity to the sender
2017-12-14 17:38:41 +01:00
const exceptions = [ actorAnnouncer ]
await forwardVideoRelatedActivity(activity, t, exceptions, video)
}
return undefined
})
}