aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video-live-session.ts
blob: 8366208729bdcc72b1571a66e4ed9c28a1f922aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { FindOptions } from 'sequelize'
import { AllowNull, 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'

export enum ScopeNames {
  WITH_REPLAY = 'WITH_REPLAY'
}

@Scopes(() => ({
  [ScopeNames.WITH_REPLAY]: {
    include: [
      {
        model: VideoModel.unscoped(),
        as: 'ReplayVideo',
        required: false
      }
    ]
  }
}))
@Table({
  tableName: 'videoLiveSession',
  indexes: [
    {
      fields: [ 'replayVideoId' ],
      unique: true
    },
    {
      fields: [ 'liveVideoId' ]
    }
  ]
})
export class VideoLiveSessionModel extends Model<Partial<AttributesOnly<VideoLiveSessionModel>>> {

  @CreatedAt
  createdAt: Date

  @UpdatedAt
  updatedAt: Date

  @AllowNull(false)
  @Column(DataType.DATE)
  startDate: Date

  @AllowNull(true)
  @Column(DataType.DATE)
  endDate: Date

  @AllowNull(true)
  @Column
  error: LiveVideoError

  @ForeignKey(() => VideoModel)
  @Column
  replayVideoId: number

  @BelongsTo(() => VideoModel, {
    foreignKey: {
      allowNull: true,
      name: 'replayVideoId'
    },
    as: 'ReplayVideo',
    onDelete: 'set null'
  })
  ReplayVideo: VideoModel

  @ForeignKey(() => VideoModel)
  @Column
  liveVideoId: number

  @BelongsTo(() => VideoModel, {
    foreignKey: {
      allowNull: true,
      name: 'liveVideoId'
    },
    as: 'LiveVideo',
    onDelete: 'set null'
  })
  LiveVideo: VideoModel

  static load (id: number): Promise<MVideoLiveSession> {
    return VideoLiveSessionModel.findOne({
      where: { id }
    })
  }

  static findSessionOfReplay (replayVideoId: number) {
    const query = {
      where: {
        replayVideoId
      }
    }

    return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findOne(query)
  }

  static findCurrentSessionOf (videoId: number) {
    return VideoLiveSessionModel.findOne({
      where: {
        liveVideoId: videoId,
        endDate: null
      },
      order: [ [ 'startDate', 'DESC' ] ]
    })
  }

  static listSessionsOfLiveForAPI (options: { videoId: number }) {
    const { videoId } = options

    const query: FindOptions<AttributesOnly<VideoLiveSessionModel>> = {
      where: {
        liveVideoId: videoId
      },
      order: [ [ 'startDate', 'ASC' ] ]
    }

    return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findAll(query)
  }

  toFormattedJSON (this: MVideoLiveSessionReplay): LiveVideoSession {
    const replayVideo = this.ReplayVideo
      ? {
        id: this.ReplayVideo.id,
        uuid: this.ReplayVideo.uuid,
        shortUUID: uuidToShort(this.ReplayVideo.uuid)
      }
      : undefined

    return {
      id: this.id,
      startDate: this.startDate.toISOString(),
      endDate: this.endDate
        ? this.endDate.toISOString()
        : null,
      replayVideo,
      error: this.error
    }
  }
}