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

178 lines
7.1 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 { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
2017-12-14 17:38:41 +01:00
import { VideoPrivacy } from '../../../../shared/models/videos'
2017-12-28 11:16:08 +01:00
import { getServerActor } from '../../../helpers/utils'
2017-12-14 17:38:41 +01:00
import { ActorModel } from '../../../models/activitypub/actor'
2017-12-12 17:53:50 +01:00
import { VideoModel } from '../../../models/video/video'
import { VideoAbuseModel } from '../../../models/video/video-abuse'
import { VideoCommentModel } from '../../../models/video/video-comment'
2017-11-23 14:19:55 +01:00
import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
import {
2018-01-08 10:00:35 +01:00
audiencify, broadcastToActors, broadcastToFollowers, getActorsInvolvedInVideo, getAudience, getObjectFollowersAudience,
2018-01-05 11:19:25 +01:00
getOriginVideoAudience, getOriginVideoCommentAudience,
2017-11-23 14:19:55 +01:00
unicastTo
} from './misc'
2017-11-20 09:43:39 +01:00
2017-12-14 17:38:41 +01:00
async function sendCreateVideo (video: VideoModel, t: Transaction) {
2017-12-19 14:22:38 +01:00
if (video.privacy === VideoPrivacy.PRIVATE) return undefined
2017-11-20 09:43:39 +01:00
2017-12-19 10:34:56 +01:00
const byActor = video.VideoChannel.Account.Actor
2017-12-14 17:38:41 +01:00
const videoObject = video.toActivityPubObject()
2017-12-19 10:34:56 +01:00
2017-12-14 17:38:41 +01:00
const audience = await getAudience(byActor, t, video.privacy === VideoPrivacy.PUBLIC)
const data = await createActivityData(video.url, byActor, videoObject, t, audience)
2017-11-20 09:43:39 +01:00
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 sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) {
2017-11-20 09:43:39 +01:00
const url = getVideoAbuseActivityPubUrl(videoAbuse)
2017-11-22 16:25:03 +01:00
2017-12-14 17:38:41 +01:00
const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
const data = await createActivityData(url, byActor, videoAbuse.toActivityPubObject(), t, audience)
2017-11-22 16:25:03 +01:00
2017-12-14 17:38:41 +01:00
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
2017-11-22 16:25:03 +01:00
}
async function sendCreateVideoCommentToOrigin (comment: VideoCommentModel, t: Transaction) {
const byActor = comment.Account.Actor
2018-01-05 11:19:25 +01:00
const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
const commentObject = comment.toActivityPubObject(threadParentComments)
2018-01-08 10:00:35 +01:00
const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
actorsInvolvedInComment.push(byActor)
const audience = getOriginVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment)
const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
2018-01-08 10:00:35 +01:00
// This was a reply, send it to the parent actors
const actorsException = [ byActor ]
await broadcastToActors(data, byActor, threadParentComments.map(c => c.Account.Actor), t, actorsException)
// Broadcast to our followers
await broadcastToFollowers(data, byActor, [ byActor ], t)
// Send to origin
return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl, t)
}
async function sendCreateVideoCommentToVideoFollowers (comment: VideoCommentModel, t: Transaction) {
const byActor = comment.Account.Actor
2018-01-05 11:19:25 +01:00
const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
const commentObject = comment.toActivityPubObject(threadParentComments)
2018-01-08 10:00:35 +01:00
const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
actorsInvolvedInComment.push(byActor)
const audience = getOriginVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment)
const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
2018-01-08 10:00:35 +01:00
// This was a reply, send it to the parent actors
const actorsException = [ byActor ]
await broadcastToActors(data, byActor, threadParentComments.map(c => c.Account.Actor), t, actorsException)
// Broadcast to our followers
await broadcastToFollowers(data, byActor, [ byActor ], t)
// Send to actors involved in the comment
return broadcastToFollowers(data, byActor, actorsInvolvedInComment, t, actorsException)
}
2017-12-14 17:38:41 +01:00
async function sendCreateViewToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
const url = getVideoViewActivityPubUrl(byActor, video)
const viewActivityData = createViewActivityData(byActor, video)
2017-11-22 16:25:03 +01:00
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, viewActivityData, t, audience)
2017-11-20 09:43:39 +01:00
2017-12-14 17:38:41 +01:00
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
2017-11-20 09:43:39 +01:00
}
2017-12-14 17:38:41 +01:00
async function sendCreateViewToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
const url = getVideoViewActivityPubUrl(byActor, video)
const viewActivityData = createViewActivityData(byActor, video)
2017-11-22 16:25:03 +01:00
2017-12-14 17:38:41 +01:00
const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
const audience = getObjectFollowersAudience(actorsToForwardView)
const data = await createActivityData(url, byActor, viewActivityData, t, audience)
2017-11-22 16:25:03 +01:00
2017-12-14 17:38:41 +01:00
// Use the server actor to send the view
const serverActor = await getServerActor()
2018-01-08 10:00:35 +01:00
const actorsException = [ byActor ]
return broadcastToFollowers(data, serverActor, actorsToForwardView, t, actorsException)
2017-11-23 14:19:55 +01:00
}
2017-12-14 17:38:41 +01:00
async function sendCreateDislikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
const url = getVideoDislikeActivityPubUrl(byActor, video)
const dislikeActivityData = createDislikeActivityData(byActor, video)
2017-11-23 14:19:55 +01:00
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, dislikeActivityData, t, audience)
2017-11-23 14:19:55 +01:00
2017-12-14 17:38:41 +01:00
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
2017-11-23 14:19:55 +01:00
}
2017-11-22 16:25:03 +01:00
2017-12-14 17:38:41 +01:00
async function sendCreateDislikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
const url = getVideoDislikeActivityPubUrl(byActor, video)
const dislikeActivityData = createDislikeActivityData(byActor, video)
2017-11-23 14:19:55 +01:00
2017-12-14 17:38:41 +01:00
const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
const audience = getObjectFollowersAudience(actorsToForwardView)
const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
2017-11-23 14:19:55 +01:00
2018-01-08 10:00:35 +01:00
const actorsException = [ byActor ]
return broadcastToFollowers(data, byActor, actorsToForwardView, t, actorsException)
2017-11-22 16:25:03 +01:00
}
2017-12-12 17:53:50 +01:00
async function createActivityData (
url: string,
2017-12-14 17:38:41 +01:00
byActor: ActorModel,
2017-12-12 17:53:50 +01:00
object: any,
t: Transaction,
audience?: ActivityAudience
): Promise<ActivityCreate> {
2017-11-22 16:25:03 +01:00
if (!audience) {
2017-12-14 17:38:41 +01:00
audience = await getAudience(byActor, t)
2017-11-22 16:25:03 +01:00
}
2017-11-22 10:29:55 +01:00
2017-12-19 10:34:56 +01:00
return audiencify({
2017-11-20 09:43:39 +01:00
type: 'Create',
id: url,
2017-12-14 17:38:41 +01:00
actor: byActor.url,
2017-12-19 10:34:56 +01:00
object: audiencify(object, audience)
}, audience)
2017-11-20 09:43:39 +01:00
}
2017-12-14 17:38:41 +01:00
function createDislikeActivityData (byActor: ActorModel, video: VideoModel) {
2017-12-12 17:53:50 +01:00
return {
2017-11-23 14:19:55 +01:00
type: 'Dislike',
2017-12-14 17:38:41 +01:00
actor: byActor.url,
2017-11-23 14:19:55 +01:00
object: video.url
}
}
function createViewActivityData (byActor: ActorModel, video: VideoModel) {
return {
type: 'View',
actor: byActor.url,
object: video.url
}
}
2017-11-20 09:43:39 +01:00
// ---------------------------------------------------------------------------
export {
2017-12-14 17:38:41 +01:00
sendCreateVideo,
2017-11-20 09:43:39 +01:00
sendVideoAbuse,
2017-11-22 16:25:03 +01:00
createActivityData,
sendCreateViewToOrigin,
2017-11-23 14:19:55 +01:00
sendCreateViewToVideoFollowers,
sendCreateDislikeToOrigin,
sendCreateDislikeToVideoFollowers,
createDislikeActivityData,
sendCreateVideoCommentToOrigin,
sendCreateVideoCommentToVideoFollowers
2017-11-20 09:43:39 +01:00
}