]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video.ts
Save
[github/Chocobozzz/PeerTube.git] / server / models / video / video.ts
index 43609587cf6f77a233e4c5af22d1cd18eccfe9a0..8493ab802bc8841205543d0d7bf129aa6f8bb772 100644 (file)
@@ -31,7 +31,7 @@ import { getServerActor } from '@server/models/application/application'
 import { ModelCache } from '@server/models/model-cache'
 import { VideoFile } from '@shared/models/videos/video-file.model'
 import { ResultList, UserRight, VideoPrivacy, VideoState } from '../../../shared'
-import { VideoTorrentObject } from '../../../shared/models/activitypub/objects'
+import { VideoObject } from '../../../shared/models/activitypub/objects'
 import { Video, VideoDetails } from '../../../shared/models/videos'
 import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type'
 import { VideoFilter } from '../../../shared/models/videos/video-query.type'
@@ -95,7 +95,7 @@ import {
   MVideoWithRights
 } from '../../types/models'
 import { MThumbnail } from '../../types/models/video/thumbnail'
-import { MVideoFile, MVideoFileStreamingPlaylistVideo } from '../../types/models/video/video-file'
+import { MVideoFile, MVideoFileRedundanciesOpt, MVideoFileStreamingPlaylistVideo } from '../../types/models/video/video-file'
 import { VideoAbuseModel } from '../abuse/video-abuse'
 import { AccountModel } from '../account/account'
 import { AccountVideoRateModel } from '../account/account-video-rate'
@@ -127,6 +127,7 @@ import { VideoShareModel } from './video-share'
 import { VideoStreamingPlaylistModel } from './video-streaming-playlist'
 import { VideoTagModel } from './video-tag'
 import { VideoViewModel } from './video-view'
+import { LiveManager } from '@server/lib/live-manager'
 
 export enum ScopeNames {
   AVAILABLE_FOR_LIST_IDS = 'AVAILABLE_FOR_LIST_IDS',
@@ -549,6 +550,11 @@ export class VideoModel extends Model<VideoModel> {
   @Column
   remote: boolean
 
+  @AllowNull(false)
+  @Default(false)
+  @Column
+  isLive: boolean
+
   @AllowNull(false)
   @Is('VideoUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
   @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
@@ -794,6 +800,13 @@ export class VideoModel extends Model<VideoModel> {
     return undefined
   }
 
+  @BeforeDestroy
+  static stopLiveIfNeeded (instance: VideoModel) {
+    if (!instance.isLive) return
+
+    return LiveManager.Instance.stopSessionOf(instance.id)
+  }
+
   @BeforeDestroy
   static invalidateCache (instance: VideoModel) {
     ModelCache.Instance.invalidateCache('video', instance.id)
@@ -1472,7 +1485,8 @@ export class VideoModel extends Model<VideoModel> {
   }
 
   private static buildAPIResult (rows: any[]) {
-    const memo: { [ id: number ]: VideoModel } = {}
+    const videosMemo: { [ id: number ]: VideoModel } = {}
+    const videoStreamingPlaylistMemo: { [ id: number ]: VideoStreamingPlaylistModel } = {}
 
     const thumbnailsDone = new Set<number>()
     const historyDone = new Set<number>()
@@ -1484,6 +1498,7 @@ export class VideoModel extends Model<VideoModel> {
     const actorKeys = [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ]
     const serverKeys = [ 'id', 'host' ]
     const videoFileKeys = [ 'id', 'createdAt', 'updatedAt', 'resolution', 'size', 'extname', 'infoHash', 'fps', 'videoId' ]
+    const videoStreamingPlaylistKeys = [ 'id' ]
     const videoKeys = [
       'id',
       'uuid',
@@ -1529,7 +1544,7 @@ export class VideoModel extends Model<VideoModel> {
     }
 
     for (const row of rows) {
-      if (!memo[row.id]) {
+      if (!videosMemo[row.id]) {
         // Build Channel
         const channel = row.VideoChannel
         const channelModel = new VideoChannelModel(pick(channel, [ 'id', 'name', 'description', 'actorId' ]))
@@ -1547,13 +1562,14 @@ export class VideoModel extends Model<VideoModel> {
         videoModel.UserVideoHistories = []
         videoModel.Thumbnails = []
         videoModel.VideoFiles = []
+        videoModel.VideoStreamingPlaylists = []
 
-        memo[row.id] = videoModel
+        videosMemo[row.id] = videoModel
         // Don't take object value to have a sorted array
         videos.push(videoModel)
       }
 
-      const videoModel = memo[row.id]
+      const videoModel = videosMemo[row.id]
 
       if (row.userVideoHistory?.id && !historyDone.has(row.userVideoHistory.id)) {
         const historyModel = new UserVideoHistoryModel(pick(row.userVideoHistory, [ 'id', 'currentTime' ]))
@@ -1575,6 +1591,31 @@ export class VideoModel extends Model<VideoModel> {
 
         videoFilesDone.add(row.VideoFiles.id)
       }
+
+      if (row.VideoFiles?.id && !videoFilesDone.has(row.VideoFiles.id)) {
+        const videoFileModel = new VideoFileModel(pick(row.VideoFiles, videoFileKeys))
+        videoModel.VideoFiles.push(videoFileModel)
+
+        videoFilesDone.add(row.VideoFiles.id)
+      }
+
+      if (row.VideoStreamingPlaylists?.id && !videoStreamingPlaylistMemo[row.VideoStreamingPlaylists.id]) {
+        const streamingPlaylist = new VideoStreamingPlaylistModel(pick(row.VideoStreamingPlaylists, videoStreamingPlaylistKeys))
+        streamingPlaylist.VideoFiles = []
+
+        videoModel.VideoStreamingPlaylists.push(streamingPlaylist)
+
+        videoStreamingPlaylistMemo[streamingPlaylist.id] = streamingPlaylist
+      }
+
+      if (row.VideoStreamingPlaylists?.VideoFiles?.id && !videoFilesDone.has(row.VideoStreamingPlaylists.VideoFiles.id)) {
+        const streamingPlaylist = videoStreamingPlaylistMemo[row.VideoStreamingPlaylists.id]
+
+        const videoFileModel = new VideoFileModel(pick(row.VideoStreamingPlaylists.VideoFiles, videoFileKeys))
+        streamingPlaylist.VideoFiles.push(videoFileModel)
+
+        videoFilesDone.add(row.VideoStreamingPlaylists.VideoFiles.id)
+      }
     }
 
     return videos
@@ -1717,10 +1758,20 @@ export class VideoModel extends Model<VideoModel> {
 
   getFormattedVideoFilesJSON (): VideoFile[] {
     const { baseUrlHttp, baseUrlWs } = this.getBaseUrls()
-    return videoFilesModelToFormattedJSON(this, baseUrlHttp, baseUrlWs, this.VideoFiles)
+    let files: MVideoFileRedundanciesOpt[] = []
+
+    if (Array.isArray(this.VideoFiles)) {
+      files = files.concat(this.VideoFiles)
+    }
+
+    for (const p of (this.VideoStreamingPlaylists || [])) {
+      files = files.concat(p.VideoFiles || [])
+    }
+
+    return videoFilesModelToFormattedJSON(this, baseUrlHttp, baseUrlWs, files)
   }
 
-  toActivityPubObject (this: MVideoAP): VideoTorrentObject {
+  toActivityPubObject (this: MVideoAP): VideoObject {
     return videoModelToActivityPubObject(this)
   }