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

62 lines
1.8 KiB
TypeScript
Raw Normal View History

import { Transaction } from 'sequelize'
import { VideoViewsManager } from '@server/lib/views/video-views-manager'
import { MActorAudience, MActorLight, MVideoImmutable, MVideoUrl } from '@server/types/models'
import { ActivityAudience, ActivityView } from '@shared/models'
2020-11-20 04:21:08 -06:00
import { logger } from '../../../helpers/logger'
import { audiencify, getAudience } from '../audience'
2020-11-20 04:21:08 -06:00
import { getLocalVideoViewActivityPubUrl } from '../url'
import { sendVideoRelatedActivity } from './shared/send-utils'
type ViewType = 'view' | 'viewer'
async function sendView (options: {
byActor: MActorLight
type: ViewType
video: MVideoImmutable
transaction?: Transaction
}) {
const { byActor, type, video, transaction } = options
logger.info('Creating job to send %s of %s.', type, video.url)
const activityBuilder = (audience: ActivityAudience) => {
2020-11-20 04:21:08 -06:00
const url = getLocalVideoViewActivityPubUrl(byActor, video)
return buildViewActivity({ url, byActor, video, audience, type })
}
return sendVideoRelatedActivity(activityBuilder, { byActor, video, transaction, contextType: 'View' })
}
// ---------------------------------------------------------------------------
export {
sendView
}
// ---------------------------------------------------------------------------
function buildViewActivity (options: {
url: string
byActor: MActorAudience
video: MVideoUrl
type: ViewType
audience?: ActivityAudience
}): ActivityView {
const { url, byActor, type, video, audience = getAudience(byActor) } = options
return audiencify(
{
id: url,
type: 'View' as 'View',
actor: byActor.url,
object: video.url,
expires: type === 'viewer'
? new Date(VideoViewsManager.Instance.buildViewerExpireTime()).toISOString()
: undefined
},
audience
)
}