]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/misc.ts
f3dc5c148e59dc94fffe76f554b006d0ed215711
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / misc.ts
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
7 async function broadcastToFollowers (
8 data: any,
9 byAccount: AccountInstance,
10 toAccountFollowers: AccountInstance[],
11 t: Transaction,
12 followersException: AccountInstance[] = []
13 ) {
14 const toAccountFollowerIds = toAccountFollowers.map(a => a.id)
15
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
22 const followersSharedInboxException = followersException.map(f => f.sharedInboxUrl)
23 const uris = result.data.filter(sharedInbox => followersSharedInboxException.indexOf(sharedInbox) === -1)
24
25 const jobPayload = {
26 uris,
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 }