1 import * as Sequelize from 'sequelize'
3 import { database as db } from '../../initializers'
9 import { httpRequestJobScheduler } from '../jobs'
10 import { signObject, activityPubContextify } from '../../helpers'
11 import { Activity } from '../../../shared'
13 function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
14 const videoChannelObject = videoChannel.toActivityPubObject()
15 const data = createActivityData(videoChannel.url, videoChannel.Account, videoChannelObject)
17 return broadcastToFollowers(data, videoChannel.Account, t)
20 function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
21 const videoChannelObject = videoChannel.toActivityPubObject()
22 const data = updateActivityData(videoChannel.url, videoChannel.Account, videoChannelObject)
24 return broadcastToFollowers(data, videoChannel.Account, t)
27 function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
28 const data = deleteActivityData(videoChannel.url, videoChannel.Account)
30 return broadcastToFollowers(data, videoChannel.Account, t)
33 function sendAddVideo (video: VideoInstance, t: Sequelize.Transaction) {
34 const videoObject = video.toActivityPubObject()
35 const data = addActivityData(video.url, video.VideoChannel.Account, video.VideoChannel.url, videoObject)
37 return broadcastToFollowers(data, video.VideoChannel.Account, t)
40 function sendUpdateVideo (video: VideoInstance, t: Sequelize.Transaction) {
41 const videoObject = video.toActivityPubObject()
42 const data = updateActivityData(video.url, video.VideoChannel.Account, videoObject)
44 return broadcastToFollowers(data, video.VideoChannel.Account, t)
47 function sendDeleteVideo (video: VideoInstance, t: Sequelize.Transaction) {
48 const data = deleteActivityData(video.url, video.VideoChannel.Account)
50 return broadcastToFollowers(data, video.VideoChannel.Account, t)
53 function sendDeleteAccount (account: AccountInstance, t: Sequelize.Transaction) {
54 const data = deleteActivityData(account.url, account)
56 return broadcastToFollowers(data, account, t)
59 function sendAccept (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
60 const data = acceptActivityData(fromAccount)
62 return unicastTo(data, toAccount, t)
65 function sendFollow (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
66 const data = followActivityData(toAccount.url, fromAccount)
68 return unicastTo(data, toAccount, t)
71 // ---------------------------------------------------------------------------
74 sendCreateVideoChannel,
75 sendUpdateVideoChannel,
76 sendDeleteVideoChannel,
85 // ---------------------------------------------------------------------------
87 async function broadcastToFollowers (data: any, fromAccount: AccountInstance, t: Sequelize.Transaction) {
88 const result = await db.Account.listFollowerUrlsForApi(fromAccount.id, 0)
95 return httpRequestJobScheduler.createJob(t, 'httpRequestBroadcastHandler', jobPayload)
98 async function unicastTo (data: any, toAccount: AccountInstance, t: Sequelize.Transaction) {
100 uris: [ toAccount.url ],
104 return httpRequestJobScheduler.createJob(t, 'httpRequestUnicastHandler', jobPayload)
107 function buildSignedActivity (byAccount: AccountInstance, data: Object) {
108 const activity = activityPubContextify(data)
110 return signObject(byAccount, activity) as Promise<Activity>
113 async function getPublicActivityTo (account: AccountInstance) {
114 const inboxUrls = await account.getFollowerSharedInboxUrls()
116 return inboxUrls.concat('https://www.w3.org/ns/activitystreams#Public')
119 async function createActivityData (url: string, byAccount: AccountInstance, object: any) {
120 const to = await getPublicActivityTo(byAccount)
124 actor: byAccount.url,
129 return buildSignedActivity(byAccount, base)
132 async function updateActivityData (url: string, byAccount: AccountInstance, object: any) {
133 const to = await getPublicActivityTo(byAccount)
137 actor: byAccount.url,
142 return buildSignedActivity(byAccount, base)
145 async function deleteActivityData (url: string, byAccount: AccountInstance) {
152 return buildSignedActivity(byAccount, base)
155 async function addActivityData (url: string, byAccount: AccountInstance, target: string, object: any) {
156 const to = await getPublicActivityTo(byAccount)
160 actor: byAccount.url,
166 return buildSignedActivity(byAccount, base)
169 async function followActivityData (url: string, byAccount: AccountInstance) {
173 actor: byAccount.url,
177 return buildSignedActivity(byAccount, base)
180 async function acceptActivityData (byAccount: AccountInstance) {
187 return buildSignedActivity(byAccount, base)