]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/misc.ts
Federate likes/dislikes
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / misc.ts
CommitLineData
54141398
C
1import { Transaction } from 'sequelize'
2import { logger } from '../../../helpers/logger'
3import { ACTIVITY_PUB, database as db } from '../../../initializers'
4import { AccountInstance } from '../../../models/account/account-interface'
5import { activitypubHttpJobScheduler } from '../../jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler'
0032ebe9 6import { VideoInstance } from '../../../models/video/video-interface'
54141398 7
40ff5707
C
8async function broadcastToFollowers (
9 data: any,
10 byAccount: AccountInstance,
11 toAccountFollowers: AccountInstance[],
12 t: Transaction,
13 followersException: AccountInstance[] = []
14) {
54141398 15 const toAccountFollowerIds = toAccountFollowers.map(a => a.id)
40ff5707 16
54141398
C
17 const result = await db.AccountFollow.listAcceptedFollowerSharedInboxUrls(toAccountFollowerIds)
18 if (result.data.length === 0) {
19 logger.info('Not broadcast because of 0 followers for %s.', toAccountFollowerIds.join(', '))
20 return undefined
21 }
22
40ff5707
C
23 const followersSharedInboxException = followersException.map(f => f.sharedInboxUrl)
24 const uris = result.data.filter(sharedInbox => followersSharedInboxException.indexOf(sharedInbox) === -1)
25
54141398 26 const jobPayload = {
40ff5707 27 uris,
54141398
C
28 signatureAccountId: byAccount.id,
29 body: data
30 }
31
32 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
33}
34
35async function unicastTo (data: any, byAccount: AccountInstance, toAccountUrl: string, t: Transaction) {
36 const jobPayload = {
37 uris: [ toAccountUrl ],
38 signatureAccountId: byAccount.id,
39 body: data
40 }
41
42 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload)
43}
44
0032ebe9
C
45function getOriginVideoAudience (video: VideoInstance) {
46 return {
47 to: [ video.VideoChannel.Account.url ],
48 cc: [ video.VideoChannel.Account.url + '/followers' ]
49 }
50}
51
52function getVideoFollowersAudience (video: VideoInstance) {
53 return {
54 to: [ video.VideoChannel.Account.url + '/followers' ],
55 cc: []
56 }
57}
58
59async function getAccountsToForwardVideoAction (byAccount: AccountInstance, video: VideoInstance) {
60 const accountsToForwardView = await db.VideoShare.loadAccountsByShare(video.id)
61 accountsToForwardView.push(video.VideoChannel.Account)
62
63 return accountsToForwardView
64}
65
54141398
C
66async function getAudience (accountSender: AccountInstance, isPublic = true) {
67 const followerInboxUrls = await accountSender.getFollowerSharedInboxUrls()
68
69 // Thanks Mastodon: https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/tag_manager.rb#L47
70 let to = []
71 let cc = []
72
73 if (isPublic) {
74 to = [ ACTIVITY_PUB.PUBLIC ]
75 cc = followerInboxUrls
76 } else { // Unlisted
77 to = followerInboxUrls
78 cc = [ ACTIVITY_PUB.PUBLIC ]
79 }
80
81 return { to, cc }
82}
83
84// ---------------------------------------------------------------------------
85
86export {
87 broadcastToFollowers,
88 unicastTo,
0032ebe9
C
89 getAudience,
90 getOriginVideoAudience,
91 getAccountsToForwardVideoAction,
92 getVideoFollowersAudience
54141398 93}