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'
model: VideoModel.unscoped(),
as: 'ReplayVideo',
required: false
+ },
+ {
+ model: VideoLiveReplaySettingModel,
+ required: false
}
]
}
},
{
fields: [ 'liveVideoId' ]
+ },
+ {
+ fields: [ 'replaySettingId' ],
+ unique: true
}
]
})
})
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 }
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' ] ]
})
}
}
: undefined
+ const replaySettings = this.replaySettingId
+ ? this.ReplaySetting.toFormattedJSON()
+ : undefined
+
return {
id: this.id,
startDate: this.startDate.toISOString(),
: null,
endingProcessed: this.endingProcessed,
saveReplay: this.saveReplay,
+ replaySettings,
replayVideo,
error: this.error
}