]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/formatter/video-format-utils.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / models / video / formatter / video-format-utils.ts
index 5dc2c2f1b947c754dc6c4b5c0526d67e0a16b1df..6222107d7d1f343eafcc23167e1c4f87d7e9b124 100644 (file)
@@ -1,11 +1,19 @@
-import { uuidToShort } from '@server/helpers/uuid'
 import { generateMagnetUri } from '@server/helpers/webtorrent'
+import { getActivityStreamDuration } from '@server/lib/activitypub/activity'
 import { getLocalVideoFileMetadataUrl } from '@server/lib/video-urls'
-import { VideosCommonQueryAfterSanitize } from '@shared/models'
-import { VideoFile } from '@shared/models/videos/video-file.model'
-import { ActivityTagObject, ActivityUrlObject, VideoObject } from '../../../../shared/models/activitypub/objects'
-import { Video, VideoDetails, VideoInclude } from '../../../../shared/models/videos'
-import { VideoStreamingPlaylist } from '../../../../shared/models/videos/video-streaming-playlist.model'
+import { VideoViewsManager } from '@server/lib/views/video-views-manager'
+import { uuidToShort } from '@shared/extra-utils'
+import {
+  ActivityTagObject,
+  ActivityUrlObject,
+  Video,
+  VideoDetails,
+  VideoFile,
+  VideoInclude,
+  VideoObject,
+  VideosCommonQueryAfterSanitize,
+  VideoStreamingPlaylist
+} from '@shared/models'
 import { isArray } from '../../../helpers/custom-validators/misc'
 import {
   MIMETYPES,
@@ -42,6 +50,7 @@ export type VideoFormattingJSONOptions = {
     waitTranscoding?: boolean
     scheduledUpdate?: boolean
     blacklistInfo?: boolean
+    files?: boolean
     blockedOwner?: boolean
   }
 }
@@ -55,6 +64,7 @@ function guessAdditionalAttributesFromQuery (query: VideosCommonQueryAfterSaniti
       waitTranscoding: !!(query.include & VideoInclude.NOT_PUBLISHED_STATE),
       scheduledUpdate: !!(query.include & VideoInclude.NOT_PUBLISHED_STATE),
       blacklistInfo: !!(query.include & VideoInclude.BLACKLISTED),
+      files: !!(query.include & VideoInclude.FILES),
       blockedOwner: !!(query.include & VideoInclude.BLOCKED_OWNER)
     }
   }
@@ -95,7 +105,10 @@ function videoModelToFormattedJSON (video: MVideoFormattable, options: VideoForm
 
     isLocal: video.isOwned(),
     duration: video.duration,
+
     views: video.views,
+    viewers: VideoViewsManager.Instance.getViewers(video),
+
     likes: video.likes,
     dislikes: video.dislikes,
     thumbnailPath: video.getMiniatureStaticPath(),
@@ -150,22 +163,26 @@ function videoModelToFormattedJSON (video: MVideoFormattable, options: VideoForm
     videoObject.blockedServer = !!(server?.isBlocked())
   }
 
+  if (add?.files === true) {
+    videoObject.streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
+    videoObject.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
+  }
+
   return videoObject
 }
 
 function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): VideoDetails {
-  const formattedJson = video.toFormattedJSON({
+  const videoJSON = video.toFormattedJSON({
     additionalAttributes: {
       scheduledUpdate: true,
-      blacklistInfo: true
+      blacklistInfo: true,
+      files: true
     }
-  })
+  }) as Video & Required<Pick<Video, 'files' | 'streamingPlaylists'>>
 
   const tags = video.Tags ? video.Tags.map(t => t.name) : []
 
-  const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
-
-  const detailsJson = {
+  const detailsJSON = {
     support: video.support,
     descriptionPath: video.getDescriptionAPIPath(),
     channel: video.VideoChannel.toFormattedJSON(),
@@ -179,20 +196,14 @@ function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): Vid
       label: getStateLabel(video.state)
     },
 
-    trackerUrls: video.getTrackerUrls(),
-
-    files: [],
-    streamingPlaylists
+    trackerUrls: video.getTrackerUrls()
   }
 
-  // Format and sort video files
-  detailsJson.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
-
-  return Object.assign(formattedJson, detailsJson)
+  return Object.assign(videoJSON, detailsJSON)
 }
 
 function streamingPlaylistsModelToFormattedJSON (
-  video: MVideoFormattableDetails,
+  video: MVideoFormattable,
   playlists: MStreamingPlaylistRedundanciesOpt[]
 ): VideoStreamingPlaylist[] {
   if (isArray(playlists) === false) return []
@@ -223,7 +234,7 @@ function sortByResolutionDesc (fileA: MVideoFile, fileB: MVideoFile) {
 }
 
 function videoFilesModelToFormattedJSON (
-  video: MVideoFormattableDetails,
+  video: MVideoFormattable,
   videoFiles: MVideoFileRedundanciesOpt[],
   includeMagnet = true
 ): VideoFile[] {
@@ -407,15 +418,6 @@ function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
     views: video.views,
     sensitive: video.nsfw,
     waitTranscoding: video.waitTranscoding,
-    isLiveBroadcast: video.isLive,
-
-    liveSaveReplay: video.isLive
-      ? video.VideoLive.saveReplay
-      : null,
-
-    permanentLive: video.isLive
-      ? video.VideoLive.permanentLive
-      : null,
 
     state: video.state,
     commentsEnabled: video.commentsEnabled,
@@ -427,10 +429,13 @@ function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
       : null,
 
     updated: video.updatedAt.toISOString(),
+
     mediaType: 'text/markdown',
     content: video.description,
     support: video.support,
+
     subtitleLanguage,
+
     icon: icons.map(i => ({
       type: 'Image',
       url: i.getFileUrl(video),
@@ -438,11 +443,14 @@ function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
       width: i.width,
       height: i.height
     })),
+
     url,
+
     likes: getLocalVideoLikesActivityPubUrl(video),
     dislikes: getLocalVideoDislikesActivityPubUrl(video),
     shares: getLocalVideoSharesActivityPubUrl(video),
     comments: getLocalVideoCommentsActivityPubUrl(video),
+
     attributedTo: [
       {
         type: 'Person',
@@ -452,13 +460,10 @@ function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
         type: 'Group',
         id: video.VideoChannel.Actor.url
       }
-    ]
-  }
-}
+    ],
 
-function getActivityStreamDuration (duration: number) {
-  // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
-  return 'PT' + duration + 'S'
+    ...buildLiveAPAttributes(video)
+  }
 }
 
 function getCategoryLabel (id: number) {
@@ -486,7 +491,6 @@ export {
   videoModelToFormattedDetailsJSON,
   videoFilesModelToFormattedJSON,
   videoModelToActivityPubObject,
-  getActivityStreamDuration,
 
   guessAdditionalAttributesFromQuery,
 
@@ -496,3 +500,23 @@ export {
   getPrivacyLabel,
   getStateLabel
 }
+
+// ---------------------------------------------------------------------------
+
+function buildLiveAPAttributes (video: MVideoAP) {
+  if (!video.isLive) {
+    return {
+      isLiveBroadcast: false,
+      liveSaveReplay: null,
+      permanentLive: null,
+      latencyMode: null
+    }
+  }
+
+  return {
+    isLiveBroadcast: true,
+    liveSaveReplay: video.VideoLive.saveReplay,
+    permanentLive: video.VideoLive.permanentLive,
+    latencyMode: video.VideoLive.latencyMode
+  }
+}