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

185 lines
6.3 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'
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-09-14 09:51:35 -05:00
import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf, getVideoCommentAudience } from '../audience'
2018-07-30 10:02:40 -05:00
import { logger } from '../../../helpers/logger'
2018-09-11 09:27:07 -05:00
import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
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)
2018-09-11 09:27:07 -05:00
const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
2017-11-20 02:43:39 -06:00
2018-09-11 09:27:07 -05:00
return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
2017-11-20 02:43:39 -06:00
}
2018-09-11 09:27:07 -05:00
async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel) {
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)
2018-09-14 09:51:35 -05:00
// Custom audience, we only send the abuse to the origin instance
2017-12-14 10:38:41 -06:00
const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
2018-09-11 09:27:07 -05:00
const createActivity = buildCreateActivity(url, byActor, videoAbuse.toActivityPubObject(), audience)
2017-11-22 09:25:03 -06:00
2018-09-11 09:27:07 -05:00
return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
}
async function sendCreateCacheFile (byActor: ActorModel, fileRedundancy: VideoRedundancyModel) {
logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(fileRedundancy.VideoFile.Video.id)
2018-09-14 09:51:35 -05:00
const redundancyObject = fileRedundancy.toActivityPubObject()
2018-09-11 09:27:07 -05:00
2018-09-14 09:51:35 -05:00
return sendVideoRelatedCreateActivity({
byActor,
video,
url: fileRedundancy.url,
object: redundancyObject
})
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)
2018-09-14 09:51:35 -05:00
// Add the actor that commented too
2018-01-08 03:00:35 -06:00
actorsInvolvedInComment.push(byActor)
const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
let audience: ActivityAudience
if (isOrigin) {
audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
} else {
2018-09-14 09:51:35 -05:00
audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
}
2018-01-08 03:00:35 -06:00
2018-09-11 09:27:07 -05:00
const createActivity = buildCreateActivity(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 ]
2018-09-11 09:27:07 -05:00
await broadcastToActors(createActivity, byActor, parentsCommentActors, actorsException)
2018-01-08 03:00:35 -06:00
// Broadcast to our followers
2018-09-11 09:27:07 -05:00
await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
2018-01-08 03:00:35 -06:00
// Send to actors involved in the comment
2018-09-11 09:27:07 -05:00
if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
// Send to origin
2018-09-11 09:27:07 -05:00
return unicastTo(createActivity, 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)
2018-11-14 08:01:28 -06:00
const viewActivity = buildViewActivity(url, byActor, video)
2017-11-22 09:25:03 -06:00
2018-09-14 09:51:35 -05:00
return sendVideoRelatedCreateActivity({
// Use the server actor to send the view
byActor,
video,
url,
object: viewActivity,
transaction: t
})
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)
2018-11-14 08:01:28 -06:00
const dislikeActivity = buildDislikeActivity(url, byActor, video)
2017-11-23 07:19:55 -06:00
2018-09-14 09:51:35 -05:00
return sendVideoRelatedCreateActivity({
byActor,
video,
url,
object: dislikeActivity,
transaction: t
})
2017-11-22 09:25:03 -06:00
}
2018-09-11 09:27:07 -05:00
function buildCreateActivity (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
}
2018-11-14 08:01:28 -06:00
function buildDislikeActivity (url: string, byActor: ActorModel, video: VideoModel) {
2017-12-12 10:53:50 -06:00
return {
2018-11-14 08:01:28 -06:00
id: url,
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
}
}
2018-11-14 08:01:28 -06:00
function buildViewActivity (url: string, byActor: ActorModel, video: VideoModel) {
return {
2018-11-14 08:01:28 -06:00
id: url,
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,
2018-09-11 09:27:07 -05:00
buildCreateActivity,
sendCreateView,
sendCreateDislike,
2018-09-11 09:27:07 -05:00
buildDislikeActivity,
sendCreateVideoComment,
sendCreateCacheFile
2017-11-20 02:43:39 -06:00
}
2018-09-14 09:51:35 -05:00
// ---------------------------------------------------------------------------
async function sendVideoRelatedCreateActivity (options: {
byActor: ActorModel,
video: VideoModel,
url: string,
object: any,
transaction?: Transaction
}) {
const activityBuilder = (audience: ActivityAudience) => {
return buildCreateActivity(options.url, options.byActor, options.object, audience)
}
return sendVideoRelatedActivity(activityBuilder, options)
}