PeerTube/server/lib/activitypub/send/send-announce.ts

109 lines
4.4 KiB
TypeScript
Raw Normal View History

2017-11-20 09:43:39 +01:00
import { Transaction } from 'sequelize'
2017-11-21 18:23:10 +01:00
import { ActivityAdd } from '../../../../shared/index'
2017-12-12 17:53:50 +01:00
import { ActivityAnnounce, ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
import { AccountModel } from '../../../models/account/account'
import { VideoModel } from '../../../models/video/video'
import { VideoChannelModel } from '../../../models/video/video-channel'
2017-11-21 18:23:10 +01:00
import { getAnnounceActivityPubUrl } from '../url'
import {
broadcastToFollowers,
getAccountsInvolvedInVideo,
getAccountsInvolvedInVideoChannel,
getAudience,
getObjectFollowersAudience,
getOriginVideoAudience,
getOriginVideoChannelAudience,
unicastTo
} from './misc'
2017-11-20 09:43:39 +01:00
import { addActivityData } from './send-add'
import { createActivityData } from './send-create'
2017-12-12 17:53:50 +01:00
async function buildVideoAnnounceToFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) {
2017-11-20 09:43:39 +01:00
const url = getAnnounceActivityPubUrl(video.url, byAccount)
const videoChannel = video.VideoChannel
const announcedActivity = await addActivityData(url, videoChannel.Account, video, videoChannel.url, video.toActivityPubObject(), t)
2017-11-20 09:43:39 +01:00
const accountsToForwardView = await getAccountsInvolvedInVideo(video, t)
const audience = getObjectFollowersAudience(accountsToForwardView)
2017-12-12 17:53:50 +01:00
return announceActivityData(url, byAccount, announcedActivity, t, audience)
}
2017-12-12 17:53:50 +01:00
async function sendVideoAnnounceToFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) {
const data = await buildVideoAnnounceToFollowers(byAccount, video, t)
2017-11-20 09:43:39 +01:00
return broadcastToFollowers(data, byAccount, [ byAccount ], t)
}
2017-12-12 17:53:50 +01:00
async function sendVideoAnnounceToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) {
const url = getAnnounceActivityPubUrl(video.url, byAccount)
const videoChannel = video.VideoChannel
const announcedActivity = await addActivityData(url, videoChannel.Account, video, videoChannel.url, video.toActivityPubObject(), t)
const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
const data = await createActivityData(url, byAccount, announcedActivity, t, audience)
return unicastTo(data, byAccount, videoChannel.Account.sharedInboxUrl, t)
}
2017-12-12 17:53:50 +01:00
async function buildVideoChannelAnnounceToFollowers (byAccount: AccountModel, videoChannel: VideoChannelModel, t: Transaction) {
2017-11-20 09:43:39 +01:00
const url = getAnnounceActivityPubUrl(videoChannel.url, byAccount)
const announcedActivity = await createActivityData(url, videoChannel.Account, videoChannel.toActivityPubObject(), t)
2017-11-20 09:43:39 +01:00
const accountsToForwardView = await getAccountsInvolvedInVideoChannel(videoChannel, t)
const audience = getObjectFollowersAudience(accountsToForwardView)
2017-12-12 17:53:50 +01:00
return announceActivityData(url, byAccount, announcedActivity, t, audience)
}
2017-12-12 17:53:50 +01:00
async function sendVideoChannelAnnounceToFollowers (byAccount: AccountModel, videoChannel: VideoChannelModel, t: Transaction) {
const data = await buildVideoChannelAnnounceToFollowers(byAccount, videoChannel, t)
2017-11-20 09:43:39 +01:00
return broadcastToFollowers(data, byAccount, [ byAccount ], t)
}
2017-12-12 17:53:50 +01:00
async function sendVideoChannelAnnounceToOrigin (byAccount: AccountModel, videoChannel: VideoChannelModel, t: Transaction) {
const url = getAnnounceActivityPubUrl(videoChannel.url, byAccount)
const announcedActivity = await createActivityData(url, videoChannel.Account, videoChannel.toActivityPubObject(), t)
const accountsInvolvedInVideo = await getAccountsInvolvedInVideoChannel(videoChannel, t)
const audience = getOriginVideoChannelAudience(videoChannel, accountsInvolvedInVideo)
const data = await createActivityData(url, byAccount, announcedActivity, t, audience)
return unicastTo(data, byAccount, videoChannel.Account.sharedInboxUrl, t)
}
async function announceActivityData (
url: string,
2017-12-12 17:53:50 +01:00
byAccount: AccountModel,
object: ActivityCreate | ActivityAdd,
t: Transaction,
audience?: ActivityAudience
2017-12-12 17:53:50 +01:00
): Promise<ActivityAnnounce> {
if (!audience) {
audience = await getAudience(byAccount, t)
}
2017-12-12 17:53:50 +01:00
return {
2017-11-20 09:43:39 +01:00
type: 'Announce',
to: audience.to,
cc: audience.cc,
2017-11-20 09:43:39 +01:00
id: url,
actor: byAccount.url,
object
}
}
2017-11-21 18:23:10 +01:00
// ---------------------------------------------------------------------------
export {
sendVideoAnnounceToFollowers,
sendVideoChannelAnnounceToFollowers,
sendVideoAnnounceToOrigin,
sendVideoChannelAnnounceToOrigin,
announceActivityData,
buildVideoAnnounceToFollowers,
buildVideoChannelAnnounceToFollowers
2017-11-21 18:23:10 +01:00
}