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 (videoUUID: string) {
151 return VideoLiveSessionModel.findOne({
157 model: VideoModel.unscoped(),
165 order: [ [ 'startDate', 'DESC' ] ]
169 static findLatestSessionOf (videoId: number) {
170 return VideoLiveSessionModel.findOne({
174 order: [ [ 'startDate', 'DESC' ] ]
178 static listSessionsOfLiveForAPI (options: { videoId: number }) {
179 const { videoId } = options
181 const query: FindOptions<AttributesOnly<VideoLiveSessionModel>> = {
185 order: [ [ 'startDate', 'ASC' ] ]
188 return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findAll(query)
191 toFormattedJSON (this: MVideoLiveSessionReplay): LiveVideoSession {
192 const replayVideo = this.ReplayVideo
194 id: this.ReplayVideo.id,
195 uuid: this.ReplayVideo.uuid,
196 shortUUID: uuidToShort(this.ReplayVideo.uuid)
200 const replaySettings = this.replaySettingId
201 ? this.ReplaySetting.toFormattedJSON()
206 startDate: this.startDate.toISOString(),
207 endDate: this.endDate
208 ? this.endDate.toISOString()
210 endingProcessed: this.endingProcessed,
211 saveReplay: this.saveReplay,