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 listSessionsOfLiveForAPI (options: { videoId: number }) {
111 const { videoId } = options
113 const query: FindOptions<AttributesOnly<VideoLiveSessionModel>> = {
117 order: [ [ 'startDate', 'ASC' ] ]
120 return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findAll(query)
123 toFormattedJSON (this: MVideoLiveSessionReplay): LiveVideoSession {
124 const replayVideo = this.ReplayVideo
126 id: this.ReplayVideo.id,
127 uuid: this.ReplayVideo.uuid,
128 shortUUID: uuidToShort(this.ReplayVideo.uuid)
134 startDate: this.startDate.toISOString(),
135 endDate: this.endDate
136 ? this.endDate.toISOString()