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)
56 @ForeignKey(() => VideoModel)
60 @BelongsTo(() => VideoModel, {
68 ReplayVideo: VideoModel
70 @ForeignKey(() => VideoModel)
74 @BelongsTo(() => VideoModel, {
84 static load (id: number): Promise<MVideoLiveSession> {
85 return VideoLiveSessionModel.findOne({
90 static findSessionOfReplay (replayVideoId: number) {
97 return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findOne(query)
100 static findCurrentSessionOf (videoId: number) {
101 return VideoLiveSessionModel.findOne({
103 liveVideoId: videoId,
106 order: [ [ 'startDate', 'DESC' ] ]
110 static findLatestSessionOf (videoId: number) {
111 return VideoLiveSessionModel.findOne({
115 order: [ [ 'startDate', 'DESC' ] ]
119 static listSessionsOfLiveForAPI (options: { videoId: number }) {
120 const { videoId } = options
122 const query: FindOptions<AttributesOnly<VideoLiveSessionModel>> = {
126 order: [ [ 'startDate', 'ASC' ] ]
129 return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findAll(query)
132 toFormattedJSON (this: MVideoLiveSessionReplay): LiveVideoSession {
133 const replayVideo = this.ReplayVideo
135 id: this.ReplayVideo.id,
136 uuid: this.ReplayVideo.uuid,
137 shortUUID: uuidToShort(this.ReplayVideo.uuid)
143 startDate: this.startDate.toISOString(),
144 endDate: this.endDate
145 ? this.endDate.toISOString()