1 import { ActivityAdd, ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub'
2 import { logger, retryTransactionWrapper } from '../../../helpers'
3 import { sequelizeTypescript } from '../../../initializers'
4 import { AccountModel } from '../../../models/account/account'
5 import { VideoModel } from '../../../models/video/video'
6 import { VideoChannelModel } from '../../../models/video/video-channel'
7 import { VideoChannelShareModel } from '../../../models/video/video-channel-share'
8 import { VideoShareModel } from '../../../models/video/video-share'
9 import { getOrCreateAccountAndServer } from '../account'
10 import { forwardActivity } from '../send/misc'
11 import { processAddActivity } from './process-add'
12 import { processCreateActivity } from './process-create'
14 async function processAnnounceActivity (activity: ActivityAnnounce) {
15 const announcedActivity = activity.object
16 const accountAnnouncer = await getOrCreateAccountAndServer(activity.actor)
18 if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'VideoChannel') {
19 return processVideoChannelShare(accountAnnouncer, activity)
20 } else if (announcedActivity.type === 'Add' && announcedActivity.object.type === 'Video') {
21 return processVideoShare(accountAnnouncer, activity)
25 'Unknown activity object type %s -> %s when announcing activity.', announcedActivity.type, announcedActivity.object.type,
26 { activity: activity.id }
32 // ---------------------------------------------------------------------------
35 processAnnounceActivity
38 // ---------------------------------------------------------------------------
40 function processVideoChannelShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
42 arguments: [ accountAnnouncer, activity ],
43 errorMessage: 'Cannot share the video channel with many retries.'
46 return retryTransactionWrapper(shareVideoChannel, options)
49 async function shareVideoChannel (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
50 const announcedActivity = activity.object as ActivityCreate
52 return sequelizeTypescript.transaction(async t => {
54 const videoChannel: VideoChannelModel = await processCreateActivity(announcedActivity)
56 accountId: accountAnnouncer.id,
57 videoChannelId: videoChannel.id
60 const [ , created ] = await VideoChannelShareModel.findOrCreate({
66 if (videoChannel.isOwned() && created === true) {
67 // Don't resend the activity to the sender
68 const exceptions = [ accountAnnouncer ]
69 await forwardActivity(activity, t, exceptions)
76 function processVideoShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
78 arguments: [ accountAnnouncer, activity ],
79 errorMessage: 'Cannot share the video with many retries.'
82 return retryTransactionWrapper(shareVideo, options)
85 function shareVideo (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
86 const announcedActivity = activity.object as ActivityAdd
88 return sequelizeTypescript.transaction(async t => {
90 const video: VideoModel = await processAddActivity(announcedActivity)
93 accountId: accountAnnouncer.id,
97 const [ , created ] = await VideoShareModel.findOrCreate({
103 if (video.isOwned() && created === true) {
104 // Don't resend the activity to the sender
105 const exceptions = [ accountAnnouncer ]
106 await forwardActivity(activity, t, exceptions)