1 import { ActivityAnnounce } from '../../../../shared/models/activitypub'
2 import { retryTransactionWrapper } from '../../../helpers/database-utils'
3 import { sequelizeTypescript } from '../../../initializers'
4 import { ActorModel } from '../../../models/activitypub/actor'
5 import { VideoShareModel } from '../../../models/video/video-share'
6 import { forwardVideoRelatedActivity } from '../send/utils'
7 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
8 import { Notifier } from '../../notifier'
9 import { VideoModel } from '../../../models/video/video'
10 import { logger } from '../../../helpers/logger'
11 import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
13 async function processAnnounceActivity (options: APProcessorOptions<ActivityAnnounce>) {
14 const { activity, byActor: actorAnnouncer } = options
15 // Only notify if it is not from a fetcher job
16 const notify = options.fromFetch !== true
18 return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity, notify)
21 // ---------------------------------------------------------------------------
24 processAnnounceActivity
27 // ---------------------------------------------------------------------------
29 async function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce, notify: boolean) {
30 const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id
33 let videoCreated: boolean
36 const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: objectUri })
38 videoCreated = result.created
40 logger.debug('Cannot process share of %s. Maybe this is not a video object, so just skipping.', objectUri, { err })
44 await sequelizeTypescript.transaction(async t => {
48 actorId: actorAnnouncer.id,
53 const [ , created ] = await VideoShareModel.findOrCreate({
61 if (video.isOwned() && created === true) {
62 // Don't resend the activity to the sender
63 const exceptions = [ actorAnnouncer ]
65 await forwardVideoRelatedActivity(activity, t, exceptions, video)
71 if (videoCreated && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)