diff options
author | Chocobozzz <me@florianbigard.com> | 2022-03-23 16:14:33 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2022-03-24 09:40:46 +0100 |
commit | a219c9100b3ce8774d454497d46be87465bf664e (patch) | |
tree | caa869e47919a9e23cc86dcece1100e239683b8c /server/lib/activitypub/send/send-create.ts | |
parent | 7e98a7df7d04e19ba67163a86c7b876d78d76839 (diff) | |
download | PeerTube-a219c9100b3ce8774d454497d46be87465bf664e.tar.gz PeerTube-a219c9100b3ce8774d454497d46be87465bf664e.tar.zst PeerTube-a219c9100b3ce8774d454497d46be87465bf664e.zip |
Refactor AP context builder
Diffstat (limited to 'server/lib/activitypub/send/send-create.ts')
-rw-r--r-- | server/lib/activitypub/send/send-create.ts | 67 |
1 files changed, 54 insertions, 13 deletions
diff --git a/server/lib/activitypub/send/send-create.ts b/server/lib/activitypub/send/send-create.ts index f6d897220..5d8763495 100644 --- a/server/lib/activitypub/send/send-create.ts +++ b/server/lib/activitypub/send/send-create.ts | |||
@@ -25,7 +25,7 @@ import { | |||
25 | 25 | ||
26 | const lTags = loggerTagsFactory('ap', 'create') | 26 | const lTags = loggerTagsFactory('ap', 'create') |
27 | 27 | ||
28 | async function sendCreateVideo (video: MVideoAP, t: Transaction) { | 28 | async function sendCreateVideo (video: MVideoAP, transaction: Transaction) { |
29 | if (!video.hasPrivacyForFederation()) return undefined | 29 | if (!video.hasPrivacyForFederation()) return undefined |
30 | 30 | ||
31 | logger.info('Creating job to send video creation of %s.', video.url, lTags(video.uuid)) | 31 | logger.info('Creating job to send video creation of %s.', video.url, lTags(video.uuid)) |
@@ -36,7 +36,13 @@ async function sendCreateVideo (video: MVideoAP, t: Transaction) { | |||
36 | const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC) | 36 | const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC) |
37 | const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience) | 37 | const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience) |
38 | 38 | ||
39 | return broadcastToFollowers(createActivity, byActor, [ byActor ], t) | 39 | return broadcastToFollowers({ |
40 | data: createActivity, | ||
41 | byActor, | ||
42 | toFollowersOf: [ byActor ], | ||
43 | transaction, | ||
44 | contextType: 'Video' | ||
45 | }) | ||
40 | } | 46 | } |
41 | 47 | ||
42 | async function sendCreateCacheFile ( | 48 | async function sendCreateCacheFile ( |
@@ -55,7 +61,7 @@ async function sendCreateCacheFile ( | |||
55 | }) | 61 | }) |
56 | } | 62 | } |
57 | 63 | ||
58 | async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, t: Transaction) { | 64 | async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, transaction: Transaction) { |
59 | if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined | 65 | if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined |
60 | 66 | ||
61 | logger.info('Creating job to send create video playlist of %s.', playlist.url, lTags(playlist.uuid)) | 67 | logger.info('Creating job to send create video playlist of %s.', playlist.url, lTags(playlist.uuid)) |
@@ -63,7 +69,7 @@ async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, t: Transac | |||
63 | const byActor = playlist.OwnerAccount.Actor | 69 | const byActor = playlist.OwnerAccount.Actor |
64 | const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC) | 70 | const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC) |
65 | 71 | ||
66 | const object = await playlist.toActivityPubObject(null, t) | 72 | const object = await playlist.toActivityPubObject(null, transaction) |
67 | const createActivity = buildCreateActivity(playlist.url, byActor, object, audience) | 73 | const createActivity = buildCreateActivity(playlist.url, byActor, object, audience) |
68 | 74 | ||
69 | const serverActor = await getServerActor() | 75 | const serverActor = await getServerActor() |
@@ -71,19 +77,25 @@ async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, t: Transac | |||
71 | 77 | ||
72 | if (playlist.VideoChannel) toFollowersOf.push(playlist.VideoChannel.Actor) | 78 | if (playlist.VideoChannel) toFollowersOf.push(playlist.VideoChannel.Actor) |
73 | 79 | ||
74 | return broadcastToFollowers(createActivity, byActor, toFollowersOf, t) | 80 | return broadcastToFollowers({ |
81 | data: createActivity, | ||
82 | byActor, | ||
83 | toFollowersOf, | ||
84 | transaction, | ||
85 | contextType: 'Playlist' | ||
86 | }) | ||
75 | } | 87 | } |
76 | 88 | ||
77 | async function sendCreateVideoComment (comment: MCommentOwnerVideo, t: Transaction) { | 89 | async function sendCreateVideoComment (comment: MCommentOwnerVideo, transaction: Transaction) { |
78 | logger.info('Creating job to send comment %s.', comment.url) | 90 | logger.info('Creating job to send comment %s.', comment.url) |
79 | 91 | ||
80 | const isOrigin = comment.Video.isOwned() | 92 | const isOrigin = comment.Video.isOwned() |
81 | 93 | ||
82 | const byActor = comment.Account.Actor | 94 | const byActor = comment.Account.Actor |
83 | const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t) | 95 | const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, transaction) |
84 | const commentObject = comment.toActivityPubObject(threadParentComments) | 96 | const commentObject = comment.toActivityPubObject(threadParentComments) |
85 | 97 | ||
86 | const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t) | 98 | const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, transaction) |
87 | // Add the actor that commented too | 99 | // Add the actor that commented too |
88 | actorsInvolvedInComment.push(byActor) | 100 | actorsInvolvedInComment.push(byActor) |
89 | 101 | ||
@@ -101,16 +113,45 @@ async function sendCreateVideoComment (comment: MCommentOwnerVideo, t: Transacti | |||
101 | 113 | ||
102 | // This was a reply, send it to the parent actors | 114 | // This was a reply, send it to the parent actors |
103 | const actorsException = [ byActor ] | 115 | const actorsException = [ byActor ] |
104 | await broadcastToActors(createActivity, byActor, parentsCommentActors, t, actorsException) | 116 | await broadcastToActors({ |
117 | data: createActivity, | ||
118 | byActor, | ||
119 | toActors: parentsCommentActors, | ||
120 | transaction, | ||
121 | actorsException, | ||
122 | contextType: 'Comment' | ||
123 | }) | ||
105 | 124 | ||
106 | // Broadcast to our followers | 125 | // Broadcast to our followers |
107 | await broadcastToFollowers(createActivity, byActor, [ byActor ], t) | 126 | await broadcastToFollowers({ |
127 | data: createActivity, | ||
128 | byActor, | ||
129 | toFollowersOf: [ byActor ], | ||
130 | transaction, | ||
131 | contextType: 'Comment' | ||
132 | }) | ||
108 | 133 | ||
109 | // Send to actors involved in the comment | 134 | // Send to actors involved in the comment |
110 | if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException) | 135 | if (isOrigin) { |
136 | return broadcastToFollowers({ | ||
137 | data: createActivity, | ||
138 | byActor, | ||
139 | toFollowersOf: actorsInvolvedInComment, | ||
140 | transaction, | ||
141 | actorsException, | ||
142 | contextType: 'Comment' | ||
143 | }) | ||
144 | } | ||
111 | 145 | ||
112 | // Send to origin | 146 | // Send to origin |
113 | t.afterCommit(() => unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.getSharedInbox())) | 147 | return transaction.afterCommit(() => { |
148 | return unicastTo({ | ||
149 | data: createActivity, | ||
150 | byActor, | ||
151 | toActorUrl: comment.Video.VideoChannel.Account.Actor.getSharedInbox(), | ||
152 | contextType: 'Comment' | ||
153 | }) | ||
154 | }) | ||
114 | } | 155 | } |
115 | 156 | ||
116 | function buildCreateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityCreate { | 157 | function buildCreateActivity (url: string, byActor: MActorLight, object: any, audience?: ActivityAudience): ActivityCreate { |
@@ -144,8 +185,8 @@ async function sendVideoRelatedCreateActivity (options: { | |||
144 | video: MVideoAccountLight | 185 | video: MVideoAccountLight |
145 | url: string | 186 | url: string |
146 | object: any | 187 | object: any |
188 | contextType: ContextType | ||
147 | transaction?: Transaction | 189 | transaction?: Transaction |
148 | contextType?: ContextType | ||
149 | }) { | 190 | }) { |
150 | const activityBuilder = (audience: ActivityAudience) => { | 191 | const activityBuilder = (audience: ActivityAudience) => { |
151 | return buildCreateActivity(options.url, options.byActor, options.object, audience) | 192 | return buildCreateActivity(options.url, options.byActor, options.object, audience) |