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

76 lines
2.5 KiB
TypeScript
Raw Normal View History

import { getAPId } from '@server/lib/activitypub/activity'
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'
import { logger } from '../../../helpers/logger'
2020-05-07 14:58:24 +02:00
import { sequelizeTypescript } from '../../../initializers/database'
2017-12-12 17:53:50 +01:00
import { VideoShareModel } from '../../../models/video/video-share'
2020-06-18 10:45:25 +02:00
import { APProcessorOptions } from '../../../types/activitypub-processor.model'
import { MActorSignature, MVideoAccountLightBlacklistAllFiles } from '../../../types/models'
import { Notifier } from '../../notifier'
import { forwardVideoRelatedActivity } from '../send/shared/send-utils'
import { getOrCreateAPVideo } from '../videos'
2017-11-15 17:56:21 +01:00
2019-08-02 10:53:36 +02: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
2021-11-16 10:05:12 +01:00
// Announces on accounts are not supported
if (actorAnnouncer.type !== 'Application' && actorAnnouncer.type !== 'Group') return
2019-08-02 10:53:36 +02:00
return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity, notify)
2017-11-15 17:56:21 +01:00
}
// ---------------------------------------------------------------------------
export {
processAnnounceActivity
}
// ---------------------------------------------------------------------------
2019-08-15 11:53:26 +02:00
async function processVideoShare (actorAnnouncer: MActorSignature, activity: ActivityAnnounce, notify: boolean) {
2021-11-16 10:05:12 +01:00
const objectUri = getAPId(activity.object)
2018-01-10 17:18:12 +01:00
2019-08-20 13:52:49 +02:00
let video: MVideoAccountLightBlacklistAllFiles
let videoCreated: boolean
try {
2021-06-02 15:47:05 +02:00
const result = await getOrCreateAPVideo({ 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 10:36:24 +01:00
await 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
})
2018-12-26 10:36:24 +01:00
2019-08-02 10:53:36 +02:00
if (videoCreated && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
}