]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send/misc.ts
Federate likes/dislikes
[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 import { VideoInstance } from '../../../models/video/video-interface'
7
8 async function broadcastToFollowers (
9 data: any,
10 byAccount: AccountInstance,
11 toAccountFollowers: AccountInstance[],
12 t: Transaction,
13 followersException: AccountInstance[] = []
14 ) {
15 const toAccountFollowerIds = toAccountFollowers.map(a => a.id)
16
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
23 const followersSharedInboxException = followersException.map(f => f.sharedInboxUrl)
24 const uris = result.data.filter(sharedInbox => followersSharedInboxException.indexOf(sharedInbox) === -1)
25
26 const jobPayload = {
27 uris,
28 signatureAccountId: byAccount.id,
29 body: data
30 }
31
32 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
33 }
34
35 async 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
45 function getOriginVideoAudience (video: VideoInstance) {
46 return {
47 to: [ video.VideoChannel.Account.url ],
48 cc: [ video.VideoChannel.Account.url + '/followers' ]
49 }
50 }
51
52 function getVideoFollowersAudience (video: VideoInstance) {
53 return {
54 to: [ video.VideoChannel.Account.url + '/followers' ],
55 cc: []
56 }
57 }
58
59 async 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
66 async 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
86 export {
87 broadcastToFollowers,
88 unicastTo,
89 getAudience,
90 getOriginVideoAudience,
91 getAccountsToForwardVideoAction,
92 getVideoFollowersAudience
93 }