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

232 lines
7.1 KiB
TypeScript
Raw Normal View History

import {
ActivityAudience,
ActivityCreate,
ActivityCreateObject,
ContextType,
VideoCommentObject,
VideoPlaylistPrivacy,
VideoPrivacy
} from '@peertube/peertube-models'
import { AccountModel } from '@server/models/account/account.js'
import { getServerActor } from '@server/models/application/application.js'
import { VideoModel } from '@server/models/video/video.js'
import { Transaction } from 'sequelize'
import { logger, loggerTagsFactory } from '../../../helpers/logger.js'
import { VideoCommentModel } from '../../../models/video/video-comment.js'
2019-08-15 11:53:26 +02:00
import {
MActorLight,
MCommentOwnerVideo,
MLocalVideoViewerWithWatchSections,
2019-08-15 11:53:26 +02:00
MVideoAP,
MVideoAccountLight,
2019-08-15 11:53:26 +02:00
MVideoPlaylistFull,
MVideoRedundancyFileVideo,
MVideoRedundancyStreamingPlaylistVideo
} from '../../../types/models/index.js'
import { audiencify, getAudience } from '../audience.js'
import { canVideoBeFederated } from '../videos/federate.js'
import {
broadcastToActors,
broadcastToFollowers,
getActorsInvolvedInVideo,
getAudienceFromFollowersOf,
getVideoCommentAudience,
sendVideoActivityToOrigin,
sendVideoRelatedActivity,
unicastTo
} from './shared/index.js'
2019-08-15 11:53:26 +02:00
const lTags = loggerTagsFactory('ap', 'create')
export async function sendCreateVideo (video: MVideoAP, transaction: Transaction) {
if (!canVideoBeFederated(video)) return undefined
2017-11-20 09:43:39 +01:00
logger.info('Creating job to send video creation of %s.', video.url, lTags(video.uuid))
2018-07-30 17:02:40 +02:00
2017-12-19 10:34:56 +01:00
const byActor = video.VideoChannel.Account.Actor
const videoObject = await video.toActivityPubObject()
2017-12-19 10:34:56 +01:00
const audience = getAudience(byActor, video.privacy === VideoPrivacy.PUBLIC)
2018-09-11 16:27:07 +02:00
const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience)
2017-11-20 09:43:39 +01:00
2022-03-23 16:14:33 +01:00
return broadcastToFollowers({
data: createActivity,
byActor,
toFollowersOf: [ byActor ],
transaction,
contextType: 'Video'
})
2017-11-20 09:43:39 +01:00
}
export async function sendCreateCacheFile (
2019-08-15 11:53:26 +02:00
byActor: MActorLight,
video: MVideoAccountLight,
fileRedundancy: MVideoRedundancyStreamingPlaylistVideo | MVideoRedundancyFileVideo
) {
logger.info('Creating job to send file cache of %s.', fileRedundancy.url, lTags(video.uuid))
2018-09-11 16:27:07 +02:00
2018-09-14 16:51:35 +02:00
return sendVideoRelatedCreateActivity({
byActor,
video,
url: fileRedundancy.url,
2020-02-04 16:34:46 +01:00
object: fileRedundancy.toActivityPubObject(),
contextType: 'CacheFile'
2018-09-14 16:51:35 +02:00
})
2017-11-22 16:25:03 +01:00
}
export async function sendCreateWatchAction (stats: MLocalVideoViewerWithWatchSections, transaction: Transaction) {
logger.info('Creating job to send create watch action %s.', stats.url, lTags(stats.uuid))
const byActor = await getServerActor()
const activityBuilder = (audience: ActivityAudience) => {
return buildCreateActivity(stats.url, byActor, stats.toActivityPubObject(), audience)
}
return sendVideoActivityToOrigin(activityBuilder, { byActor, video: stats.Video, transaction, contextType: 'WatchAction' })
}
export async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, transaction: Transaction) {
2019-02-26 10:55:40 +01:00
if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
logger.info('Creating job to send create video playlist of %s.', playlist.url, lTags(playlist.uuid))
2019-02-26 10:55:40 +01:00
const byActor = playlist.OwnerAccount.Actor
const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
2022-03-23 16:14:33 +01:00
const object = await playlist.toActivityPubObject(null, transaction)
2019-02-26 10:55:40 +01: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)
2022-03-23 16:14:33 +01:00
return broadcastToFollowers({
data: createActivity,
byActor,
toFollowersOf,
transaction,
contextType: 'Playlist'
})
2019-02-26 10:55:40 +01:00
}
export async function sendCreateVideoComment (comment: MCommentOwnerVideo, transaction: Transaction) {
const isOrigin = comment.Video.isOwned()
if (isOrigin) {
const videoWithBlacklist = await VideoModel.loadWithBlacklist(comment.Video.id)
if (!canVideoBeFederated(videoWithBlacklist)) {
logger.debug(`Do not send comment ${comment.url} on a video that cannot be federated`)
return undefined
}
}
logger.info('Creating job to send comment %s.', comment.url)
const byActor = comment.Account.Actor
2023-10-31 10:02:19 +01:00
const videoAccount = await AccountModel.load(comment.Video.VideoChannel.Account.id, transaction)
2022-03-23 16:14:33 +01:00
const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, transaction)
const commentObject = comment.toActivityPubObject(threadParentComments) as VideoCommentObject
2022-03-23 16:14:33 +01:00
const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, transaction)
2018-09-14 16:51:35 +02:00
// Add the actor that commented too
2018-01-08 10:00:35 +01:00
actorsInvolvedInComment.push(byActor)
const parentsCommentActors = threadParentComments.filter(c => !c.isDeleted())
.map(c => c.Account.Actor)
let audience: ActivityAudience
if (isOrigin) {
audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
} else {
2018-09-14 16:51:35 +02:00
audience = getAudienceFromFollowersOf(actorsInvolvedInComment.concat(parentsCommentActors))
}
2018-01-08 10:00:35 +01:00
2018-09-11 16:27:07 +02:00
const createActivity = buildCreateActivity(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 ]
2022-03-23 16:14:33 +01:00
await broadcastToActors({
data: createActivity,
byActor,
toActors: parentsCommentActors,
transaction,
actorsException,
contextType: 'Comment'
})
2018-01-08 10:00:35 +01:00
// Broadcast to our followers
2022-03-23 16:14:33 +01:00
await broadcastToFollowers({
data: createActivity,
byActor,
toFollowersOf: [ byActor ],
transaction,
contextType: 'Comment'
})
2018-01-08 10:00:35 +01:00
// Send to actors involved in the comment
2022-03-23 16:14:33 +01:00
if (isOrigin) {
return broadcastToFollowers({
data: createActivity,
byActor,
toFollowersOf: actorsInvolvedInComment,
transaction,
actorsException,
contextType: 'Comment'
})
}
// Send to origin
2022-03-23 16:14:33 +01:00
return transaction.afterCommit(() => {
return unicastTo({
data: createActivity,
byActor,
2023-10-31 10:02:19 +01:00
toActorUrl: videoAccount.Actor.getSharedInbox(),
2022-03-23 16:14:33 +01:00
contextType: 'Comment'
})
})
}
export function buildCreateActivity <T extends ActivityCreateObject> (
url: string,
byActor: MActorLight,
object: T,
audience?: ActivityAudience
): ActivityCreate<T> {
if (!audience) audience = getAudience(byActor)
return audiencify(
{
type: 'Create' as 'Create',
id: url + '/activity',
actor: byActor.url,
object: typeof object === 'string'
? object
: audiencify(object, audience)
},
audience
)
2017-11-20 09:43:39 +01:00
}
// ---------------------------------------------------------------------------
// Private
2018-09-14 16:51:35 +02:00
// ---------------------------------------------------------------------------
async function sendVideoRelatedCreateActivity (options: {
2020-01-31 16:56:52 +01:00
byActor: MActorLight
video: MVideoAccountLight
url: string
object: any
2022-03-23 16:14:33 +01:00
contextType: ContextType
2018-09-14 16:51:35 +02:00
transaction?: Transaction
}) {
const activityBuilder = (audience: ActivityAudience) => {
return buildCreateActivity(options.url, options.byActor, options.object, audience)
}
return sendVideoRelatedActivity(activityBuilder, options)
}