]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/formatter/video-format-utils.ts
feat: show contained playlists under My videos (#5125)
[github/Chocobozzz/PeerTube.git] / server / models / video / formatter / video-format-utils.ts
index 611edf0b938ebb265e6da299e5c76152d82acbb4..e1b0eb6108e00d8a131044d92a502ba0fccf65a2 100644 (file)
@@ -1,11 +1,20 @@
 import { generateMagnetUri } from '@server/helpers/webtorrent'
+import { getActivityStreamDuration } from '@server/lib/activitypub/activity'
+import { tracer } from '@server/lib/opentelemetry/tracing'
 import { getLocalVideoFileMetadataUrl } from '@server/lib/video-urls'
-import { VideoViews } from '@server/lib/video-views'
+import { VideoViewsManager } from '@server/lib/views/video-views-manager'
 import { uuidToShort } from '@shared/extra-utils'
-import { VideoFile, VideosCommonQueryAfterSanitize } from '@shared/models'
-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 {
+  ActivityTagObject,
+  ActivityUrlObject,
+  Video,
+  VideoDetails,
+  VideoFile,
+  VideoInclude,
+  VideoObject,
+  VideosCommonQueryAfterSanitize,
+  VideoStreamingPlaylist
+} from '@shared/models'
 import { isArray } from '../../../helpers/custom-validators/misc'
 import {
   MIMETYPES,
@@ -63,6 +72,8 @@ function guessAdditionalAttributesFromQuery (query: VideosCommonQueryAfterSaniti
 }
 
 function videoModelToFormattedJSON (video: MVideoFormattable, options: VideoFormattingJSONOptions = {}): Video {
+  const span = tracer.startSpan('peertube.VideoModel.toFormattedJSON')
+
   const userHistory = isArray(video.UserVideoHistories) ? video.UserVideoHistories[0] : undefined
 
   const videoObject: Video = {
@@ -97,7 +108,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(),
@@ -121,10 +135,6 @@ function videoModelToFormattedJSON (video: MVideoFormattable, options: VideoForm
     pluginData: (video as any).pluginData
   }
 
-  if (video.isLive) {
-    videoObject.viewers = VideoViews.Instance.getViewers(video)
-  }
-
   const add = options.additionalAttributes
   if (add?.state === true) {
     videoObject.state = {
@@ -161,10 +171,14 @@ function videoModelToFormattedJSON (video: MVideoFormattable, options: VideoForm
     videoObject.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
   }
 
+  span.end()
+
   return videoObject
 }
 
 function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): VideoDetails {
+  const span = tracer.startSpan('peertube.VideoModel.toFormattedDetailsJSON')
+
   const videoJSON = video.toFormattedJSON({
     additionalAttributes: {
       scheduledUpdate: true,
@@ -192,6 +206,8 @@ function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): Vid
     trackerUrls: video.getTrackerUrls()
   }
 
+  span.end()
+
   return Object.assign(videoJSON, detailsJSON)
 }
 
@@ -240,6 +256,8 @@ function videoFilesModelToFormattedJSON (
     .sort(sortByResolutionDesc)
     .map(videoFile => {
       return {
+        id: videoFile.id,
+
         resolution: {
           id: videoFile.resolution,
           label: videoFile.resolution === 0 ? 'Audio' : `${videoFile.resolution}p`
@@ -459,11 +477,6 @@ function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
   }
 }
 
-function getActivityStreamDuration (duration: number) {
-  // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
-  return 'PT' + duration + 'S'
-}
-
 function getCategoryLabel (id: number) {
   return VIDEO_CATEGORIES[id] || 'Misc'
 }
@@ -489,7 +502,6 @@ export {
   videoModelToFormattedDetailsJSON,
   videoFilesModelToFormattedJSON,
   videoModelToActivityPubObject,
-  getActivityStreamDuration,
 
   guessAdditionalAttributesFromQuery,