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'
12 import { VideoAbuseInstance } from '../../models/video/video-abuse-interface'
13 import { getActivityPubUrl } from '../../helpers/activitypub'
14 import { logger } from '../../helpers/logger'
16 async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
17 const videoChannelObject = videoChannel.toActivityPubObject()
18 const data = await createActivityData(videoChannel.url, videoChannel.Account, videoChannelObject)
20 return broadcastToFollowers(data, videoChannel.Account, t)
23 async function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
24 const videoChannelObject = videoChannel.toActivityPubObject()
25 const data = await updateActivityData(videoChannel.url, videoChannel.Account, videoChannelObject)
27 return broadcastToFollowers(data, videoChannel.Account, t)
30 async function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
31 const data = await deleteActivityData(videoChannel.url, videoChannel.Account)
33 return broadcastToFollowers(data, videoChannel.Account, t)
36 async function sendAddVideo (video: VideoInstance, t: Sequelize.Transaction) {
37 const videoObject = video.toActivityPubObject()
38 const data = await addActivityData(video.url, video.VideoChannel.Account, video.VideoChannel.url, videoObject)
40 return broadcastToFollowers(data, video.VideoChannel.Account, t)
43 async function sendUpdateVideo (video: VideoInstance, t: Sequelize.Transaction) {
44 const videoObject = video.toActivityPubObject()
45 const data = await updateActivityData(video.url, video.VideoChannel.Account, videoObject)
47 return broadcastToFollowers(data, video.VideoChannel.Account, t)
50 async function sendDeleteVideo (video: VideoInstance, t: Sequelize.Transaction) {
51 const data = await deleteActivityData(video.url, video.VideoChannel.Account)
53 return broadcastToFollowers(data, video.VideoChannel.Account, t)
56 async function sendDeleteAccount (account: AccountInstance, t: Sequelize.Transaction) {
57 const data = await deleteActivityData(account.url, account)
59 return broadcastToFollowers(data, account, t)
62 async function sendVideoAbuse (
63 fromAccount: AccountInstance,
64 videoAbuse: VideoAbuseInstance,
66 t: Sequelize.Transaction
68 const url = getActivityPubUrl('videoAbuse', videoAbuse.id.toString())
69 const data = await createActivityData(url, fromAccount, video.url)
71 return unicastTo(data, video.VideoChannel.Account.sharedInboxUrl, t)
74 async function sendAccept (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
75 const data = await acceptActivityData(fromAccount)
77 return unicastTo(data, toAccount.inboxUrl, t)
80 async function sendFollow (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
81 const data = await followActivityData(toAccount.url, fromAccount)
83 return unicastTo(data, toAccount.inboxUrl, t)
86 // ---------------------------------------------------------------------------
89 sendCreateVideoChannel,
90 sendUpdateVideoChannel,
91 sendDeleteVideoChannel,
101 // ---------------------------------------------------------------------------
103 async function broadcastToFollowers (data: any, fromAccount: AccountInstance, t: Sequelize.Transaction) {
104 const result = await db.AccountFollow.listAcceptedFollowerUrlsForApi(fromAccount.id)
105 if (result.data.length === 0) {
106 logger.info('Not broadcast because of 0 followers.')
115 return httpRequestJobScheduler.createJob(t, 'httpRequestBroadcastHandler', jobPayload)
118 async function unicastTo (data: any, toAccountUrl: string, t: Sequelize.Transaction) {
120 uris: [ toAccountUrl ],
124 return httpRequestJobScheduler.createJob(t, 'httpRequestUnicastHandler', jobPayload)
127 function buildSignedActivity (byAccount: AccountInstance, data: Object) {
128 const activity = activityPubContextify(data)
130 return signObject(byAccount, activity) as Promise<Activity>
133 async function getPublicActivityTo (account: AccountInstance) {
134 const inboxUrls = await account.getFollowerSharedInboxUrls()
136 return inboxUrls.concat('https://www.w3.org/ns/activitystreams#Public')
139 async function createActivityData (url: string, byAccount: AccountInstance, object: any) {
140 const to = await getPublicActivityTo(byAccount)
144 actor: byAccount.url,
149 return buildSignedActivity(byAccount, base)
152 async function updateActivityData (url: string, byAccount: AccountInstance, object: any) {
153 const to = await getPublicActivityTo(byAccount)
157 actor: byAccount.url,
162 return buildSignedActivity(byAccount, base)
165 async function deleteActivityData (url: string, byAccount: AccountInstance) {
172 return buildSignedActivity(byAccount, base)
175 async function addActivityData (url: string, byAccount: AccountInstance, target: string, object: any) {
176 const to = await getPublicActivityTo(byAccount)
180 actor: byAccount.url,
186 return buildSignedActivity(byAccount, base)
189 async function followActivityData (url: string, byAccount: AccountInstance) {
193 actor: byAccount.url,
197 return buildSignedActivity(byAccount, base)
200 async function acceptActivityData (byAccount: AccountInstance) {
207 return buildSignedActivity(byAccount, base)