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'
8 async function broadcastToFollowers (
10 byAccount: AccountInstance,
11 toAccountFollowers: AccountInstance[],
13 followersException: AccountInstance[] = []
15 const toAccountFollowerIds = toAccountFollowers.map(a => a.id)
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(', '))
23 const followersSharedInboxException = followersException.map(f => f.sharedInboxUrl)
24 const uris = result.data.filter(sharedInbox => followersSharedInboxException.indexOf(sharedInbox) === -1)
28 signatureAccountId: byAccount.id,
32 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
35 async function unicastTo (data: any, byAccount: AccountInstance, toAccountUrl: string, t: Transaction) {
37 uris: [ toAccountUrl ],
38 signatureAccountId: byAccount.id,
42 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload)
45 function getOriginVideoAudience (video: VideoInstance) {
47 to: [ video.VideoChannel.Account.url ],
48 cc: [ video.VideoChannel.Account.url + '/followers' ]
52 function getVideoFollowersAudience (video: VideoInstance) {
54 to: [ video.VideoChannel.Account.url + '/followers' ],
59 async function getAccountsToForwardVideoAction (byAccount: AccountInstance, video: VideoInstance) {
60 const accountsToForwardView = await db.VideoShare.loadAccountsByShare(video.id)
61 accountsToForwardView.push(video.VideoChannel.Account)
63 return accountsToForwardView
66 async function getAudience (accountSender: AccountInstance, isPublic = true) {
67 const followerInboxUrls = await accountSender.getFollowerSharedInboxUrls()
69 // Thanks Mastodon: https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/tag_manager.rb#L47
74 to = [ ACTIVITY_PUB.PUBLIC ]
75 cc = followerInboxUrls
77 to = followerInboxUrls
78 cc = [ ACTIVITY_PUB.PUBLIC ]
84 // ---------------------------------------------------------------------------
90 getOriginVideoAudience,
91 getAccountsToForwardVideoAction,
92 getVideoFollowersAudience