]>
Commit | Line | Data |
---|---|---|
1 | import { Transaction } from 'sequelize' | |
2 | import { Activity, ActivityAudience } from '../../../../shared/models/activitypub' | |
3 | import { logger } from '../../../helpers/logger' | |
4 | import { ACTIVITY_PUB } from '../../../initializers' | |
5 | import { ActorModel } from '../../../models/activitypub/actor' | |
6 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | |
7 | import { VideoModel } from '../../../models/video/video' | |
8 | import { VideoCommentModel } from '../../../models/video/video-comment' | |
9 | import { VideoShareModel } from '../../../models/video/video-share' | |
10 | import { activitypubHttpJobScheduler, ActivityPubHttpPayload } from '../../jobs/activitypub-http-job-scheduler' | |
11 | ||
12 | async function forwardActivity ( | |
13 | activity: Activity, | |
14 | t: Transaction, | |
15 | followersException: ActorModel[] = [] | |
16 | ) { | |
17 | const to = activity.to || [] | |
18 | const cc = activity.cc || [] | |
19 | ||
20 | const followersUrls: string[] = [] | |
21 | for (const dest of to.concat(cc)) { | |
22 | if (dest.endsWith('/followers')) { | |
23 | followersUrls.push(dest) | |
24 | } | |
25 | } | |
26 | ||
27 | const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t) | |
28 | const uris = await computeFollowerUris(toActorFollowers, followersException, t) | |
29 | ||
30 | if (uris.length === 0) { | |
31 | logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', ')) | |
32 | return undefined | |
33 | } | |
34 | ||
35 | logger.debug('Creating forwarding job.', { uris }) | |
36 | ||
37 | const jobPayload: ActivityPubHttpPayload = { | |
38 | uris, | |
39 | body: activity | |
40 | } | |
41 | ||
42 | return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload) | |
43 | } | |
44 | ||
45 | async function broadcastToFollowers ( | |
46 | data: any, | |
47 | byActor: ActorModel, | |
48 | toActorFollowers: ActorModel[], | |
49 | t: Transaction, | |
50 | followersException: ActorModel[] = [] | |
51 | ) { | |
52 | const uris = await computeFollowerUris(toActorFollowers, followersException, t) | |
53 | if (uris.length === 0) { | |
54 | logger.info('0 followers for %s, no broadcasting.', toActorFollowers.map(a => a.id).join(', ')) | |
55 | return undefined | |
56 | } | |
57 | ||
58 | logger.debug('Creating broadcast job.', { uris }) | |
59 | ||
60 | const jobPayload: ActivityPubHttpPayload = { | |
61 | uris, | |
62 | signatureActorId: byActor.id, | |
63 | body: data | |
64 | } | |
65 | ||
66 | return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload) | |
67 | } | |
68 | ||
69 | async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string, t: Transaction) { | |
70 | logger.debug('Creating unicast job.', { uri: toActorUrl }) | |
71 | ||
72 | const jobPayload: ActivityPubHttpPayload = { | |
73 | uris: [ toActorUrl ], | |
74 | signatureActorId: byActor.id, | |
75 | body: data | |
76 | } | |
77 | ||
78 | return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload) | |
79 | } | |
80 | ||
81 | function getOriginVideoAudience (video: VideoModel, actorsInvolvedInVideo: ActorModel[]) { | |
82 | return { | |
83 | to: [ video.VideoChannel.Account.Actor.url ], | |
84 | cc: actorsInvolvedInVideo.map(a => a.followersUrl) | |
85 | } | |
86 | } | |
87 | ||
88 | function getOriginVideoCommentAudience ( | |
89 | videoComment: VideoCommentModel, | |
90 | threadParentComments: VideoCommentModel[], | |
91 | actorsInvolvedInVideo: ActorModel[], | |
92 | isOrigin = false | |
93 | ) { | |
94 | const to = [ ACTIVITY_PUB.PUBLIC ] | |
95 | const cc = [ ] | |
96 | ||
97 | // Owner of the video we comment | |
98 | if (isOrigin === false) { | |
99 | cc.push(videoComment.Video.VideoChannel.Account.Actor.url) | |
100 | } | |
101 | ||
102 | // Followers of the poster | |
103 | cc.push(videoComment.Account.Actor.followersUrl) | |
104 | ||
105 | // Send to actors we reply to | |
106 | for (const parentComment of threadParentComments) { | |
107 | cc.push(parentComment.Account.Actor.url) | |
108 | } | |
109 | ||
110 | return { | |
111 | to, | |
112 | cc: cc.concat(actorsInvolvedInVideo.map(a => a.followersUrl)) | |
113 | } | |
114 | } | |
115 | ||
116 | function getObjectFollowersAudience (actorsInvolvedInObject: ActorModel[]) { | |
117 | return { | |
118 | to: actorsInvolvedInObject.map(a => a.followersUrl), | |
119 | cc: [] | |
120 | } | |
121 | } | |
122 | ||
123 | async function getActorsInvolvedInVideo (video: VideoModel, t: Transaction) { | |
124 | const actors = await VideoShareModel.loadActorsByShare(video.id, t) | |
125 | actors.push(video.VideoChannel.Account.Actor) | |
126 | ||
127 | return actors | |
128 | } | |
129 | ||
130 | async function getAudience (actorSender: ActorModel, t: Transaction, isPublic = true) { | |
131 | const followerInboxUrls = await actorSender.getFollowerSharedInboxUrls(t) | |
132 | ||
133 | // Thanks Mastodon: https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/tag_manager.rb#L47 | |
134 | let to = [] | |
135 | let cc = [] | |
136 | ||
137 | if (isPublic) { | |
138 | to = [ ACTIVITY_PUB.PUBLIC ] | |
139 | cc = followerInboxUrls | |
140 | } else { // Unlisted | |
141 | to = followerInboxUrls | |
142 | cc = [ ACTIVITY_PUB.PUBLIC ] | |
143 | } | |
144 | ||
145 | return { to, cc } | |
146 | } | |
147 | ||
148 | function audiencify (object: any, audience: ActivityAudience) { | |
149 | return Object.assign(object, audience) | |
150 | } | |
151 | ||
152 | async function computeFollowerUris (toActorFollower: ActorModel[], followersException: ActorModel[], t: Transaction) { | |
153 | const toActorFollowerIds = toActorFollower.map(a => a.id) | |
154 | ||
155 | const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t) | |
156 | const followersSharedInboxException = followersException.map(f => f.sharedInboxUrl) | |
157 | return result.data.filter(sharedInbox => followersSharedInboxException.indexOf(sharedInbox) === -1) | |
158 | } | |
159 | ||
160 | // --------------------------------------------------------------------------- | |
161 | ||
162 | export { | |
163 | broadcastToFollowers, | |
164 | unicastTo, | |
165 | getAudience, | |
166 | getOriginVideoAudience, | |
167 | getActorsInvolvedInVideo, | |
168 | getObjectFollowersAudience, | |
169 | forwardActivity, | |
170 | audiencify, | |
171 | getOriginVideoCommentAudience | |
172 | } |