]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/misc.ts
A few updates for the watch video view (#181)
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / misc.ts
CommitLineData
54141398 1import { Transaction } from 'sequelize'
e12a0092 2import { Activity, ActivityAudience } from '../../../../shared/models/activitypub'
da854ddd 3import { logger } from '../../../helpers/logger'
3fd3ab2d 4import { ACTIVITY_PUB } from '../../../initializers'
50d6de9c
C
5import { ActorModel } from '../../../models/activitypub/actor'
6import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
3fd3ab2d 7import { VideoModel } from '../../../models/video/video'
d7e70384 8import { VideoCommentModel } from '../../../models/video/video-comment'
3fd3ab2d
C
9import { VideoShareModel } from '../../../models/video/video-share'
10import { activitypubHttpJobScheduler, ActivityPubHttpPayload } from '../../jobs/activitypub-http-job-scheduler'
63c93323
C
11
12async function forwardActivity (
13 activity: Activity,
14 t: Transaction,
50d6de9c 15 followersException: ActorModel[] = []
63c93323
C
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
50d6de9c
C
27 const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
28 const uris = await computeFollowerUris(toActorFollowers, followersException, t)
63c93323
C
29
30 if (uris.length === 0) {
50d6de9c 31 logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
df1966c9 32 return undefined
63c93323
C
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}
54141398 44
40ff5707
C
45async function broadcastToFollowers (
46 data: any,
50d6de9c
C
47 byActor: ActorModel,
48 toActorFollowers: ActorModel[],
40ff5707 49 t: Transaction,
50d6de9c 50 followersException: ActorModel[] = []
40ff5707 51) {
50d6de9c 52 const uris = await computeFollowerUris(toActorFollowers, followersException, t)
63c93323 53 if (uris.length === 0) {
50d6de9c 54 logger.info('0 followers for %s, no broadcasting.', toActorFollowers.map(a => a.id).join(', '))
df1966c9 55 return undefined
54141398
C
56 }
57
63c93323 58 logger.debug('Creating broadcast job.', { uris })
40ff5707 59
63c93323 60 const jobPayload: ActivityPubHttpPayload = {
40ff5707 61 uris,
50d6de9c 62 signatureActorId: byActor.id,
54141398
C
63 body: data
64 }
65
66 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
67}
68
50d6de9c
C
69async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string, t: Transaction) {
70 logger.debug('Creating unicast job.', { uri: toActorUrl })
63c93323
C
71
72 const jobPayload: ActivityPubHttpPayload = {
50d6de9c
C
73 uris: [ toActorUrl ],
74 signatureActorId: byActor.id,
54141398
C
75 body: data
76 }
77
78 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload)
79}
80
50d6de9c 81function getOriginVideoAudience (video: VideoModel, actorsInvolvedInVideo: ActorModel[]) {
0032ebe9 82 return {
50d6de9c
C
83 to: [ video.VideoChannel.Account.Actor.url ],
84 cc: actorsInvolvedInVideo.map(a => a.followersUrl)
0032ebe9
C
85 }
86}
87
d7e70384
C
88function 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
50d6de9c 116function getObjectFollowersAudience (actorsInvolvedInObject: ActorModel[]) {
4e50b6a1 117 return {
50d6de9c 118 to: actorsInvolvedInObject.map(a => a.followersUrl),
0032ebe9
C
119 cc: []
120 }
121}
122
50d6de9c 123async function getActorsInvolvedInVideo (video: VideoModel, t: Transaction) {
d7e70384
C
124 const actors = await VideoShareModel.loadActorsByShare(video.id, t)
125 actors.push(video.VideoChannel.Account.Actor)
4e50b6a1 126
d7e70384 127 return actors
4e50b6a1
C
128}
129
50d6de9c
C
130async function getAudience (actorSender: ActorModel, t: Transaction, isPublic = true) {
131 const followerInboxUrls = await actorSender.getFollowerSharedInboxUrls(t)
54141398
C
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
e12a0092
C
148function audiencify (object: any, audience: ActivityAudience) {
149 return Object.assign(object, audience)
150}
151
50d6de9c
C
152async function computeFollowerUris (toActorFollower: ActorModel[], followersException: ActorModel[], t: Transaction) {
153 const toActorFollowerIds = toActorFollower.map(a => a.id)
63c93323 154
50d6de9c 155 const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
63c93323 156 const followersSharedInboxException = followersException.map(f => f.sharedInboxUrl)
3fd3ab2d 157 return result.data.filter(sharedInbox => followersSharedInboxException.indexOf(sharedInbox) === -1)
63c93323
C
158}
159
54141398
C
160// ---------------------------------------------------------------------------
161
162export {
163 broadcastToFollowers,
164 unicastTo,
0032ebe9
C
165 getAudience,
166 getOriginVideoAudience,
50d6de9c 167 getActorsInvolvedInVideo,
4e50b6a1 168 getObjectFollowersAudience,
e12a0092 169 forwardActivity,
d7e70384
C
170 audiencify,
171 getOriginVideoCommentAudience
54141398 172}