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

163 lines
5.8 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'
2018-05-25 11:32:36 +02:00
import { broadcastToActors, broadcastToFollowers, unicastTo } from './utils'
2017-11-23 14:19:55 +01:00
import {
audiencify,
getActorsInvolvedInVideo,
getAudience,
getObjectFollowersAudience,
2018-05-25 11:32:36 +02:00
getVideoAudience,
getVideoCommentAudience
} from '../audience'
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
const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
const data = createActivityData(video.url, byActor, videoObject, 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 = createActivityData(url, byActor, videoAbuse.toActivityPubObject(), audience)
2017-11-22 16:25:03 +01:00
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
2017-11-22 16:25:03 +01:00
}
async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
const isOrigin = comment.Video.isOwned()
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 parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
let audience: ActivityAudience
if (isOrigin) {
audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
} else {
audience = getObjectFollowersAudience(actorsInvolvedInComment.concat(parentsCommentActors))
}
2018-01-08 10:00:35 +01:00
const data = createActivityData(comment.url, byActor, commentObject, 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, parentsCommentActors, actorsException)
2018-01-08 10:00:35 +01:00
// Broadcast to our followers
await broadcastToFollowers(data, byActor, [ byActor ], t)
// Send to actors involved in the comment
if (isOrigin) return broadcastToFollowers(data, byActor, actorsInvolvedInComment, t, actorsException)
// Send to origin
return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
}
async function sendCreateView (byActor: ActorModel, video: VideoModel, t: Transaction) {
2017-12-14 17:38:41 +01:00
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)
2017-11-20 09:43:39 +01:00
// Send to origin
if (video.isOwned() === false) {
2018-05-25 11:32:36 +02:00
const audience = getVideoAudience(video, actorsInvolvedInVideo)
const data = createActivityData(url, byActor, viewActivityData, audience)
2017-11-20 09:43:39 +01:00
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
}
2017-11-22 16:25:03 +01:00
// Send to followers
const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
const data = createActivityData(url, byActor, viewActivityData, 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, actorsInvolvedInVideo, t, actorsException)
2017-11-23 14:19:55 +01:00
}
async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
2017-12-14 17:38:41 +01:00
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)
2017-11-23 14:19:55 +01:00
// Send to origin
if (video.isOwned() === false) {
2018-05-25 11:32:36 +02:00
const audience = getVideoAudience(video, actorsInvolvedInVideo)
const data = createActivityData(url, byActor, dislikeActivityData, audience)
2017-11-22 16:25:03 +01:00
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
}
2017-11-23 14:19:55 +01:00
// Send to followers
const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
const data = createActivityData(url, byActor, dislikeActivityData, audience)
2017-11-23 14:19:55 +01:00
2018-01-08 10:00:35 +01:00
const actorsException = [ byActor ]
return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, actorsException)
2017-11-22 16:25:03 +01:00
}
function createActivityData (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
if (!audience) audience = getAudience(byActor)
return audiencify(
{
type: 'Create' as 'Create',
id: url + '/activity',
actor: byActor.url,
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,
sendCreateView,
sendCreateDislike,
createDislikeActivityData,
sendCreateVideoComment
2017-11-20 09:43:39 +01:00
}