]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/formatter/video-format-utils.ts
Move test functions outside extra-utils
[github/Chocobozzz/PeerTube.git] / server / models / video / formatter / video-format-utils.ts
index 6b1e5906333f5142ffe26948005bb9b69d154ab3..99a4818ab09a74c6cbca657a012c6d8a73f94d8c 100644 (file)
@@ -1,9 +1,10 @@
-import { uuidToShort } from '@server/helpers/uuid'
 import { generateMagnetUri } from '@server/helpers/webtorrent'
-import { getLocalVideoFileMetadataUrl } from '@server/lib/video-paths'
-import { VideoFile } from '@shared/models/videos/video-file.model'
+import { getLocalVideoFileMetadataUrl } from '@server/lib/video-urls'
+import { VideoViews } from '@server/lib/video-views'
+import { uuidToShort } from '@shared/core-utils'
+import { VideoFile, VideosCommonQueryAfterSanitize } from '@shared/models'
 import { ActivityTagObject, ActivityUrlObject, VideoObject } from '../../../../shared/models/activitypub/objects'
-import { Video, VideoDetails } from '../../../../shared/models/videos'
+import { Video, VideoDetails, VideoInclude } from '../../../../shared/models/videos'
 import { VideoStreamingPlaylist } from '../../../../shared/models/videos/video-streaming-playlist.model'
 import { isArray } from '../../../helpers/custom-validators/misc'
 import {
@@ -22,6 +23,7 @@ import {
   getLocalVideoSharesActivityPubUrl
 } from '../../../lib/activitypub/url'
 import {
+  MServer,
   MStreamingPlaylistRedundanciesOpt,
   MVideo,
   MVideoAP,
@@ -34,15 +36,33 @@ import { VideoCaptionModel } from '../video-caption'
 
 export type VideoFormattingJSONOptions = {
   completeDescription?: boolean
-  additionalAttributes: {
+
+  additionalAttributes?: {
     state?: boolean
     waitTranscoding?: boolean
     scheduledUpdate?: boolean
     blacklistInfo?: boolean
+    files?: boolean
+    blockedOwner?: boolean
+  }
+}
+
+function guessAdditionalAttributesFromQuery (query: VideosCommonQueryAfterSanitize): VideoFormattingJSONOptions {
+  if (!query || !query.include) return {}
+
+  return {
+    additionalAttributes: {
+      state: !!(query.include & VideoInclude.NOT_PUBLISHED_STATE),
+      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)
+    }
   }
 }
 
-function videoModelToFormattedJSON (video: MVideoFormattable, options?: VideoFormattingJSONOptions): Video {
+function videoModelToFormattedJSON (video: MVideoFormattable, options: VideoFormattingJSONOptions = {}): Video {
   const userHistory = isArray(video.UserVideoHistories) ? video.UserVideoHistories[0] : undefined
 
   const videoObject: Video = {
@@ -50,6 +70,8 @@ function videoModelToFormattedJSON (video: MVideoFormattable, options?: VideoFor
     uuid: video.uuid,
     shortUUID: uuidToShort(video.uuid),
 
+    url: video.url,
+
     name: video.name,
     category: {
       id: video.category,
@@ -99,47 +121,61 @@ function videoModelToFormattedJSON (video: MVideoFormattable, options?: VideoFor
     pluginData: (video as any).pluginData
   }
 
-  if (options) {
-    if (options.additionalAttributes.state === true) {
-      videoObject.state = {
-        id: video.state,
-        label: getStateLabel(video.state)
-      }
-    }
+  if (video.isLive) {
+    videoObject.viewers = VideoViews.Instance.getViewers(video)
+  }
 
-    if (options.additionalAttributes.waitTranscoding === true) {
-      videoObject.waitTranscoding = video.waitTranscoding
+  const add = options.additionalAttributes
+  if (add?.state === true) {
+    videoObject.state = {
+      id: video.state,
+      label: getStateLabel(video.state)
     }
+  }
 
-    if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
-      videoObject.scheduledUpdate = {
-        updateAt: video.ScheduleVideoUpdate.updateAt,
-        privacy: video.ScheduleVideoUpdate.privacy || undefined
-      }
-    }
+  if (add?.waitTranscoding === true) {
+    videoObject.waitTranscoding = video.waitTranscoding
+  }
 
-    if (options.additionalAttributes.blacklistInfo === true) {
-      videoObject.blacklisted = !!video.VideoBlacklist
-      videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
+  if (add?.scheduledUpdate === true && video.ScheduleVideoUpdate) {
+    videoObject.scheduledUpdate = {
+      updateAt: video.ScheduleVideoUpdate.updateAt,
+      privacy: video.ScheduleVideoUpdate.privacy || undefined
     }
   }
 
+  if (add?.blacklistInfo === true) {
+    videoObject.blacklisted = !!video.VideoBlacklist
+    videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
+  }
+
+  if (add?.blockedOwner === true) {
+    videoObject.blockedOwner = video.VideoChannel.Account.isBlocked()
+
+    const server = video.VideoChannel.Account.Actor.Server as MServer
+    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(),
@@ -153,20 +189,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 []
@@ -182,8 +212,8 @@ function streamingPlaylistsModelToFormattedJSON (
       return {
         id: playlist.id,
         type: playlist.type,
-        playlistUrl: playlist.playlistUrl,
-        segmentsSha256Url: playlist.segmentsSha256Url,
+        playlistUrl: playlist.getMasterPlaylistUrl(video),
+        segmentsSha256Url: playlist.getSha256SegmentsUrl(video),
         redundancies,
         files
       }
@@ -197,7 +227,7 @@ function sortByResolutionDesc (fileA: MVideoFile, fileB: MVideoFile) {
 }
 
 function videoFilesModelToFormattedJSON (
-  video: MVideoFormattableDetails,
+  video: MVideoFormattable,
   videoFiles: MVideoFileRedundanciesOpt[],
   includeMagnet = true
 ): VideoFile[] {
@@ -205,7 +235,7 @@ function videoFilesModelToFormattedJSON (
     ? video.getTrackerUrls()
     : []
 
-  return [ ...videoFiles ]
+  return (videoFiles || [])
     .filter(f => !f.isLive())
     .sort(sortByResolutionDesc)
     .map(videoFile => {
@@ -240,7 +270,7 @@ function addVideoFilesInAPAcc (
 ) {
   const trackerUrls = video.getTrackerUrls()
 
-  const sortedFiles = [ ...files ]
+  const sortedFiles = (files || [])
     .filter(f => !f.isLive())
     .sort(sortByResolutionDesc)
 
@@ -331,7 +361,7 @@ function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
       type: 'Link',
       name: 'sha256',
       mediaType: 'application/json' as 'application/json',
-      href: playlist.segmentsSha256Url
+      href: playlist.getSha256SegmentsUrl(video)
     })
 
     addVideoFilesInAPAcc(tag, video, playlist.VideoFiles || [])
@@ -339,7 +369,7 @@ function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
     url.push({
       type: 'Link',
       mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
-      href: playlist.playlistUrl,
+      href: playlist.getMasterPlaylistUrl(video),
       tag
     })
   }
@@ -462,6 +492,8 @@ export {
   videoModelToActivityPubObject,
   getActivityStreamDuration,
 
+  guessAdditionalAttributesFromQuery,
+
   getCategoryLabel,
   getLicenceLabel,
   getLanguageLabel,