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

77 lines
2.7 KiB
TypeScript
Raw Normal View History

2017-11-20 09:43:39 +01:00
import { Transaction } from 'sequelize'
2017-12-12 17:53:50 +01:00
import { ActivityAnnounce, ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
2017-12-14 17:38:41 +01:00
import { VideoPrivacy } from '../../../../shared/models/videos'
import { ActorModel } from '../../../models/activitypub/actor'
2017-12-12 17:53:50 +01:00
import { VideoModel } from '../../../models/video/video'
2017-11-21 18:23:10 +01:00
import { getAnnounceActivityPubUrl } from '../url'
import {
broadcastToFollowers,
2017-12-14 17:38:41 +01:00
getActorsInvolvedInVideo,
getAudience,
getObjectFollowersAudience,
getOriginVideoAudience,
unicastTo
} from './misc'
2017-11-20 09:43:39 +01:00
import { createActivityData } from './send-create'
2017-12-14 17:38:41 +01:00
async function buildVideoAnnounceToFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
const url = getAnnounceActivityPubUrl(video.url, byActor)
const videoObject = video.toActivityPubObject()
2017-11-20 09:43:39 +01:00
2017-12-14 17:38:41 +01:00
const announcedAudience = await getAudience(byActor, t, video.privacy === VideoPrivacy.PUBLIC)
const announcedActivity = await createActivityData(url, video.VideoChannel.Account.Actor, videoObject, t, announcedAudience)
2017-11-20 09:43:39 +01:00
2017-12-14 17:38:41 +01:00
const accountsToForwardView = await getActorsInvolvedInVideo(video, t)
const audience = getObjectFollowersAudience(accountsToForwardView)
2017-12-14 17:38:41 +01:00
return announceActivityData(url, byActor, announcedActivity, t, audience)
}
2017-12-14 17:38:41 +01:00
async function sendVideoAnnounceToFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
const data = await buildVideoAnnounceToFollowers(byActor, video, t)
2017-12-14 17:38:41 +01:00
return broadcastToFollowers(data, byActor, [ byActor ], t)
2017-11-20 09:43:39 +01:00
}
2017-12-14 17:38:41 +01:00
async function sendVideoAnnounceToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
const url = getAnnounceActivityPubUrl(video.url, byActor)
2017-12-14 17:38:41 +01:00
const videoObject = video.toActivityPubObject()
const announcedActivity = await createActivityData(url, video.VideoChannel.Account.Actor, videoObject, t)
2017-12-14 17:38:41 +01:00
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
const data = await createActivityData(url, byActor, announcedActivity, t, audience)
2017-12-14 17:38:41 +01:00
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
}
async function announceActivityData (
url: string,
2017-12-14 17:38:41 +01:00
byActor: ActorModel,
object: ActivityCreate,
t: Transaction,
audience?: ActivityAudience
2017-12-12 17:53:50 +01:00
): Promise<ActivityAnnounce> {
if (!audience) {
2017-12-14 17:38:41 +01:00
audience = await getAudience(byActor, 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,
2017-12-14 17:38:41 +01:00
actor: byActor.url,
2017-11-20 09:43:39 +01:00
object
}
}
2017-11-21 18:23:10 +01:00
// ---------------------------------------------------------------------------
export {
sendVideoAnnounceToFollowers,
sendVideoAnnounceToOrigin,
announceActivityData,
2017-12-14 17:38:41 +01:00
buildVideoAnnounceToFollowers
2017-11-21 18:23:10 +01:00
}