PeerTube/server/lib/activitypub/audience.ts

97 lines
2.6 KiB
TypeScript
Raw Normal View History

2018-05-25 11:32:36 +02:00
import { Transaction } from 'sequelize'
import { ActivityAudience } from '../../../shared/models/activitypub'
import { ACTIVITY_PUB } from '../../initializers/constants'
2018-05-25 11:32:36 +02:00
import { ActorModel } from '../../models/activitypub/actor'
import { VideoModel } from '../../models/video/video'
import { VideoCommentModel } from '../../models/video/video-comment'
import { VideoShareModel } from '../../models/video/video-share'
2018-09-14 16:51:35 +02:00
function getRemoteVideoAudience (video: VideoModel, actorsInvolvedInVideo: ActorModel[]): ActivityAudience {
2018-05-25 11:32:36 +02:00
return {
to: [ video.VideoChannel.Account.Actor.url ],
cc: actorsInvolvedInVideo.map(a => a.followersUrl)
}
}
function getVideoCommentAudience (
videoComment: VideoCommentModel,
threadParentComments: VideoCommentModel[],
actorsInvolvedInVideo: ActorModel[],
isOrigin = false
2018-09-14 16:51:35 +02:00
): ActivityAudience {
2018-05-25 11:32:36 +02:00
const to = [ ACTIVITY_PUB.PUBLIC ]
const cc: string[] = []
2018-05-25 11:32:36 +02:00
// Owner of the video we comment
if (isOrigin === false) {
cc.push(videoComment.Video.VideoChannel.Account.Actor.url)
}
// Followers of the poster
cc.push(videoComment.Account.Actor.followersUrl)
// Send to actors we reply to
for (const parentComment of threadParentComments) {
cc.push(parentComment.Account.Actor.url)
}
return {
to,
cc: cc.concat(actorsInvolvedInVideo.map(a => a.followersUrl))
}
}
2018-09-14 16:51:35 +02:00
function getAudienceFromFollowersOf (actorsInvolvedInObject: ActorModel[]): ActivityAudience {
2018-05-25 11:32:36 +02:00
return {
to: [ ACTIVITY_PUB.PUBLIC ].concat(actorsInvolvedInObject.map(a => a.followersUrl)),
cc: []
}
}
async function getActorsInvolvedInVideo (video: VideoModel, t: Transaction) {
const actors = await VideoShareModel.loadActorsByShare(video.id, t)
const videoActor = video.VideoChannel && video.VideoChannel.Account
? video.VideoChannel.Account.Actor
: await ActorModel.loadAccountActorByVideoId(video.id, t)
actors.push(videoActor)
2018-05-25 11:32:36 +02:00
return actors
}
function getAudience (actorSender: ActorModel, isPublic = true) {
2018-05-25 11:32:36 +02:00
return buildAudience([ actorSender.followersUrl ], isPublic)
}
2018-05-28 12:13:00 +02:00
function buildAudience (followerUrls: string[], isPublic = true) {
let to: string[] = []
let cc: string[] = []
2018-05-25 11:32:36 +02:00
if (isPublic) {
to = [ ACTIVITY_PUB.PUBLIC ]
2018-05-28 12:13:00 +02:00
cc = followerUrls
2018-05-25 11:32:36 +02:00
} else { // Unlisted
to = []
cc = []
2018-05-25 11:32:36 +02:00
}
return { to, cc }
}
function audiencify<T> (object: T, audience: ActivityAudience) {
2018-05-25 11:32:36 +02:00
return Object.assign(object, audience)
}
// ---------------------------------------------------------------------------
export {
buildAudience,
getAudience,
2018-09-14 16:51:35 +02:00
getRemoteVideoAudience,
2018-05-25 11:32:36 +02:00
getActorsInvolvedInVideo,
2018-09-14 16:51:35 +02:00
getAudienceFromFollowersOf,
2018-05-25 11:32:36 +02:00
audiencify,
getVideoCommentAudience
}