2017-11-20 02:43:39 -06:00
|
|
|
import { Transaction } from 'sequelize'
|
2022-03-18 05:17:35 -05:00
|
|
|
import { getServerActor } from '@server/models/application/application'
|
2023-06-05 08:51:16 -05:00
|
|
|
import {
|
|
|
|
ActivityAudience,
|
|
|
|
ActivityCreate,
|
|
|
|
ActivityCreateObject,
|
|
|
|
ContextType,
|
|
|
|
VideoCommentObject,
|
|
|
|
VideoPlaylistPrivacy,
|
|
|
|
VideoPrivacy
|
|
|
|
} from '@shared/models'
|
2021-03-05 06:26:02 -06:00
|
|
|
import { logger, loggerTagsFactory } from '../../../helpers/logger'
|
2022-03-18 05:17:35 -05:00
|
|
|
import { VideoCommentModel } from '../../../models/video/video-comment'
|
2019-08-15 04:53:26 -05:00
|
|
|
import {
|
|
|
|
MActorLight,
|
|
|
|
MCommentOwnerVideo,
|
2022-03-24 07:36:47 -05:00
|
|
|
MLocalVideoViewerWithWatchSections,
|
2019-08-15 04:53:26 -05:00
|
|
|
MVideoAccountLight,
|
|
|
|
MVideoAP,
|
|
|
|
MVideoPlaylistFull,
|
|
|
|
MVideoRedundancyFileVideo,
|
|
|
|
MVideoRedundancyStreamingPlaylistVideo
|
2020-06-18 03:45:25 -05:00
|
|
|
} from '../../../types/models'
|
2022-03-18 05:17:35 -05:00
|
|
|
import { audiencify, getAudience } from '../audience'
|
|
|
|
import {
|
|
|
|
broadcastToActors,
|
|
|
|
broadcastToFollowers,
|
|
|
|
getActorsInvolvedInVideo,
|
|
|
|
getAudienceFromFollowersOf,
|
|
|
|
getVideoCommentAudience,
|
2022-03-24 07:36:47 -05:00
|
|
|
sendVideoActivityToOrigin,
|
2022-03-18 05:17:35 -05:00
|
|
|
sendVideoRelatedActivity,
|
|
|
|
unicastTo
|
|
|
|
} from './shared'
|
2019-08-15 04:53:26 -05:00
|
|
|
|
2021-03-05 06:26:02 -06:00
|
|
|
const lTags = loggerTagsFactory('ap', 'create')
|
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
async function sendCreateVideo (video: MVideoAP, transaction: Transaction) {
|
2019-12-12 08:47:47 -06:00
|
|
|
if (!video.hasPrivacyForFederation()) return undefined
|
2017-11-20 02:43:39 -06:00
|
|
|
|
2021-03-05 06:26:02 -06:00
|
|
|
logger.info('Creating job to send video creation of %s.', video.url, lTags(video.uuid))
|
2018-07-30 10:02:40 -05:00
|
|
|
|
2017-12-19 03:34:56 -06:00
|
|
|
const byActor = video.VideoChannel.Account.Actor
|
2023-03-10 05:01:21 -06:00
|
|
|
const videoObject = await video.toActivityPubObject()
|
2017-12-19 03:34:56 -06:00
|
|
|
|
2018-06-12 13:04:58 -05: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
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
return broadcastToFollowers({
|
|
|
|
data: createActivity,
|
|
|
|
byActor,
|
|
|
|
toFollowersOf: [ byActor ],
|
|
|
|
transaction,
|
|
|
|
contextType: 'Video'
|
|
|
|
})
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
async function sendCreateCacheFile (
|
|
|
|
byActor: MActorLight,
|
|
|
|
video: MVideoAccountLight,
|
|
|
|
fileRedundancy: MVideoRedundancyStreamingPlaylistVideo | MVideoRedundancyFileVideo
|
|
|
|
) {
|
2021-03-05 06:26:02 -06:00
|
|
|
logger.info('Creating job to send file cache of %s.', fileRedundancy.url, lTags(video.uuid))
|
2018-09-11 09:27:07 -05:00
|
|
|
|
2018-09-14 09:51:35 -05:00
|
|
|
return sendVideoRelatedCreateActivity({
|
|
|
|
byActor,
|
|
|
|
video,
|
|
|
|
url: fileRedundancy.url,
|
2020-02-04 09:34:46 -06:00
|
|
|
object: fileRedundancy.toActivityPubObject(),
|
|
|
|
contextType: 'CacheFile'
|
2018-09-14 09:51:35 -05:00
|
|
|
})
|
2017-11-22 09:25:03 -06:00
|
|
|
}
|
|
|
|
|
2022-03-24 07:36:47 -05:00
|
|
|
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' })
|
|
|
|
}
|
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
async function sendCreateVideoPlaylist (playlist: MVideoPlaylistFull, transaction: Transaction) {
|
2019-02-26 03:55:40 -06:00
|
|
|
if (playlist.privacy === VideoPlaylistPrivacy.PRIVATE) return undefined
|
|
|
|
|
2021-03-05 06:26:02 -06:00
|
|
|
logger.info('Creating job to send create video playlist of %s.', playlist.url, lTags(playlist.uuid))
|
2019-02-26 03:55:40 -06:00
|
|
|
|
|
|
|
const byActor = playlist.OwnerAccount.Actor
|
|
|
|
const audience = getAudience(byActor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
|
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
const object = await playlist.toActivityPubObject(null, transaction)
|
2019-02-26 03:55:40 -06: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 10:14:33 -05:00
|
|
|
return broadcastToFollowers({
|
|
|
|
data: createActivity,
|
|
|
|
byActor,
|
|
|
|
toFollowersOf,
|
|
|
|
transaction,
|
|
|
|
contextType: 'Playlist'
|
|
|
|
})
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
async function sendCreateVideoComment (comment: MCommentOwnerVideo, transaction: Transaction) {
|
2018-07-30 10:02:40 -05:00
|
|
|
logger.info('Creating job to send comment %s.', comment.url)
|
|
|
|
|
2018-03-27 06:33:56 -05:00
|
|
|
const isOrigin = comment.Video.isOwned()
|
|
|
|
|
2017-12-27 03:39:31 -06:00
|
|
|
const byActor = comment.Account.Actor
|
2022-03-23 10:14:33 -05:00
|
|
|
const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, transaction)
|
2023-06-05 08:51:16 -05:00
|
|
|
const commentObject = comment.toActivityPubObject(threadParentComments) as VideoCommentObject
|
2017-12-27 03:39:31 -06:00
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, transaction)
|
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)
|
|
|
|
|
2020-03-20 08:43:12 -05:00
|
|
|
const parentsCommentActors = threadParentComments.filter(c => !c.isDeleted())
|
|
|
|
.map(c => c.Account.Actor)
|
2017-12-27 03:39:31 -06:00
|
|
|
|
2018-03-27 06:33:56 -05:00
|
|
|
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-03-27 06:33:56 -05:00
|
|
|
}
|
2018-01-08 03:00:35 -06:00
|
|
|
|
2018-09-11 09:27:07 -05:00
|
|
|
const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience)
|
2017-12-27 03:39:31 -06:00
|
|
|
|
2018-01-08 03:00:35 -06:00
|
|
|
// This was a reply, send it to the parent actors
|
|
|
|
const actorsException = [ byActor ]
|
2022-03-23 10:14:33 -05:00
|
|
|
await broadcastToActors({
|
|
|
|
data: createActivity,
|
|
|
|
byActor,
|
|
|
|
toActors: parentsCommentActors,
|
|
|
|
transaction,
|
|
|
|
actorsException,
|
|
|
|
contextType: 'Comment'
|
|
|
|
})
|
2018-01-08 03:00:35 -06:00
|
|
|
|
|
|
|
// Broadcast to our followers
|
2022-03-23 10:14:33 -05:00
|
|
|
await broadcastToFollowers({
|
|
|
|
data: createActivity,
|
|
|
|
byActor,
|
|
|
|
toFollowersOf: [ byActor ],
|
|
|
|
transaction,
|
|
|
|
contextType: 'Comment'
|
|
|
|
})
|
2018-01-08 03:00:35 -06:00
|
|
|
|
|
|
|
// Send to actors involved in the comment
|
2022-03-23 10:14:33 -05:00
|
|
|
if (isOrigin) {
|
|
|
|
return broadcastToFollowers({
|
|
|
|
data: createActivity,
|
|
|
|
byActor,
|
|
|
|
toFollowersOf: actorsInvolvedInComment,
|
|
|
|
transaction,
|
|
|
|
actorsException,
|
|
|
|
contextType: 'Comment'
|
|
|
|
})
|
|
|
|
}
|
2018-03-27 06:33:56 -05:00
|
|
|
|
|
|
|
// Send to origin
|
2022-03-23 10:14:33 -05:00
|
|
|
return transaction.afterCommit(() => {
|
|
|
|
return unicastTo({
|
|
|
|
data: createActivity,
|
|
|
|
byActor,
|
|
|
|
toActorUrl: comment.Video.VideoChannel.Account.Actor.getSharedInbox(),
|
|
|
|
contextType: 'Comment'
|
|
|
|
})
|
|
|
|
})
|
2017-12-27 03:39:31 -06:00
|
|
|
}
|
|
|
|
|
2023-06-05 08:51:16 -05:00
|
|
|
function buildCreateActivity <T extends ActivityCreateObject> (
|
|
|
|
url: string,
|
|
|
|
byActor: MActorLight,
|
|
|
|
object: T,
|
|
|
|
audience?: ActivityAudience
|
|
|
|
): ActivityCreate<T> {
|
2018-06-12 13:04:58 -05:00
|
|
|
if (!audience) audience = getAudience(byActor)
|
|
|
|
|
|
|
|
return audiencify(
|
|
|
|
{
|
|
|
|
type: 'Create' as 'Create',
|
|
|
|
id: url + '/activity',
|
|
|
|
actor: byActor.url,
|
2023-06-05 08:51:16 -05:00
|
|
|
object: typeof object === 'string'
|
|
|
|
? object
|
|
|
|
: audiencify(object, audience)
|
2018-06-12 13:04:58 -05:00
|
|
|
},
|
|
|
|
audience
|
|
|
|
)
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2017-12-14 10:38:41 -06:00
|
|
|
sendCreateVideo,
|
2018-09-11 09:27:07 -05:00
|
|
|
buildCreateActivity,
|
|
|
|
sendCreateVideoComment,
|
2019-02-26 03:55:40 -06:00
|
|
|
sendCreateVideoPlaylist,
|
2022-03-24 07:36:47 -05:00
|
|
|
sendCreateCacheFile,
|
|
|
|
sendCreateWatchAction
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
2018-09-14 09:51:35 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function sendVideoRelatedCreateActivity (options: {
|
2020-01-31 09:56:52 -06:00
|
|
|
byActor: MActorLight
|
|
|
|
video: MVideoAccountLight
|
|
|
|
url: string
|
|
|
|
object: any
|
2022-03-23 10:14:33 -05:00
|
|
|
contextType: ContextType
|
2018-09-14 09:51:35 -05:00
|
|
|
transaction?: Transaction
|
|
|
|
}) {
|
|
|
|
const activityBuilder = (audience: ActivityAudience) => {
|
|
|
|
return buildCreateActivity(options.url, options.byActor, options.object, audience)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sendVideoRelatedActivity(activityBuilder, options)
|
|
|
|
}
|