diff options
author | Chocobozzz <me@florianbigard.com> | 2018-09-14 16:51:35 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-09-14 16:51:35 +0200 |
commit | a2377d15ee09301cf4cc5434ad865a21918da15f (patch) | |
tree | 4325153370bb19511ff162114056a5c7fd89744d /server/lib/activitypub/send/send-create.ts | |
parent | d61b817890d5d5bba61d447518321870498028d8 (diff) | |
download | PeerTube-a2377d15ee09301cf4cc5434ad865a21918da15f.tar.gz PeerTube-a2377d15ee09301cf4cc5434ad865a21918da15f.tar.zst PeerTube-a2377d15ee09301cf4cc5434ad865a21918da15f.zip |
Refractor activities sending
Diffstat (limited to 'server/lib/activitypub/send/send-create.ts')
-rw-r--r-- | server/lib/activitypub/send/send-create.ts | 95 |
1 files changed, 43 insertions, 52 deletions
diff --git a/server/lib/activitypub/send/send-create.ts b/server/lib/activitypub/send/send-create.ts index 6f89b1a22..285edba3b 100644 --- a/server/lib/activitypub/send/send-create.ts +++ b/server/lib/activitypub/send/send-create.ts | |||
@@ -1,21 +1,13 @@ | |||
1 | import { Transaction } from 'sequelize' | 1 | import { Transaction } from 'sequelize' |
2 | import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub' | 2 | import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub' |
3 | import { VideoPrivacy } from '../../../../shared/models/videos' | 3 | import { VideoPrivacy } from '../../../../shared/models/videos' |
4 | import { getServerActor } from '../../../helpers/utils' | ||
5 | import { ActorModel } from '../../../models/activitypub/actor' | 4 | import { ActorModel } from '../../../models/activitypub/actor' |
6 | import { VideoModel } from '../../../models/video/video' | 5 | import { VideoModel } from '../../../models/video/video' |
7 | import { VideoAbuseModel } from '../../../models/video/video-abuse' | 6 | import { VideoAbuseModel } from '../../../models/video/video-abuse' |
8 | import { VideoCommentModel } from '../../../models/video/video-comment' | 7 | import { VideoCommentModel } from '../../../models/video/video-comment' |
9 | import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url' | 8 | import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url' |
10 | import { broadcastToActors, broadcastToFollowers, unicastTo } from './utils' | 9 | import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils' |
11 | import { | 10 | import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf, getVideoCommentAudience } from '../audience' |
12 | audiencify, | ||
13 | getActorsInvolvedInVideo, | ||
14 | getAudience, | ||
15 | getObjectFollowersAudience, | ||
16 | getVideoAudience, | ||
17 | getVideoCommentAudience | ||
18 | } from '../audience' | ||
19 | import { logger } from '../../../helpers/logger' | 11 | import { logger } from '../../../helpers/logger' |
20 | import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy' | 12 | import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy' |
21 | 13 | ||
@@ -40,6 +32,7 @@ async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, | |||
40 | 32 | ||
41 | logger.info('Creating job to send video abuse %s.', url) | 33 | logger.info('Creating job to send video abuse %s.', url) |
42 | 34 | ||
35 | // Custom audience, we only send the abuse to the origin instance | ||
43 | const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] } | 36 | const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] } |
44 | const createActivity = buildCreateActivity(url, byActor, videoAbuse.toActivityPubObject(), audience) | 37 | const createActivity = buildCreateActivity(url, byActor, videoAbuse.toActivityPubObject(), audience) |
45 | 38 | ||
@@ -49,15 +42,15 @@ async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, | |||
49 | async function sendCreateCacheFile (byActor: ActorModel, fileRedundancy: VideoRedundancyModel) { | 42 | async function sendCreateCacheFile (byActor: ActorModel, fileRedundancy: VideoRedundancyModel) { |
50 | logger.info('Creating job to send file cache of %s.', fileRedundancy.url) | 43 | logger.info('Creating job to send file cache of %s.', fileRedundancy.url) |
51 | 44 | ||
52 | const redundancyObject = fileRedundancy.toActivityPubObject() | ||
53 | |||
54 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(fileRedundancy.VideoFile.Video.id) | 45 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(fileRedundancy.VideoFile.Video.id) |
55 | const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, undefined) | 46 | const redundancyObject = fileRedundancy.toActivityPubObject() |
56 | |||
57 | const audience = getVideoAudience(video, actorsInvolvedInVideo) | ||
58 | const createActivity = buildCreateActivity(fileRedundancy.url, byActor, redundancyObject, audience) | ||
59 | 47 | ||
60 | return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl) | 48 | return sendVideoRelatedCreateActivity({ |
49 | byActor, | ||
50 | video, | ||
51 | url: fileRedundancy.url, | ||
52 | object: redundancyObject | ||
53 | }) | ||
61 | } | 54 | } |
62 | 55 | ||
63 | async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) { | 56 | async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) { |
@@ -70,6 +63,7 @@ async function sendCreateVideoComment (comment: VideoCommentModel, t: Transactio | |||
70 | const commentObject = comment.toActivityPubObject(threadParentComments) | 63 | const commentObject = comment.toActivityPubObject(threadParentComments) |
71 | 64 | ||
72 | const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t) | 65 | const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t) |
66 | // Add the actor that commented too | ||
73 | actorsInvolvedInComment.push(byActor) | 67 | actorsInvolvedInComment.push(byActor) |
74 | 68 | ||
75 | const parentsCommentActors = threadParentComments.map(c => c.Account.Actor) | 69 | const parentsCommentActors = threadParentComments.map(c => c.Account.Actor) |
@@ -78,7 +72,7 @@ async function sendCreateVideoComment (comment: VideoCommentModel, t: Transactio | |||
78 | if (isOrigin) { | 72 | if (isOrigin) { |
79 | audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin) | 73 | audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin) |
80 | } else { | 74 | } else { |
81 | audience = getObjectFollowersAudience(actorsInvolvedInComment.concat(parentsCommentActors)) | 75 | audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors)) |
82 | } | 76 | } |
83 | 77 | ||
84 | const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience) | 78 | const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience) |
@@ -103,24 +97,14 @@ async function sendCreateView (byActor: ActorModel, video: VideoModel, t: Transa | |||
103 | const url = getVideoViewActivityPubUrl(byActor, video) | 97 | const url = getVideoViewActivityPubUrl(byActor, video) |
104 | const viewActivity = buildViewActivity(byActor, video) | 98 | const viewActivity = buildViewActivity(byActor, video) |
105 | 99 | ||
106 | const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t) | 100 | return sendVideoRelatedCreateActivity({ |
107 | 101 | // Use the server actor to send the view | |
108 | // Send to origin | 102 | byActor, |
109 | if (video.isOwned() === false) { | 103 | video, |
110 | const audience = getVideoAudience(video, actorsInvolvedInVideo) | 104 | url, |
111 | const createActivity = buildCreateActivity(url, byActor, viewActivity, audience) | 105 | object: viewActivity, |
112 | 106 | transaction: t | |
113 | return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl) | 107 | }) |
114 | } | ||
115 | |||
116 | // Send to followers | ||
117 | const audience = getObjectFollowersAudience(actorsInvolvedInVideo) | ||
118 | const createActivity = buildCreateActivity(url, byActor, viewActivity, audience) | ||
119 | |||
120 | // Use the server actor to send the view | ||
121 | const serverActor = await getServerActor() | ||
122 | const actorsException = [ byActor ] | ||
123 | return broadcastToFollowers(createActivity, serverActor, actorsInvolvedInVideo, t, actorsException) | ||
124 | } | 108 | } |
125 | 109 | ||
126 | async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) { | 110 | async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) { |
@@ -129,22 +113,13 @@ async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Tra | |||
129 | const url = getVideoDislikeActivityPubUrl(byActor, video) | 113 | const url = getVideoDislikeActivityPubUrl(byActor, video) |
130 | const dislikeActivity = buildDislikeActivity(byActor, video) | 114 | const dislikeActivity = buildDislikeActivity(byActor, video) |
131 | 115 | ||
132 | const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t) | 116 | return sendVideoRelatedCreateActivity({ |
133 | 117 | byActor, | |
134 | // Send to origin | 118 | video, |
135 | if (video.isOwned() === false) { | 119 | url, |
136 | const audience = getVideoAudience(video, actorsInvolvedInVideo) | 120 | object: dislikeActivity, |
137 | const createActivity = buildCreateActivity(url, byActor, dislikeActivity, audience) | 121 | transaction: t |
138 | 122 | }) | |
139 | return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl) | ||
140 | } | ||
141 | |||
142 | // Send to followers | ||
143 | const audience = getObjectFollowersAudience(actorsInvolvedInVideo) | ||
144 | const createActivity = buildCreateActivity(url, byActor, dislikeActivity, audience) | ||
145 | |||
146 | const actorsException = [ byActor ] | ||
147 | return broadcastToFollowers(createActivity, byActor, actorsInvolvedInVideo, t, actorsException) | ||
148 | } | 123 | } |
149 | 124 | ||
150 | function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate { | 125 | function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate { |
@@ -189,3 +164,19 @@ export { | |||
189 | sendCreateVideoComment, | 164 | sendCreateVideoComment, |
190 | sendCreateCacheFile | 165 | sendCreateCacheFile |
191 | } | 166 | } |
167 | |||
168 | // --------------------------------------------------------------------------- | ||
169 | |||
170 | async function sendVideoRelatedCreateActivity (options: { | ||
171 | byActor: ActorModel, | ||
172 | video: VideoModel, | ||
173 | url: string, | ||
174 | object: any, | ||
175 | transaction?: Transaction | ||
176 | }) { | ||
177 | const activityBuilder = (audience: ActivityAudience) => { | ||
178 | return buildCreateActivity(options.url, options.byActor, options.object, audience) | ||
179 | } | ||
180 | |||
181 | return sendVideoRelatedActivity(activityBuilder, options) | ||
182 | } | ||