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

72 lines
2.4 KiB
TypeScript
Raw Normal View History

2017-12-20 14:36:29 +00:00
import { ActivityAnnounce } from '../../../../shared/models/activitypub'
2017-12-28 10:16:08 +00:00
import { retryTransactionWrapper } from '../../../helpers/database-utils'
2020-05-07 12:58:24 +00:00
import { sequelizeTypescript } from '../../../initializers/database'
2017-12-12 16:53:50 +00:00
import { VideoShareModel } from '../../../models/video/video-share'
import { forwardVideoRelatedActivity } from '../send/utils'
2018-08-22 14:15:35 +00:00
import { getOrCreateVideoAndAccountAndChannel } from '../videos'
2018-12-26 09:36:24 +00:00
import { Notifier } from '../../notifier'
import { logger } from '../../../helpers/logger'
2020-06-18 08:45:25 +00:00
import { APProcessorOptions } from '../../../types/activitypub-processor.model'
import { MActorSignature, MVideoAccountLightBlacklistAllFiles } from '../../../types/models'
2017-11-15 16:56:21 +00:00
2019-08-02 08:53:36 +00:00
async function processAnnounceActivity (options: APProcessorOptions<ActivityAnnounce>) {
const { activity, byActor: actorAnnouncer } = options
// Only notify if it is not from a fetcher job
const notify = options.fromFetch !== true
return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity, notify)
2017-11-15 16:56:21 +00:00
}
// ---------------------------------------------------------------------------
export {
processAnnounceActivity
}
// ---------------------------------------------------------------------------
2019-08-15 09:53:26 +00:00
async function processVideoShare (actorAnnouncer: MActorSignature, activity: ActivityAnnounce, notify: boolean) {
2018-01-26 11:02:18 +00:00
const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id
2018-01-10 16:18:12 +00:00
2019-08-20 11:52:49 +00:00
let video: MVideoAccountLightBlacklistAllFiles
let videoCreated: boolean
try {
const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: objectUri })
video = result.video
videoCreated = result.created
} catch (err) {
logger.debug('Cannot process share of %s. Maybe this is not a video object, so just skipping.', objectUri, { err })
return
}
2018-12-26 09:36:24 +00:00
await sequelizeTypescript.transaction(async t => {
// Add share entry
const share = {
2017-12-14 16:38:41 +00:00
actorId: actorAnnouncer.id,
videoId: video.id,
url: activity.id
}
2017-12-12 16:53:50 +00: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 16:38:41 +00:00
const exceptions = [ actorAnnouncer ]
await forwardVideoRelatedActivity(activity, t, exceptions, video)
}
return undefined
})
2018-12-26 09:36:24 +00:00
2019-08-02 08:53:36 +00:00
if (videoCreated && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
}