]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/send/send-view.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-view.ts
index 3358188a27e27695ec09285200295702f9c0cbbc..25a20ec6decad3a31d7cf7514bd3cc6c619ccf95 100644 (file)
@@ -1,40 +1,62 @@
 import { Transaction } from 'sequelize'
-import { ActivityAudience, ActivityView } from '../../../../shared/models/activitypub'
-import { ActorModel } from '../../../models/activitypub/actor'
-import { getVideoLikeActivityPubUrl } from '../url'
-import { sendVideoRelatedActivity } from './utils'
-import { audiencify, getAudience } from '../audience'
+import { VideoViewsManager } from '@server/lib/views/video-views-manager'
+import { MActorAudience, MActorLight, MVideoImmutable, MVideoUrl } from '@server/types/models'
+import { ActivityAudience, ActivityView } from '@shared/models'
 import { logger } from '../../../helpers/logger'
-import { MActorAudience, MVideoImmutable, MVideoUrl } from '@server/types/models'
+import { audiencify, getAudience } from '../audience'
+import { getLocalVideoViewActivityPubUrl } from '../url'
+import { sendVideoRelatedActivity } from './shared/send-utils'
+
+type ViewType = 'view' | 'viewer'
 
-async function sendView (byActor: ActorModel, video: MVideoImmutable, t: Transaction) {
-  logger.info('Creating job to send view of %s.', video.url)
+async function sendView (options: {
+  byActor: MActorLight
+  type: ViewType
+  video: MVideoImmutable
+  viewerIdentifier: string
+  transaction?: Transaction
+}) {
+  const { byActor, type, video, viewerIdentifier, transaction } = options
+
+  logger.info('Creating job to send %s of %s.', type, video.url)
 
   const activityBuilder = (audience: ActivityAudience) => {
-    const url = getVideoLikeActivityPubUrl(byActor, video)
+    const url = getLocalVideoViewActivityPubUrl(byActor, video, viewerIdentifier)
 
-    return buildViewActivity(url, byActor, video, audience)
+    return buildViewActivity({ url, byActor, video, audience, type })
   }
 
-  return sendVideoRelatedActivity(activityBuilder, { byActor, video, transaction: t, contextType: 'View' })
+  return sendVideoRelatedActivity(activityBuilder, { byActor, video, transaction, contextType: 'View' })
 }
 
-function buildViewActivity (url: string, byActor: MActorAudience, video: MVideoUrl, audience?: ActivityAudience): ActivityView {
-  if (!audience) audience = getAudience(byActor)
+// ---------------------------------------------------------------------------
+
+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
+      object: video.url,
+
+      expires: type === 'viewer'
+        ? new Date(VideoViewsManager.Instance.buildViewerExpireTime()).toISOString()
+        : undefined
     },
     audience
   )
 }
-
-// ---------------------------------------------------------------------------
-
-export {
-  sendView
-}