1 import { ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub'
2 import { logger, retryTransactionWrapper } from '../../../helpers'
3 import { sequelizeTypescript } from '../../../initializers'
4 import { ActorModel } from '../../../models/activitypub/actor'
5 import { VideoModel } from '../../../models/video/video'
6 import { VideoShareModel } from '../../../models/video/video-share'
7 import { getOrCreateActorAndServerAndModel } from '../actor'
8 import { forwardActivity } from '../send/misc'
9 import { processCreateActivity } from './process-create'
11 async function processAnnounceActivity (activity: ActivityAnnounce) {
12 const announcedActivity = activity.object
13 const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor)
15 if (typeof announcedActivity === 'string') {
16 return processVideoShare(actorAnnouncer, activity)
17 } else if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'Video') {
18 return processVideoShare(actorAnnouncer, activity)
22 'Unknown activity object type %s -> %s when announcing activity.', announcedActivity.type, announcedActivity.object.type,
23 { activity: activity.id }
29 // ---------------------------------------------------------------------------
32 processAnnounceActivity
35 // ---------------------------------------------------------------------------
37 function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
39 arguments: [ actorAnnouncer, activity ],
40 errorMessage: 'Cannot share the video activity with many retries.'
43 return retryTransactionWrapper(shareVideo, options)
46 function shareVideo (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
47 const announced = activity.object
49 return sequelizeTypescript.transaction(async t => {
53 if (typeof announced === 'string') {
54 video = await VideoModel.loadByUrlAndPopulateAccount(announced as string)
55 if (!video) throw new Error('Unknown video to share ' + announced)
57 video = await processCreateActivity(announced as ActivityCreate)
61 actorId: actorAnnouncer.id,
65 const [ , created ] = await VideoShareModel.findOrCreate({
71 if (video.isOwned() && created === true) {
72 // Don't resend the activity to the sender
73 const exceptions = [ actorAnnouncer ]
74 await forwardActivity(activity, t, exceptions)