]>
Commit | Line | Data |
---|---|---|
54141398 C |
1 | import { Transaction } from 'sequelize' |
2 | import { logger } from '../../../helpers/logger' | |
3 | import { ACTIVITY_PUB, database as db } from '../../../initializers' | |
4 | import { AccountInstance } from '../../../models/account/account-interface' | |
5 | import { activitypubHttpJobScheduler } from '../../jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler' | |
6 | ||
40ff5707 C |
7 | async function broadcastToFollowers ( |
8 | data: any, | |
9 | byAccount: AccountInstance, | |
10 | toAccountFollowers: AccountInstance[], | |
11 | t: Transaction, | |
12 | followersException: AccountInstance[] = [] | |
13 | ) { | |
54141398 | 14 | const toAccountFollowerIds = toAccountFollowers.map(a => a.id) |
40ff5707 | 15 | |
54141398 C |
16 | const result = await db.AccountFollow.listAcceptedFollowerSharedInboxUrls(toAccountFollowerIds) |
17 | if (result.data.length === 0) { | |
18 | logger.info('Not broadcast because of 0 followers for %s.', toAccountFollowerIds.join(', ')) | |
19 | return undefined | |
20 | } | |
21 | ||
40ff5707 C |
22 | const followersSharedInboxException = followersException.map(f => f.sharedInboxUrl) |
23 | const uris = result.data.filter(sharedInbox => followersSharedInboxException.indexOf(sharedInbox) === -1) | |
24 | ||
54141398 | 25 | const jobPayload = { |
40ff5707 | 26 | uris, |
54141398 C |
27 | signatureAccountId: byAccount.id, |
28 | body: data | |
29 | } | |
30 | ||
31 | return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload) | |
32 | } | |
33 | ||
34 | async function unicastTo (data: any, byAccount: AccountInstance, toAccountUrl: string, t: Transaction) { | |
35 | const jobPayload = { | |
36 | uris: [ toAccountUrl ], | |
37 | signatureAccountId: byAccount.id, | |
38 | body: data | |
39 | } | |
40 | ||
41 | return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload) | |
42 | } | |
43 | ||
44 | async function getAudience (accountSender: AccountInstance, isPublic = true) { | |
45 | const followerInboxUrls = await accountSender.getFollowerSharedInboxUrls() | |
46 | ||
47 | // Thanks Mastodon: https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/tag_manager.rb#L47 | |
48 | let to = [] | |
49 | let cc = [] | |
50 | ||
51 | if (isPublic) { | |
52 | to = [ ACTIVITY_PUB.PUBLIC ] | |
53 | cc = followerInboxUrls | |
54 | } else { // Unlisted | |
55 | to = followerInboxUrls | |
56 | cc = [ ACTIVITY_PUB.PUBLIC ] | |
57 | } | |
58 | ||
59 | return { to, cc } | |
60 | } | |
61 | ||
62 | // --------------------------------------------------------------------------- | |
63 | ||
64 | export { | |
65 | broadcastToFollowers, | |
66 | unicastTo, | |
67 | getAudience | |
68 | } |