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

176 lines
6.2 KiB
TypeScript
Raw Normal View History

2017-11-20 02:43:39 -06:00
import { Transaction } from 'sequelize'
2017-12-12 10:53:50 -06:00
import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
2017-12-14 10:38:41 -06:00
import { VideoPrivacy } from '../../../../shared/models/videos'
2017-12-28 04:16:08 -06:00
import { getServerActor } from '../../../helpers/utils'
2017-12-14 10:38:41 -06:00
import { ActorModel } from '../../../models/activitypub/actor'
2017-12-12 10:53:50 -06:00
import { VideoModel } from '../../../models/video/video'
import { VideoAbuseModel } from '../../../models/video/video-abuse'
import { VideoCommentModel } from '../../../models/video/video-comment'
2017-11-23 07:19:55 -06:00
import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
2018-05-25 04:32:36 -05:00
import { broadcastToActors, broadcastToFollowers, unicastTo } from './utils'
2017-11-23 07:19:55 -06:00
import {
audiencify,
getActorsInvolvedInVideo,
getAudience,
getObjectFollowersAudience,
2018-05-25 04:32:36 -05:00
getVideoAudience,
getVideoCommentAudience
} from '../audience'
2018-07-30 10:02:40 -05:00
import { logger } from '../../../helpers/logger'
2017-11-20 02:43:39 -06:00
2017-12-14 10:38:41 -06:00
async function sendCreateVideo (video: VideoModel, t: Transaction) {
2017-12-19 07:22:38 -06:00
if (video.privacy === VideoPrivacy.PRIVATE) return undefined
2017-11-20 02:43:39 -06:00
2018-07-30 10:02:40 -05:00
logger.info('Creating job to send video creation of %s.', video.url)
2017-12-19 03:34:56 -06:00
const byActor = video.VideoChannel.Account.Actor
2017-12-14 10:38:41 -06:00
const videoObject = video.toActivityPubObject()
2017-12-19 03:34:56 -06:00
const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
const data = createActivityData(video.url, byActor, videoObject, audience)
2017-11-20 02:43:39 -06:00
2017-12-14 10:38:41 -06:00
return broadcastToFollowers(data, byActor, [ byActor ], t)
2017-11-20 02:43:39 -06:00
}
2017-12-14 10:38:41 -06:00
async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) {
if (!video.VideoChannel.Account.Actor.serverId) return // Local
2017-11-20 02:43:39 -06:00
const url = getVideoAbuseActivityPubUrl(videoAbuse)
2017-11-22 09:25:03 -06:00
2018-07-30 10:02:40 -05:00
logger.info('Creating job to send video abuse %s.', url)
2017-12-14 10:38:41 -06:00
const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
const data = createActivityData(url, byActor, videoAbuse.toActivityPubObject(), audience)
2017-11-22 09:25:03 -06:00
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
2017-11-22 09:25:03 -06:00
}
async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
2018-07-30 10:02:40 -05:00
logger.info('Creating job to send comment %s.', comment.url)
const isOrigin = comment.Video.isOwned()
const byActor = comment.Account.Actor
2018-01-05 04:19:25 -06:00
const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
const commentObject = comment.toActivityPubObject(threadParentComments)
2018-01-08 03:00:35 -06: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 03:00:35 -06:00
const data = createActivityData(comment.url, byActor, commentObject, audience)
2018-01-08 03:00:35 -06:00
// This was a reply, send it to the parent actors
const actorsException = [ byActor ]
await broadcastToActors(data, byActor, parentsCommentActors, actorsException)
2018-01-08 03:00:35 -06: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) {
2018-07-30 10:02:40 -05:00
logger.info('Creating job to send view of %s.', video.url)
2017-12-14 10:38:41 -06:00
const url = getVideoViewActivityPubUrl(byActor, video)
const viewActivityData = createViewActivityData(byActor, video)
2017-11-22 09:25:03 -06:00
2017-12-14 10:38:41 -06:00
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
2017-11-20 02:43:39 -06:00
// Send to origin
if (video.isOwned() === false) {
2018-05-25 04:32:36 -05:00
const audience = getVideoAudience(video, actorsInvolvedInVideo)
const data = createActivityData(url, byActor, viewActivityData, audience)
2017-11-20 02:43:39 -06:00
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
}
2017-11-22 09:25:03 -06:00
// Send to followers
const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
const data = createActivityData(url, byActor, viewActivityData, audience)
2017-11-22 09:25:03 -06:00
2017-12-14 10:38:41 -06:00
// Use the server actor to send the view
const serverActor = await getServerActor()
2018-01-08 03:00:35 -06:00
const actorsException = [ byActor ]
return broadcastToFollowers(data, serverActor, actorsInvolvedInVideo, t, actorsException)
2017-11-23 07:19:55 -06:00
}
async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
2018-07-30 10:02:40 -05:00
logger.info('Creating job to dislike %s.', video.url)
2017-12-14 10:38:41 -06:00
const url = getVideoDislikeActivityPubUrl(byActor, video)
const dislikeActivityData = createDislikeActivityData(byActor, video)
2017-11-23 07:19:55 -06:00
2017-12-14 10:38:41 -06:00
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
2017-11-23 07:19:55 -06:00
// Send to origin
if (video.isOwned() === false) {
2018-05-25 04:32:36 -05:00
const audience = getVideoAudience(video, actorsInvolvedInVideo)
const data = createActivityData(url, byActor, dislikeActivityData, audience)
2017-11-22 09:25:03 -06:00
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
}
2017-11-23 07:19:55 -06:00
// Send to followers
const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
const data = createActivityData(url, byActor, dislikeActivityData, audience)
2017-11-23 07:19:55 -06:00
2018-01-08 03:00:35 -06:00
const actorsException = [ byActor ]
return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, actorsException)
2017-11-22 09:25:03 -06: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 02:43:39 -06:00
}
2017-12-14 10:38:41 -06:00
function createDislikeActivityData (byActor: ActorModel, video: VideoModel) {
2017-12-12 10:53:50 -06:00
return {
2017-11-23 07:19:55 -06:00
type: 'Dislike',
2017-12-14 10:38:41 -06:00
actor: byActor.url,
2017-11-23 07:19:55 -06:00
object: video.url
}
}
function createViewActivityData (byActor: ActorModel, video: VideoModel) {
return {
type: 'View',
actor: byActor.url,
object: video.url
}
}
2017-11-20 02:43:39 -06:00
// ---------------------------------------------------------------------------
export {
2017-12-14 10:38:41 -06:00
sendCreateVideo,
2017-11-20 02:43:39 -06:00
sendVideoAbuse,
2017-11-22 09:25:03 -06:00
createActivityData,
sendCreateView,
sendCreateDislike,
createDislikeActivityData,
sendCreateVideoComment
2017-11-20 02:43:39 -06:00
}