]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-live-session.ts
Prevent object storage mock conflicts
[github/Chocobozzz/PeerTube.git] / server / models / video / video-live-session.ts
index 8366208729bdcc72b1571a66e4ed9c28a1f922aa..9426f5d11510c18ea754892a501a9c93c90c8c37 100644 (file)
@@ -1,10 +1,23 @@
 import { FindOptions } from 'sequelize'
-import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
+import {
+  AllowNull,
+  BeforeDestroy,
+  BelongsTo,
+  Column,
+  CreatedAt,
+  DataType,
+  ForeignKey,
+  Model,
+  Scopes,
+  Table,
+  UpdatedAt
+} from 'sequelize-typescript'
 import { MVideoLiveSession, MVideoLiveSessionReplay } from '@server/types/models'
 import { uuidToShort } from '@shared/extra-utils'
 import { LiveVideoError, LiveVideoSession } from '@shared/models'
 import { AttributesOnly } from '@shared/typescript-utils'
 import { VideoModel } from './video'
+import { VideoLiveReplaySettingModel } from './video-live-replay-setting'
 
 export enum ScopeNames {
   WITH_REPLAY = 'WITH_REPLAY'
@@ -17,6 +30,10 @@ export enum ScopeNames {
         model: VideoModel.unscoped(),
         as: 'ReplayVideo',
         required: false
+      },
+      {
+        model: VideoLiveReplaySettingModel,
+        required: false
       }
     ]
   }
@@ -30,6 +47,10 @@ export enum ScopeNames {
     },
     {
       fields: [ 'liveVideoId' ]
+    },
+    {
+      fields: [ 'replaySettingId' ],
+      unique: true
     }
   ]
 })
@@ -53,6 +74,14 @@ export class VideoLiveSessionModel extends Model<Partial<AttributesOnly<VideoLiv
   @Column
   error: LiveVideoError
 
+  @AllowNull(false)
+  @Column
+  saveReplay: boolean
+
+  @AllowNull(false)
+  @Column
+  endingProcessed: boolean
+
   @ForeignKey(() => VideoModel)
   @Column
   replayVideoId: number
@@ -81,6 +110,27 @@ export class VideoLiveSessionModel extends Model<Partial<AttributesOnly<VideoLiv
   })
   LiveVideo: VideoModel
 
+  @ForeignKey(() => VideoLiveReplaySettingModel)
+  @Column
+  replaySettingId: number
+
+  @BelongsTo(() => VideoLiveReplaySettingModel, {
+    foreignKey: {
+      allowNull: true
+    },
+    onDelete: 'set null'
+  })
+  ReplaySetting: VideoLiveReplaySettingModel
+
+  @BeforeDestroy
+  static deleteReplaySetting (instance: VideoLiveSessionModel) {
+    return VideoLiveReplaySettingModel.destroy({
+      where: {
+        id: instance.replaySettingId
+      }
+    })
+  }
+
   static load (id: number): Promise<MVideoLiveSession> {
     return VideoLiveSessionModel.findOne({
       where: { id }
@@ -97,12 +147,30 @@ export class VideoLiveSessionModel extends Model<Partial<AttributesOnly<VideoLiv
     return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findOne(query)
   }
 
-  static findCurrentSessionOf (videoId: number) {
+  static findCurrentSessionOf (videoUUID: string) {
     return VideoLiveSessionModel.findOne({
       where: {
-        liveVideoId: videoId,
         endDate: null
       },
+      include: [
+        {
+          model: VideoModel.unscoped(),
+          as: 'LiveVideo',
+          required: true,
+          where: {
+            uuid: videoUUID
+          }
+        }
+      ],
+      order: [ [ 'startDate', 'DESC' ] ]
+    })
+  }
+
+  static findLatestSessionOf (videoId: number) {
+    return VideoLiveSessionModel.findOne({
+      where: {
+        liveVideoId: videoId
+      },
       order: [ [ 'startDate', 'DESC' ] ]
     })
   }
@@ -129,12 +197,19 @@ export class VideoLiveSessionModel extends Model<Partial<AttributesOnly<VideoLiv
       }
       : undefined
 
+    const replaySettings = this.replaySettingId
+      ? this.ReplaySetting.toFormattedJSON()
+      : undefined
+
     return {
       id: this.id,
       startDate: this.startDate.toISOString(),
       endDate: this.endDate
         ? this.endDate.toISOString()
         : null,
+      endingProcessed: this.endingProcessed,
+      saveReplay: this.saveReplay,
+      replaySettings,
       replayVideo,
       error: this.error
     }