diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-24 13:41:10 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:53 +0100 |
commit | 63c93323ecdeaa4b6183d75dd3f13469e1ef3ebd (patch) | |
tree | 3ee4b351025d4b19f6e880754df44fa7605b683d /server/lib/activitypub/send/misc.ts | |
parent | d4f1e94c89336255537b0b82913591f00e716201 (diff) | |
download | PeerTube-63c93323ecdeaa4b6183d75dd3f13469e1ef3ebd.tar.gz PeerTube-63c93323ecdeaa4b6183d75dd3f13469e1ef3ebd.tar.zst PeerTube-63c93323ecdeaa4b6183d75dd3f13469e1ef3ebd.zip |
Correctly forward like/dislikes and undo
Diffstat (limited to 'server/lib/activitypub/send/misc.ts')
-rw-r--r-- | server/lib/activitypub/send/misc.ts | 83 |
1 files changed, 65 insertions, 18 deletions
diff --git a/server/lib/activitypub/send/misc.ts b/server/lib/activitypub/send/misc.ts index 41a039b19..fe137464e 100644 --- a/server/lib/activitypub/send/misc.ts +++ b/server/lib/activitypub/send/misc.ts | |||
@@ -2,8 +2,45 @@ import { Transaction } from 'sequelize' | |||
2 | import { logger } from '../../../helpers/logger' | 2 | import { logger } from '../../../helpers/logger' |
3 | import { ACTIVITY_PUB, database as db } from '../../../initializers' | 3 | import { ACTIVITY_PUB, database as db } from '../../../initializers' |
4 | import { AccountInstance } from '../../../models/account/account-interface' | 4 | import { AccountInstance } from '../../../models/account/account-interface' |
5 | import { activitypubHttpJobScheduler } from '../../jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler' | 5 | import { |
6 | activitypubHttpJobScheduler, | ||
7 | ActivityPubHttpPayload | ||
8 | } from '../../jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler' | ||
6 | import { VideoInstance } from '../../../models/video/video-interface' | 9 | import { VideoInstance } from '../../../models/video/video-interface' |
10 | import { Activity } from '../../../../shared/models/activitypub/activity' | ||
11 | |||
12 | async function forwardActivity ( | ||
13 | activity: Activity, | ||
14 | t: Transaction, | ||
15 | followersException: AccountInstance[] = [] | ||
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 toAccountFollowers = await db.Account.listByFollowersUrls(followersUrls) | ||
28 | const uris = await computeFollowerUris(toAccountFollowers, followersException) | ||
29 | |||
30 | if (uris.length === 0) { | ||
31 | logger.info('0 followers for %s, no forwarding.', toAccountFollowers.map(a => a.id).join(', ')) | ||
32 | return | ||
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 | } | ||
7 | 44 | ||
8 | async function broadcastToFollowers ( | 45 | async function broadcastToFollowers ( |
9 | data: any, | 46 | data: any, |
@@ -12,18 +49,15 @@ async function broadcastToFollowers ( | |||
12 | t: Transaction, | 49 | t: Transaction, |
13 | followersException: AccountInstance[] = [] | 50 | followersException: AccountInstance[] = [] |
14 | ) { | 51 | ) { |
15 | const toAccountFollowerIds = toAccountFollowers.map(a => a.id) | 52 | const uris = await computeFollowerUris(toAccountFollowers, followersException) |
16 | 53 | if (uris.length === 0) { | |
17 | const result = await db.AccountFollow.listAcceptedFollowerSharedInboxUrls(toAccountFollowerIds) | 54 | logger.info('0 followers for %s, no broadcasting.', toAccountFollowers.map(a => a.id).join(', ')) |
18 | if (result.data.length === 0) { | 55 | return |
19 | logger.info('Not broadcast because of 0 followers for %s.', toAccountFollowerIds.join(', ')) | ||
20 | return undefined | ||
21 | } | 56 | } |
22 | 57 | ||
23 | const followersSharedInboxException = followersException.map(f => f.sharedInboxUrl) | 58 | logger.debug('Creating broadcast job.', { uris }) |
24 | const uris = result.data.filter(sharedInbox => followersSharedInboxException.indexOf(sharedInbox) === -1) | ||
25 | 59 | ||
26 | const jobPayload = { | 60 | const jobPayload: ActivityPubHttpPayload = { |
27 | uris, | 61 | uris, |
28 | signatureAccountId: byAccount.id, | 62 | signatureAccountId: byAccount.id, |
29 | body: data | 63 | body: data |
@@ -33,7 +67,9 @@ async function broadcastToFollowers ( | |||
33 | } | 67 | } |
34 | 68 | ||
35 | async function unicastTo (data: any, byAccount: AccountInstance, toAccountUrl: string, t: Transaction) { | 69 | async function unicastTo (data: any, byAccount: AccountInstance, toAccountUrl: string, t: Transaction) { |
36 | const jobPayload = { | 70 | logger.debug('Creating unicast job.', { uri: toAccountUrl }) |
71 | |||
72 | const jobPayload: ActivityPubHttpPayload = { | ||
37 | uris: [ toAccountUrl ], | 73 | uris: [ toAccountUrl ], |
38 | signatureAccountId: byAccount.id, | 74 | signatureAccountId: byAccount.id, |
39 | body: data | 75 | body: data |
@@ -42,21 +78,21 @@ async function unicastTo (data: any, byAccount: AccountInstance, toAccountUrl: s | |||
42 | return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload) | 78 | return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload) |
43 | } | 79 | } |
44 | 80 | ||
45 | function getOriginVideoAudience (video: VideoInstance) { | 81 | function getOriginVideoAudience (video: VideoInstance, accountsInvolvedInVideo: AccountInstance[]) { |
46 | return { | 82 | return { |
47 | to: [ video.VideoChannel.Account.url ], | 83 | to: [ video.VideoChannel.Account.url ], |
48 | cc: [ video.VideoChannel.Account.url + '/followers' ] | 84 | cc: accountsInvolvedInVideo.map(a => a.followersUrl) |
49 | } | 85 | } |
50 | } | 86 | } |
51 | 87 | ||
52 | function getVideoFollowersAudience (video: VideoInstance) { | 88 | function getVideoFollowersAudience (accountsInvolvedInVideo: AccountInstance[]) { |
53 | return { | 89 | return { |
54 | to: [ video.VideoChannel.Account.url + '/followers' ], | 90 | to: accountsInvolvedInVideo.map(a => a.followersUrl), |
55 | cc: [] | 91 | cc: [] |
56 | } | 92 | } |
57 | } | 93 | } |
58 | 94 | ||
59 | async function getAccountsToForwardVideoAction (byAccount: AccountInstance, video: VideoInstance) { | 95 | async function getAccountsInvolvedInVideo (video: VideoInstance) { |
60 | const accountsToForwardView = await db.VideoShare.loadAccountsByShare(video.id) | 96 | const accountsToForwardView = await db.VideoShare.loadAccountsByShare(video.id) |
61 | accountsToForwardView.push(video.VideoChannel.Account) | 97 | accountsToForwardView.push(video.VideoChannel.Account) |
62 | 98 | ||
@@ -81,6 +117,16 @@ async function getAudience (accountSender: AccountInstance, isPublic = true) { | |||
81 | return { to, cc } | 117 | return { to, cc } |
82 | } | 118 | } |
83 | 119 | ||
120 | async function computeFollowerUris (toAccountFollower: AccountInstance[], followersException: AccountInstance[]) { | ||
121 | const toAccountFollowerIds = toAccountFollower.map(a => a.id) | ||
122 | |||
123 | const result = await db.AccountFollow.listAcceptedFollowerSharedInboxUrls(toAccountFollowerIds) | ||
124 | const followersSharedInboxException = followersException.map(f => f.sharedInboxUrl) | ||
125 | const uris = result.data.filter(sharedInbox => followersSharedInboxException.indexOf(sharedInbox) === -1) | ||
126 | |||
127 | return uris | ||
128 | } | ||
129 | |||
84 | // --------------------------------------------------------------------------- | 130 | // --------------------------------------------------------------------------- |
85 | 131 | ||
86 | export { | 132 | export { |
@@ -88,6 +134,7 @@ export { | |||
88 | unicastTo, | 134 | unicastTo, |
89 | getAudience, | 135 | getAudience, |
90 | getOriginVideoAudience, | 136 | getOriginVideoAudience, |
91 | getAccountsToForwardVideoAction, | 137 | getAccountsInvolvedInVideo, |
92 | getVideoFollowersAudience | 138 | getVideoFollowersAudience, |
139 | forwardActivity | ||
93 | } | 140 | } |