1 import { FindOptions } from 'sequelize'
14 } from 'sequelize-typescript'
15 import { MVideoLiveSession, MVideoLiveSessionReplay } from '@server/types/models'
16 import { uuidToShort } from '@shared/extra-utils'
17 import { LiveVideoError, LiveVideoSession } from '@shared/models'
18 import { AttributesOnly } from '@shared/typescript-utils'
19 import { VideoModel } from './video'
20 import { VideoLiveReplaySettingModel } from './video-live-replay-setting'
22 export enum ScopeNames {
23 WITH_REPLAY = 'WITH_REPLAY'
27 [ScopeNames.WITH_REPLAY]: {
30 model: VideoModel.unscoped(),
35 model: VideoLiveReplaySettingModel,
42 tableName: 'videoLiveSession',
45 fields: [ 'replayVideoId' ],
49 fields: [ 'liveVideoId' ]
52 fields: [ 'replaySettingId' ],
57 export class VideoLiveSessionModel extends Model<Partial<AttributesOnly<VideoLiveSessionModel>>> {
66 @Column(DataType.DATE)
70 @Column(DataType.DATE)
83 endingProcessed: boolean
85 @ForeignKey(() => VideoModel)
89 @BelongsTo(() => VideoModel, {
97 ReplayVideo: VideoModel
99 @ForeignKey(() => VideoModel)
103 @BelongsTo(() => VideoModel, {
111 LiveVideo: VideoModel
113 @ForeignKey(() => VideoLiveReplaySettingModel)
115 replaySettingId: number
117 @BelongsTo(() => VideoLiveReplaySettingModel, {
123 ReplaySetting: VideoLiveReplaySettingModel
126 static deleteReplaySetting (instance: VideoLiveSessionModel) {
127 return VideoLiveReplaySettingModel.destroy({
129 id: instance.replaySettingId
134 static load (id: number): Promise<MVideoLiveSession> {
135 return VideoLiveSessionModel.findOne({
140 static findSessionOfReplay (replayVideoId: number) {
147 return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findOne(query)
150 static findCurrentSessionOf (videoId: number) {
151 return VideoLiveSessionModel.findOne({
153 liveVideoId: videoId,
156 order: [ [ 'startDate', 'DESC' ] ]
160 static findLatestSessionOf (videoId: number) {
161 return VideoLiveSessionModel.findOne({
165 order: [ [ 'startDate', 'DESC' ] ]
169 static listSessionsOfLiveForAPI (options: { videoId: number }) {
170 const { videoId } = options
172 const query: FindOptions<AttributesOnly<VideoLiveSessionModel>> = {
176 order: [ [ 'startDate', 'ASC' ] ]
179 return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findAll(query)
182 toFormattedJSON (this: MVideoLiveSessionReplay): LiveVideoSession {
183 const replayVideo = this.ReplayVideo
185 id: this.ReplayVideo.id,
186 uuid: this.ReplayVideo.uuid,
187 shortUUID: uuidToShort(this.ReplayVideo.uuid)
191 const replaySettings = this.replaySettingId
192 ? this.ReplaySetting.toFormattedJSON()
197 startDate: this.startDate.toISOString(),
198 endDate: this.endDate
199 ? this.endDate.toISOString()
201 endingProcessed: this.endingProcessed,
202 saveReplay: this.saveReplay,