aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/process/process-announce.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/activitypub/process/process-announce.ts')
-rw-r--r--server/lib/activitypub/process/process-announce.ts67
1 files changed, 13 insertions, 54 deletions
diff --git a/server/lib/activitypub/process/process-announce.ts b/server/lib/activitypub/process/process-announce.ts
index ff2c6d708..7dfee2f60 100644
--- a/server/lib/activitypub/process/process-announce.ts
+++ b/server/lib/activitypub/process/process-announce.ts
@@ -1,24 +1,19 @@
1import { ActivityAdd, ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub' 1import { ActivityAnnounce } from '../../../../shared/models/activitypub'
2import { logger, retryTransactionWrapper } from '../../../helpers' 2import { logger, retryTransactionWrapper } from '../../../helpers'
3import { sequelizeTypescript } from '../../../initializers' 3import { sequelizeTypescript } from '../../../initializers'
4import { AccountModel } from '../../../models/account/account' 4import { ActorModel } from '../../../models/activitypub/actor'
5import { VideoModel } from '../../../models/video/video' 5import { VideoModel } from '../../../models/video/video'
6import { VideoChannelModel } from '../../../models/video/video-channel'
7import { VideoChannelShareModel } from '../../../models/video/video-channel-share'
8import { VideoShareModel } from '../../../models/video/video-share' 6import { VideoShareModel } from '../../../models/video/video-share'
9import { getOrCreateAccountAndServer } from '../account' 7import { getOrCreateActorAndServerAndModel } from '../actor'
10import { forwardActivity } from '../send/misc' 8import { forwardActivity } from '../send/misc'
11import { processAddActivity } from './process-add'
12import { processCreateActivity } from './process-create' 9import { processCreateActivity } from './process-create'
13 10
14async function processAnnounceActivity (activity: ActivityAnnounce) { 11async function processAnnounceActivity (activity: ActivityAnnounce) {
15 const announcedActivity = activity.object 12 const announcedActivity = activity.object
16 const accountAnnouncer = await getOrCreateAccountAndServer(activity.actor) 13 const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor)
17 14
18 if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'VideoChannel') { 15 if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'Video') {
19 return processVideoChannelShare(accountAnnouncer, activity) 16 return processVideoShare(actorAnnouncer, activity)
20 } else if (announcedActivity.type === 'Add' && announcedActivity.object.type === 'Video') {
21 return processVideoShare(accountAnnouncer, activity)
22 } 17 }
23 18
24 logger.warn( 19 logger.warn(
@@ -37,60 +32,24 @@ export {
37 32
38// --------------------------------------------------------------------------- 33// ---------------------------------------------------------------------------
39 34
40function processVideoChannelShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { 35function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
41 const options = { 36 const options = {
42 arguments: [ accountAnnouncer, activity ], 37 arguments: [ actorAnnouncer, activity ],
43 errorMessage: 'Cannot share the video channel with many retries.'
44 }
45
46 return retryTransactionWrapper(shareVideoChannel, options)
47}
48
49async function shareVideoChannel (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
50 const announcedActivity = activity.object as ActivityCreate
51
52 return sequelizeTypescript.transaction(async t => {
53 // Add share entry
54 const videoChannel: VideoChannelModel = await processCreateActivity(announcedActivity)
55 const share = {
56 accountId: accountAnnouncer.id,
57 videoChannelId: videoChannel.id
58 }
59
60 const [ , created ] = await VideoChannelShareModel.findOrCreate({
61 where: share,
62 defaults: share,
63 transaction: t
64 })
65
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)
70 }
71
72 return undefined
73 })
74}
75
76function processVideoShare (accountAnnouncer: AccountModel, activity: ActivityAnnounce) {
77 const options = {
78 arguments: [ accountAnnouncer, activity ],
79 errorMessage: 'Cannot share the video with many retries.' 38 errorMessage: 'Cannot share the video with many retries.'
80 } 39 }
81 40
82 return retryTransactionWrapper(shareVideo, options) 41 return retryTransactionWrapper(shareVideo, options)
83} 42}
84 43
85function shareVideo (accountAnnouncer: AccountModel, activity: ActivityAnnounce) { 44function shareVideo (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
86 const announcedActivity = activity.object as ActivityAdd 45 const announcedActivity = activity.object
87 46
88 return sequelizeTypescript.transaction(async t => { 47 return sequelizeTypescript.transaction(async t => {
89 // Add share entry 48 // Add share entry
90 const video: VideoModel = await processAddActivity(announcedActivity) 49 const video: VideoModel = await processCreateActivity(announcedActivity)
91 50
92 const share = { 51 const share = {
93 accountId: accountAnnouncer.id, 52 actorId: actorAnnouncer.id,
94 videoId: video.id 53 videoId: video.id
95 } 54 }
96 55
@@ -102,7 +61,7 @@ function shareVideo (accountAnnouncer: AccountModel, activity: ActivityAnnounce)
102 61
103 if (video.isOwned() && created === true) { 62 if (video.isOwned() && created === true) {
104 // Don't resend the activity to the sender 63 // Don't resend the activity to the sender
105 const exceptions = [ accountAnnouncer ] 64 const exceptions = [ actorAnnouncer ]
106 await forwardActivity(activity, t, exceptions) 65 await forwardActivity(activity, t, exceptions)
107 } 66 }
108 67