2017-11-09 10:51:58 -06:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
|
2017-11-10 10:27:49 -06:00
|
|
|
import { database as db } from '../../initializers'
|
2017-11-09 10:51:58 -06:00
|
|
|
import {
|
|
|
|
AccountInstance,
|
|
|
|
VideoInstance,
|
|
|
|
VideoChannelInstance
|
|
|
|
} from '../../models'
|
|
|
|
import { httpRequestJobScheduler } from '../jobs'
|
|
|
|
import { signObject, activityPubContextify } from '../../helpers'
|
|
|
|
import { Activity } from '../../../shared'
|
|
|
|
|
2017-11-14 10:31:26 -06:00
|
|
|
async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
|
2017-11-09 10:51:58 -06:00
|
|
|
const videoChannelObject = videoChannel.toActivityPubObject()
|
2017-11-14 10:31:26 -06:00
|
|
|
const data = await createActivityData(videoChannel.url, videoChannel.Account, videoChannelObject)
|
2017-11-09 10:51:58 -06:00
|
|
|
|
2017-11-10 10:27:49 -06:00
|
|
|
return broadcastToFollowers(data, videoChannel.Account, t)
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
|
|
|
|
2017-11-14 10:31:26 -06:00
|
|
|
async function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
|
2017-11-09 10:51:58 -06:00
|
|
|
const videoChannelObject = videoChannel.toActivityPubObject()
|
2017-11-14 10:31:26 -06:00
|
|
|
const data = await updateActivityData(videoChannel.url, videoChannel.Account, videoChannelObject)
|
2017-11-09 10:51:58 -06:00
|
|
|
|
2017-11-10 10:27:49 -06:00
|
|
|
return broadcastToFollowers(data, videoChannel.Account, t)
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
|
|
|
|
2017-11-14 10:31:26 -06:00
|
|
|
async function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
|
|
|
|
const data = await deleteActivityData(videoChannel.url, videoChannel.Account)
|
2017-11-09 10:51:58 -06:00
|
|
|
|
2017-11-10 10:27:49 -06:00
|
|
|
return broadcastToFollowers(data, videoChannel.Account, t)
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
|
|
|
|
2017-11-14 10:31:26 -06:00
|
|
|
async function sendAddVideo (video: VideoInstance, t: Sequelize.Transaction) {
|
2017-11-09 10:51:58 -06:00
|
|
|
const videoObject = video.toActivityPubObject()
|
2017-11-14 10:31:26 -06:00
|
|
|
const data = await addActivityData(video.url, video.VideoChannel.Account, video.VideoChannel.url, videoObject)
|
2017-11-09 10:51:58 -06:00
|
|
|
|
2017-11-10 10:27:49 -06:00
|
|
|
return broadcastToFollowers(data, video.VideoChannel.Account, t)
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
|
|
|
|
2017-11-14 10:31:26 -06:00
|
|
|
async function sendUpdateVideo (video: VideoInstance, t: Sequelize.Transaction) {
|
2017-11-09 10:51:58 -06:00
|
|
|
const videoObject = video.toActivityPubObject()
|
2017-11-14 10:31:26 -06:00
|
|
|
const data = await updateActivityData(video.url, video.VideoChannel.Account, videoObject)
|
2017-11-09 10:51:58 -06:00
|
|
|
|
2017-11-10 10:27:49 -06:00
|
|
|
return broadcastToFollowers(data, video.VideoChannel.Account, t)
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
|
|
|
|
2017-11-14 10:31:26 -06:00
|
|
|
async function sendDeleteVideo (video: VideoInstance, t: Sequelize.Transaction) {
|
|
|
|
const data = await deleteActivityData(video.url, video.VideoChannel.Account)
|
2017-11-09 10:51:58 -06:00
|
|
|
|
2017-11-10 10:27:49 -06:00
|
|
|
return broadcastToFollowers(data, video.VideoChannel.Account, t)
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
|
|
|
|
2017-11-14 10:31:26 -06:00
|
|
|
async function sendDeleteAccount (account: AccountInstance, t: Sequelize.Transaction) {
|
|
|
|
const data = await deleteActivityData(account.url, account)
|
2017-11-13 10:39:41 -06:00
|
|
|
|
|
|
|
return broadcastToFollowers(data, account, t)
|
|
|
|
}
|
|
|
|
|
2017-11-14 10:31:26 -06:00
|
|
|
async function sendAccept (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
|
|
|
|
const data = await acceptActivityData(fromAccount)
|
2017-11-13 11:48:28 -06:00
|
|
|
|
|
|
|
return unicastTo(data, toAccount, t)
|
|
|
|
}
|
|
|
|
|
2017-11-14 10:31:26 -06:00
|
|
|
async function sendFollow (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
|
|
|
|
const data = await followActivityData(toAccount.url, fromAccount)
|
2017-11-13 11:48:28 -06:00
|
|
|
|
|
|
|
return unicastTo(data, toAccount, t)
|
|
|
|
}
|
|
|
|
|
2017-11-09 10:51:58 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2017-11-10 10:27:49 -06:00
|
|
|
sendCreateVideoChannel,
|
|
|
|
sendUpdateVideoChannel,
|
|
|
|
sendDeleteVideoChannel,
|
|
|
|
sendAddVideo,
|
|
|
|
sendUpdateVideo,
|
2017-11-13 10:39:41 -06:00
|
|
|
sendDeleteVideo,
|
2017-11-13 11:48:28 -06:00
|
|
|
sendDeleteAccount,
|
|
|
|
sendAccept,
|
|
|
|
sendFollow
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-11-10 10:27:49 -06:00
|
|
|
async function broadcastToFollowers (data: any, fromAccount: AccountInstance, t: Sequelize.Transaction) {
|
2017-11-14 02:11:43 -06:00
|
|
|
const result = await db.Account.listAcceptedFollowerUrlsForApi(fromAccount.id, 0)
|
2017-11-10 10:27:49 -06:00
|
|
|
|
|
|
|
const jobPayload = {
|
|
|
|
uris: result.data,
|
|
|
|
body: data
|
|
|
|
}
|
|
|
|
|
|
|
|
return httpRequestJobScheduler.createJob(t, 'httpRequestBroadcastHandler', jobPayload)
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
|
|
|
|
2017-11-13 11:48:28 -06:00
|
|
|
async function unicastTo (data: any, toAccount: AccountInstance, t: Sequelize.Transaction) {
|
|
|
|
const jobPayload = {
|
2017-11-14 10:31:26 -06:00
|
|
|
uris: [ toAccount.inboxUrl ],
|
2017-11-13 11:48:28 -06:00
|
|
|
body: data
|
|
|
|
}
|
|
|
|
|
|
|
|
return httpRequestJobScheduler.createJob(t, 'httpRequestUnicastHandler', jobPayload)
|
|
|
|
}
|
|
|
|
|
2017-11-09 10:51:58 -06:00
|
|
|
function buildSignedActivity (byAccount: AccountInstance, data: Object) {
|
|
|
|
const activity = activityPubContextify(data)
|
|
|
|
|
|
|
|
return signObject(byAccount, activity) as Promise<Activity>
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getPublicActivityTo (account: AccountInstance) {
|
|
|
|
const inboxUrls = await account.getFollowerSharedInboxUrls()
|
|
|
|
|
|
|
|
return inboxUrls.concat('https://www.w3.org/ns/activitystreams#Public')
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createActivityData (url: string, byAccount: AccountInstance, object: any) {
|
|
|
|
const to = await getPublicActivityTo(byAccount)
|
|
|
|
const base = {
|
|
|
|
type: 'Create',
|
|
|
|
id: url,
|
|
|
|
actor: byAccount.url,
|
|
|
|
to,
|
|
|
|
object
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildSignedActivity(byAccount, base)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateActivityData (url: string, byAccount: AccountInstance, object: any) {
|
|
|
|
const to = await getPublicActivityTo(byAccount)
|
|
|
|
const base = {
|
|
|
|
type: 'Update',
|
|
|
|
id: url,
|
|
|
|
actor: byAccount.url,
|
|
|
|
to,
|
|
|
|
object
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildSignedActivity(byAccount, base)
|
|
|
|
}
|
|
|
|
|
2017-11-13 10:39:41 -06:00
|
|
|
async function deleteActivityData (url: string, byAccount: AccountInstance) {
|
2017-11-09 10:51:58 -06:00
|
|
|
const base = {
|
2017-11-13 10:39:41 -06:00
|
|
|
type: 'Delete',
|
2017-11-09 10:51:58 -06:00
|
|
|
id: url,
|
2017-11-13 10:39:41 -06:00
|
|
|
actor: byAccount.url
|
2017-11-09 10:51:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return buildSignedActivity(byAccount, base)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addActivityData (url: string, byAccount: AccountInstance, target: string, object: any) {
|
|
|
|
const to = await getPublicActivityTo(byAccount)
|
|
|
|
const base = {
|
|
|
|
type: 'Add',
|
|
|
|
id: url,
|
|
|
|
actor: byAccount.url,
|
|
|
|
to,
|
|
|
|
object,
|
|
|
|
target
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildSignedActivity(byAccount, base)
|
|
|
|
}
|
2017-11-13 11:48:28 -06:00
|
|
|
|
|
|
|
async function followActivityData (url: string, byAccount: AccountInstance) {
|
|
|
|
const base = {
|
|
|
|
type: 'Follow',
|
|
|
|
id: byAccount.url,
|
|
|
|
actor: byAccount.url,
|
|
|
|
object: url
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildSignedActivity(byAccount, base)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function acceptActivityData (byAccount: AccountInstance) {
|
|
|
|
const base = {
|
|
|
|
type: 'Accept',
|
|
|
|
id: byAccount.url,
|
|
|
|
actor: byAccount.url
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildSignedActivity(byAccount, base)
|
|
|
|
}
|