1 import { FindOptions } from 'sequelize'
2 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
3 import { MVideoLiveSession, MVideoLiveSessionReplay } from '@server/types/models'
4 import { uuidToShort } from '@shared/extra-utils'
5 import { LiveVideoError, LiveVideoSession } from '@shared/models'
6 import { AttributesOnly } from '@shared/typescript-utils'
7 import { VideoModel } from './video'
9 export enum ScopeNames {
10 WITH_REPLAY = 'WITH_REPLAY'
14 [ScopeNames.WITH_REPLAY]: {
17 model: VideoModel.unscoped(),
25 tableName: 'videoLiveSession',
28 fields: [ 'replayVideoId' ],
32 fields: [ 'liveVideoId' ]
36 export class VideoLiveSessionModel extends Model<Partial<AttributesOnly<VideoLiveSessionModel>>> {
45 @Column(DataType.DATE)
49 @Column(DataType.DATE)
62 endingProcessed: boolean
64 @ForeignKey(() => VideoModel)
68 @BelongsTo(() => VideoModel, {
76 ReplayVideo: VideoModel
78 @ForeignKey(() => VideoModel)
82 @BelongsTo(() => VideoModel, {
92 static load (id: number): Promise<MVideoLiveSession> {
93 return VideoLiveSessionModel.findOne({
98 static findSessionOfReplay (replayVideoId: number) {
105 return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findOne(query)
108 static findCurrentSessionOf (videoId: number) {
109 return VideoLiveSessionModel.findOne({
111 liveVideoId: videoId,
114 order: [ [ 'startDate', 'DESC' ] ]
118 static findLatestSessionOf (videoId: number) {
119 return VideoLiveSessionModel.findOne({
123 order: [ [ 'startDate', 'DESC' ] ]
127 static listSessionsOfLiveForAPI (options: { videoId: number }) {
128 const { videoId } = options
130 const query: FindOptions<AttributesOnly<VideoLiveSessionModel>> = {
134 order: [ [ 'startDate', 'ASC' ] ]
137 return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findAll(query)
140 toFormattedJSON (this: MVideoLiveSessionReplay): LiveVideoSession {
141 const replayVideo = this.ReplayVideo
143 id: this.ReplayVideo.id,
144 uuid: this.ReplayVideo.uuid,
145 shortUUID: uuidToShort(this.ReplayVideo.uuid)
151 startDate: this.startDate.toISOString(),
152 endDate: this.endDate
153 ? this.endDate.toISOString()
155 endingProcessed: this.endingProcessed,
156 saveReplay: this.saveReplay,