]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-format-utils.ts
Merge branch 'release/3.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / models / video / video-format-utils.ts
index adf46073412bc9753f17b5d68b28545a6b2159b1..551cb2842aa6883ae69f02506c802c843b63e9ec 100644 (file)
@@ -14,13 +14,11 @@ import {
 } from '../../lib/activitypub/url'
 import {
   MStreamingPlaylistRedundanciesOpt,
-  MStreamingPlaylistVideo,
   MVideo,
   MVideoAP,
   MVideoFile,
   MVideoFormattable,
-  MVideoFormattableDetails,
-  MVideoWithHost
+  MVideoFormattableDetails
 } from '../../types/models'
 import { MVideoFileRedundanciesOpt } from '../../types/models/video/video-file'
 import { VideoModel } from './video'
@@ -127,8 +125,6 @@ function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): Vid
     }
   })
 
-  const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
-
   const tags = video.Tags ? video.Tags.map(t => t.name) : []
 
   const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
@@ -147,14 +143,14 @@ function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): Vid
       label: VideoModel.getStateLabel(video.state)
     },
 
-    trackerUrls: video.getTrackerUrls(baseUrlHttp, baseUrlWs),
+    trackerUrls: video.getTrackerUrls(),
 
     files: [],
     streamingPlaylists
   }
 
   // Format and sort video files
-  detailsJson.files = videoFilesModelToFormattedJSON(video, video, baseUrlHttp, baseUrlWs, video.VideoFiles)
+  detailsJson.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
 
   return Object.assign(formattedJson, detailsJson)
 }
@@ -165,17 +161,13 @@ function streamingPlaylistsModelToFormattedJSON (
 ): VideoStreamingPlaylist[] {
   if (isArray(playlists) === false) return []
 
-  const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
-
   return playlists
     .map(playlist => {
-      const playlistWithVideo = Object.assign(playlist, { Video: video })
-
       const redundancies = isArray(playlist.RedundancyVideos)
         ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
         : []
 
-      const files = videoFilesModelToFormattedJSON(playlistWithVideo, video, baseUrlHttp, baseUrlWs, playlist.VideoFiles)
+      const files = videoFilesModelToFormattedJSON(video, playlist.VideoFiles)
 
       return {
         id: playlist.id,
@@ -194,14 +186,15 @@ function sortByResolutionDesc (fileA: MVideoFile, fileB: MVideoFile) {
   return -1
 }
 
-// FIXME: refactor/merge model and video arguments
 function videoFilesModelToFormattedJSON (
-  model: MVideo | MStreamingPlaylistVideo,
   video: MVideoFormattableDetails,
-  baseUrlHttp: string,
-  baseUrlWs: string,
-  videoFiles: MVideoFileRedundanciesOpt[]
+  videoFiles: MVideoFileRedundanciesOpt[],
+  includeMagnet = true
 ): VideoFile[] {
+  const trackerUrls = includeMagnet
+    ? video.getTrackerUrls()
+    : []
+
   return [ ...videoFiles ]
     .filter(f => !f.isLive())
     .sort(sortByResolutionDesc)
@@ -212,8 +205,9 @@ function videoFilesModelToFormattedJSON (
           label: videoFile.resolution + 'p'
         },
 
-        // FIXME: deprecated in 3.2
-        magnetUri: generateMagnetUri(model, video, videoFile, baseUrlHttp, baseUrlWs),
+        magnetUri: includeMagnet && videoFile.hasTorrent()
+          ? generateMagnetUri(video, videoFile, trackerUrls)
+          : undefined,
 
         size: videoFile.size,
         fps: videoFile.fps,
@@ -229,15 +223,13 @@ function videoFilesModelToFormattedJSON (
     })
 }
 
-// FIXME: refactor/merge model and video arguments
 function addVideoFilesInAPAcc (
   acc: ActivityUrlObject[] | ActivityTagObject[],
-  model: MVideoAP | MStreamingPlaylistVideo,
-  video: MVideoWithHost,
-  baseUrlHttp: string,
-  baseUrlWs: string,
+  video: MVideo,
   files: MVideoFile[]
 ) {
+  const trackerUrls = video.getTrackerUrls()
+
   const sortedFiles = [ ...files ]
     .filter(f => !f.isLive())
     .sort(sortByResolutionDesc)
@@ -261,24 +253,25 @@ function addVideoFilesInAPAcc (
       fps: file.fps
     })
 
-    acc.push({
-      type: 'Link',
-      mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
-      href: file.getTorrentUrl(),
-      height: file.resolution
-    })
-
-    acc.push({
-      type: 'Link',
-      mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
-      href: generateMagnetUri(model, video, file, baseUrlHttp, baseUrlWs),
-      height: file.resolution
-    })
+    if (file.hasTorrent()) {
+      acc.push({
+        type: 'Link',
+        mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
+        href: file.getTorrentUrl(),
+        height: file.resolution
+      })
+
+      acc.push({
+        type: 'Link',
+        mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
+        href: generateMagnetUri(video, file, trackerUrls),
+        height: file.resolution
+      })
+    }
   }
 }
 
 function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
-  const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
   if (!video.Tags) video.Tags = []
 
   const tag = video.Tags.map(t => ({
@@ -319,7 +312,7 @@ function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
     }
   ]
 
-  addVideoFilesInAPAcc(url, video, video, baseUrlHttp, baseUrlWs, video.VideoFiles || [])
+  addVideoFilesInAPAcc(url, video, video.VideoFiles || [])
 
   for (const playlist of (video.VideoStreamingPlaylists || [])) {
     const tag = playlist.p2pMediaLoaderInfohashes
@@ -331,8 +324,7 @@ function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
       href: playlist.segmentsSha256Url
     })
 
-    const playlistWithVideo = Object.assign(playlist, { Video: video })
-    addVideoFilesInAPAcc(tag, playlistWithVideo, video, baseUrlHttp, baseUrlWs, playlist.VideoFiles || [])
+    addVideoFilesInAPAcc(tag, video, playlist.VideoFiles || [])
 
     url.push({
       type: 'Link',
@@ -342,6 +334,19 @@ function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
     })
   }
 
+  for (const trackerUrl of video.getTrackerUrls()) {
+    const rel2 = trackerUrl.startsWith('http')
+      ? 'http'
+      : 'websocket'
+
+    url.push({
+      type: 'Link',
+      name: `tracker-${rel2}`,
+      rel: [ 'tracker', rel2 ],
+      href: trackerUrl
+    })
+  }
+
   const subtitleLanguage = []
   for (const caption of video.VideoCaptions) {
     subtitleLanguage.push({