1
0
Fork 0
peertube/server/lib/activitypub/send/send-create.ts

136 lines
5.1 KiB
TypeScript
Raw Normal View History

2017-11-20 03:43:39 -05:00
import { Transaction } from 'sequelize'
2017-12-12 11:53:50 -05:00
import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
2017-12-14 11:38:41 -05:00
import { VideoPrivacy } from '../../../../shared/models/videos'
import { ActorModel } from '../../../models/activitypub/actor'
2017-12-12 11:53:50 -05:00
import { VideoModel } from '../../../models/video/video'
import { VideoCommentModel } from '../../../models/video/video-comment'
2018-09-14 10:51:35 -04:00
import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf, getVideoCommentAudience } from '../audience'
2018-07-30 11:02:40 -04:00
import { logger } from '../../../helpers/logger'
2018-09-11 10:27:07 -04:00
import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
2019-02-26 04:55:40 -05:00
import { VideoPlaylistModel } from '../../../models/video/video-playlist'
import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
import { getServerActor } from '../../../helpers/utils'
2017-11-20 03:43:39 -05:00
2017-12-14 11:38:41 -05:00
async function sendCreateVideo (video: VideoModel, t: Transaction) {
2017-12-19 08:22:38 -05:00
if (video.privacy === VideoPrivacy.PRIVATE) return undefined
2017-11-20 03:43:39 -05:00
2018-07-30 11:02:40 -04:00
logger.info('Creating job to send video creation of %s.', video.url)
2017-12-19 04:34:56 -05:00
const byActor = video.VideoChannel.Account.Actor
2017-12-14 11:38:41 -05:00
const videoObject = video.toActivityPubObject()
2017-12-19 04:34:56 -05:00
const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
2018-09-11 10:27:07 -04:00
const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
2017-11-20 03:43:39 -05:00
2018-09-11 10:27:07 -04:00
return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
2017-11-20 03:43:39 -05:00
}
2019-01-29 02:37:25 -05:00
async function sendCreateCacheFile (byActor: ActorModel, video: VideoModel, fileRedundancy: VideoRedundancyModel) {
2018-09-11 10:27:07 -04:00
logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
2018-09-14 10:51:35 -04:00
return sendVideoRelatedCreateActivity({
byActor,
video,
url: fileRedundancy.url,
2019-01-29 02:37:25 -05:00
object: fileRedundancy.toActivityPubObject()
2018-09-14 10:51:35 -04:00
})
2017-11-22 10:25:03 -05:00
}
2019-02-26 04:55:40 -05:00
async function sendCreateVideoPlaylist (playlist: VideoPlaylistModel, t: Transaction) {
if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
logger.info('Creating job to send create video playlist of %s.', playlist.url)
const byActor = playlist.OwnerAccount.Actor
const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
2019-03-05 04:58:44 -05:00
const object = await playlist.toActivityPubObject(null, t)
2019-02-26 04:55:40 -05:00
const createActivity = buildCreateActivity(playlist.url, byActor, object, audience)
const serverActor = await getServerActor()
const toFollowersOf = [ byActor, serverActor ]
if (playlist.VideoChannel) toFollowersOf.push(playlist.VideoChannel.Actor)
return broadcastToFollowers(createActivity, byActor, toFollowersOf, t)
}
async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
2018-07-30 11:02:40 -04:00
logger.info('Creating job to send comment %s.', comment.url)
const isOrigin = comment.Video.isOwned()
const byActor = comment.Account.Actor
2018-01-05 05:19:25 -05:00
const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
const commentObject = comment.toActivityPubObject(threadParentComments)
2018-01-08 04:00:35 -05:00
const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
2018-09-14 10:51:35 -04:00
// Add the actor that commented too
2018-01-08 04:00:35 -05: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 10:51:35 -04:00
audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
}
2018-01-08 04:00:35 -05:00
2018-09-11 10:27:07 -04:00
const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
2018-01-08 04:00:35 -05:00
// This was a reply, send it to the parent actors
const actorsException = [ byActor ]
2018-09-11 10:27:07 -04:00
await broadcastToActors(createActivity, byActor, parentsCommentActors, actorsException)
2018-01-08 04:00:35 -05:00
// Broadcast to our followers
2018-09-11 10:27:07 -04:00
await broadcastToFollowers(createActivity, byActor, [ byActor ], t)
2018-01-08 04:00:35 -05:00
// Send to actors involved in the comment
2018-09-11 10:27:07 -04:00
if (isOrigin) return broadcastToFollowers(createActivity, byActor, actorsInvolvedInComment, t, actorsException)
// Send to origin
2018-09-11 10:27:07 -04:00
return unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
}
2018-09-11 10:27:07 -04: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 03:43:39 -05:00
}
// ---------------------------------------------------------------------------
export {
2017-12-14 11:38:41 -05:00
sendCreateVideo,
2018-09-11 10:27:07 -04:00
buildCreateActivity,
sendCreateVideoComment,
2019-02-26 04:55:40 -05:00
sendCreateVideoPlaylist,
2018-09-11 10:27:07 -04:00
sendCreateCacheFile
2017-11-20 03:43:39 -05:00
}
2018-09-14 10:51:35 -04: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)
}