]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/misc.ts
Save
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / misc.ts
CommitLineData
54141398 1import { Transaction } from 'sequelize'
3fd3ab2d
C
2import { Activity } from '../../../../shared/models/activitypub'
3import { logger } from '../../../helpers'
4import { ACTIVITY_PUB } from '../../../initializers'
5import { AccountModel } from '../../../models/account/account'
6import { AccountFollowModel } from '../../../models/account/account-follow'
7import { VideoModel } from '../../../models/video/video'
8import { VideoChannelModel } from '../../../models/video/video-channel'
9import { VideoChannelShareModel } from '../../../models/video/video-channel-share'
10import { VideoShareModel } from '../../../models/video/video-share'
11import { activitypubHttpJobScheduler, ActivityPubHttpPayload } from '../../jobs/activitypub-http-job-scheduler'
63c93323
C
12
13async function forwardActivity (
14 activity: Activity,
15 t: Transaction,
3fd3ab2d 16 followersException: AccountModel[] = []
63c93323
C
17) {
18 const to = activity.to || []
19 const cc = activity.cc || []
20
21 const followersUrls: string[] = []
22 for (const dest of to.concat(cc)) {
23 if (dest.endsWith('/followers')) {
24 followersUrls.push(dest)
25 }
26 }
27
3fd3ab2d 28 const toAccountFollowers = await AccountModel.listByFollowersUrls(followersUrls, t)
25ed141c 29 const uris = await computeFollowerUris(toAccountFollowers, followersException, t)
63c93323
C
30
31 if (uris.length === 0) {
32 logger.info('0 followers for %s, no forwarding.', toAccountFollowers.map(a => a.id).join(', '))
df1966c9 33 return undefined
63c93323
C
34 }
35
36 logger.debug('Creating forwarding job.', { uris })
37
38 const jobPayload: ActivityPubHttpPayload = {
39 uris,
40 body: activity
41 }
42
43 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
44}
54141398 45
40ff5707
C
46async function broadcastToFollowers (
47 data: any,
3fd3ab2d
C
48 byAccount: AccountModel,
49 toAccountFollowers: AccountModel[],
40ff5707 50 t: Transaction,
3fd3ab2d 51 followersException: AccountModel[] = []
40ff5707 52) {
25ed141c 53 const uris = await computeFollowerUris(toAccountFollowers, followersException, t)
63c93323
C
54 if (uris.length === 0) {
55 logger.info('0 followers for %s, no broadcasting.', toAccountFollowers.map(a => a.id).join(', '))
df1966c9 56 return undefined
54141398
C
57 }
58
63c93323 59 logger.debug('Creating broadcast job.', { uris })
40ff5707 60
63c93323 61 const jobPayload: ActivityPubHttpPayload = {
40ff5707 62 uris,
54141398
C
63 signatureAccountId: byAccount.id,
64 body: data
65 }
66
67 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
68}
69
3fd3ab2d 70async function unicastTo (data: any, byAccount: AccountModel, toAccountUrl: string, t: Transaction) {
63c93323
C
71 logger.debug('Creating unicast job.', { uri: toAccountUrl })
72
73 const jobPayload: ActivityPubHttpPayload = {
54141398
C
74 uris: [ toAccountUrl ],
75 signatureAccountId: byAccount.id,
76 body: data
77 }
78
79 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload)
80}
81
3fd3ab2d 82function getOriginVideoAudience (video: VideoModel, accountsInvolvedInVideo: AccountModel[]) {
0032ebe9
C
83 return {
84 to: [ video.VideoChannel.Account.url ],
63c93323 85 cc: accountsInvolvedInVideo.map(a => a.followersUrl)
0032ebe9
C
86 }
87}
88
3fd3ab2d 89function getOriginVideoChannelAudience (videoChannel: VideoChannelModel, accountsInvolved: AccountModel[]) {
4e50b6a1
C
90 return {
91 to: [ videoChannel.Account.url ],
92 cc: accountsInvolved.map(a => a.followersUrl)
93 }
94}
95
3fd3ab2d 96function getObjectFollowersAudience (accountsInvolvedInObject: AccountModel[]) {
0032ebe9 97 return {
4e50b6a1 98 to: accountsInvolvedInObject.map(a => a.followersUrl),
0032ebe9
C
99 cc: []
100 }
101}
102
3fd3ab2d
C
103async function getAccountsInvolvedInVideo (video: VideoModel, t: Transaction) {
104 const accountsToForwardView = await VideoShareModel.loadAccountsByShare(video.id, t)
0032ebe9
C
105 accountsToForwardView.push(video.VideoChannel.Account)
106
107 return accountsToForwardView
108}
109
3fd3ab2d
C
110async function getAccountsInvolvedInVideoChannel (videoChannel: VideoChannelModel, t: Transaction) {
111 const accountsToForwardView = await VideoChannelShareModel.loadAccountsByShare(videoChannel.id, t)
4e50b6a1
C
112 accountsToForwardView.push(videoChannel.Account)
113
114 return accountsToForwardView
115}
116
3fd3ab2d 117async function getAudience (accountSender: AccountModel, t: Transaction, isPublic = true) {
25ed141c 118 const followerInboxUrls = await accountSender.getFollowerSharedInboxUrls(t)
54141398
C
119
120 // Thanks Mastodon: https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/tag_manager.rb#L47
121 let to = []
122 let cc = []
123
124 if (isPublic) {
125 to = [ ACTIVITY_PUB.PUBLIC ]
126 cc = followerInboxUrls
127 } else { // Unlisted
128 to = followerInboxUrls
129 cc = [ ACTIVITY_PUB.PUBLIC ]
130 }
131
132 return { to, cc }
133}
134
3fd3ab2d 135async function computeFollowerUris (toAccountFollower: AccountModel[], followersException: AccountModel[], t: Transaction) {
63c93323
C
136 const toAccountFollowerIds = toAccountFollower.map(a => a.id)
137
3fd3ab2d 138 const result = await AccountFollowModel.listAcceptedFollowerSharedInboxUrls(toAccountFollowerIds, t)
63c93323 139 const followersSharedInboxException = followersException.map(f => f.sharedInboxUrl)
3fd3ab2d 140 return result.data.filter(sharedInbox => followersSharedInboxException.indexOf(sharedInbox) === -1)
63c93323
C
141}
142
54141398
C
143// ---------------------------------------------------------------------------
144
145export {
146 broadcastToFollowers,
4e50b6a1 147 getOriginVideoChannelAudience,
54141398 148 unicastTo,
0032ebe9
C
149 getAudience,
150 getOriginVideoAudience,
63c93323 151 getAccountsInvolvedInVideo,
4e50b6a1
C
152 getAccountsInvolvedInVideoChannel,
153 getObjectFollowersAudience,
63c93323 154 forwardActivity
54141398 155}