diff options
Diffstat (limited to 'server/lib/activitypub/audience.ts')
-rw-r--r-- | server/lib/activitypub/audience.ts | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/server/lib/activitypub/audience.ts b/server/lib/activitypub/audience.ts new file mode 100644 index 000000000..916358fe2 --- /dev/null +++ b/server/lib/activitypub/audience.ts | |||
@@ -0,0 +1,92 @@ | |||
1 | import { Transaction } from 'sequelize' | ||
2 | import { ActivityAudience } from '../../../shared/models/activitypub' | ||
3 | import { ACTIVITY_PUB } from '../../initializers' | ||
4 | import { ActorModel } from '../../models/activitypub/actor' | ||
5 | import { VideoModel } from '../../models/video/video' | ||
6 | import { VideoCommentModel } from '../../models/video/video-comment' | ||
7 | import { VideoShareModel } from '../../models/video/video-share' | ||
8 | |||
9 | function getVideoAudience (video: VideoModel, actorsInvolvedInVideo: ActorModel[]) { | ||
10 | return { | ||
11 | to: [ video.VideoChannel.Account.Actor.url ], | ||
12 | cc: actorsInvolvedInVideo.map(a => a.followersUrl) | ||
13 | } | ||
14 | } | ||
15 | |||
16 | function getVideoCommentAudience ( | ||
17 | videoComment: VideoCommentModel, | ||
18 | threadParentComments: VideoCommentModel[], | ||
19 | actorsInvolvedInVideo: ActorModel[], | ||
20 | isOrigin = false | ||
21 | ) { | ||
22 | const to = [ ACTIVITY_PUB.PUBLIC ] | ||
23 | const cc = [ ] | ||
24 | |||
25 | // Owner of the video we comment | ||
26 | if (isOrigin === false) { | ||
27 | cc.push(videoComment.Video.VideoChannel.Account.Actor.url) | ||
28 | } | ||
29 | |||
30 | // Followers of the poster | ||
31 | cc.push(videoComment.Account.Actor.followersUrl) | ||
32 | |||
33 | // Send to actors we reply to | ||
34 | for (const parentComment of threadParentComments) { | ||
35 | cc.push(parentComment.Account.Actor.url) | ||
36 | } | ||
37 | |||
38 | return { | ||
39 | to, | ||
40 | cc: cc.concat(actorsInvolvedInVideo.map(a => a.followersUrl)) | ||
41 | } | ||
42 | } | ||
43 | |||
44 | function getObjectFollowersAudience (actorsInvolvedInObject: ActorModel[]) { | ||
45 | return { | ||
46 | to: [ ACTIVITY_PUB.PUBLIC ].concat(actorsInvolvedInObject.map(a => a.followersUrl)), | ||
47 | cc: [] | ||
48 | } | ||
49 | } | ||
50 | |||
51 | async function getActorsInvolvedInVideo (video: VideoModel, t: Transaction) { | ||
52 | const actors = await VideoShareModel.loadActorsByShare(video.id, t) | ||
53 | actors.push(video.VideoChannel.Account.Actor) | ||
54 | |||
55 | return actors | ||
56 | } | ||
57 | |||
58 | async function getAudience (actorSender: ActorModel, t: Transaction, isPublic = true) { | ||
59 | return buildAudience([ actorSender.followersUrl ], isPublic) | ||
60 | } | ||
61 | |||
62 | function buildAudience (followerInboxUrls: string[], isPublic = true) { | ||
63 | // Thanks Mastodon: https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/tag_manager.rb#L47 | ||
64 | let to = [] | ||
65 | let cc = [] | ||
66 | |||
67 | if (isPublic) { | ||
68 | to = [ ACTIVITY_PUB.PUBLIC ] | ||
69 | cc = followerInboxUrls | ||
70 | } else { // Unlisted | ||
71 | to = [ ] | ||
72 | cc = [ ] | ||
73 | } | ||
74 | |||
75 | return { to, cc } | ||
76 | } | ||
77 | |||
78 | function audiencify <T> (object: T, audience: ActivityAudience) { | ||
79 | return Object.assign(object, audience) | ||
80 | } | ||
81 | |||
82 | // --------------------------------------------------------------------------- | ||
83 | |||
84 | export { | ||
85 | buildAudience, | ||
86 | getAudience, | ||
87 | getVideoAudience, | ||
88 | getActorsInvolvedInVideo, | ||
89 | getObjectFollowersAudience, | ||
90 | audiencify, | ||
91 | getVideoCommentAudience | ||
92 | } | ||