]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-live-session.ts
Add expect message to ease debug
[github/Chocobozzz/PeerTube.git] / server / models / video / video-live-session.ts
CommitLineData
26e3e98f 1import { FindOptions } from 'sequelize'
05a60d85
W
2import {
3 AllowNull,
4 BeforeDestroy,
5 BelongsTo,
6 Column,
7 CreatedAt,
8 DataType,
9 ForeignKey,
10 Model,
11 Scopes,
12 Table,
13 UpdatedAt
14} from 'sequelize-typescript'
26e3e98f
C
15import { MVideoLiveSession, MVideoLiveSessionReplay } from '@server/types/models'
16import { uuidToShort } from '@shared/extra-utils'
17import { LiveVideoError, LiveVideoSession } from '@shared/models'
18import { AttributesOnly } from '@shared/typescript-utils'
19import { VideoModel } from './video'
05a60d85 20import { VideoLiveReplaySettingModel } from './video-live-replay-setting'
26e3e98f
C
21
22export enum ScopeNames {
23 WITH_REPLAY = 'WITH_REPLAY'
24}
25
26@Scopes(() => ({
27 [ScopeNames.WITH_REPLAY]: {
28 include: [
29 {
30 model: VideoModel.unscoped(),
31 as: 'ReplayVideo',
32 required: false
05a60d85
W
33 },
34 {
35 model: VideoLiveReplaySettingModel,
36 required: false
26e3e98f
C
37 }
38 ]
39 }
40}))
41@Table({
42 tableName: 'videoLiveSession',
43 indexes: [
44 {
45 fields: [ 'replayVideoId' ],
46 unique: true
47 },
48 {
49 fields: [ 'liveVideoId' ]
05a60d85
W
50 },
51 {
52 fields: [ 'replaySettingId' ],
53 unique: true
26e3e98f
C
54 }
55 ]
56})
57export class VideoLiveSessionModel extends Model<Partial<AttributesOnly<VideoLiveSessionModel>>> {
58
59 @CreatedAt
60 createdAt: Date
61
62 @UpdatedAt
63 updatedAt: Date
64
65 @AllowNull(false)
66 @Column(DataType.DATE)
67 startDate: Date
68
69 @AllowNull(true)
70 @Column(DataType.DATE)
71 endDate: Date
72
73 @AllowNull(true)
74 @Column
75 error: LiveVideoError
76
c8fa571f
C
77 @AllowNull(false)
78 @Column
79 saveReplay: boolean
80
81 @AllowNull(false)
82 @Column
83 endingProcessed: boolean
84
26e3e98f
C
85 @ForeignKey(() => VideoModel)
86 @Column
87 replayVideoId: number
88
89 @BelongsTo(() => VideoModel, {
90 foreignKey: {
91 allowNull: true,
92 name: 'replayVideoId'
93 },
94 as: 'ReplayVideo',
95 onDelete: 'set null'
96 })
97 ReplayVideo: VideoModel
98
99 @ForeignKey(() => VideoModel)
100 @Column
101 liveVideoId: number
102
103 @BelongsTo(() => VideoModel, {
104 foreignKey: {
105 allowNull: true,
106 name: 'liveVideoId'
107 },
108 as: 'LiveVideo',
109 onDelete: 'set null'
110 })
111 LiveVideo: VideoModel
112
05a60d85
W
113 @ForeignKey(() => VideoLiveReplaySettingModel)
114 @Column
115 replaySettingId: number
116
117 @BelongsTo(() => VideoLiveReplaySettingModel, {
118 foreignKey: {
119 allowNull: true
120 },
121 onDelete: 'set null'
122 })
123 ReplaySetting: VideoLiveReplaySettingModel
124
125 @BeforeDestroy
126 static deleteReplaySetting (instance: VideoLiveSessionModel) {
127 return VideoLiveReplaySettingModel.destroy({
128 where: {
129 id: instance.replaySettingId
130 }
131 })
132 }
133
26e3e98f
C
134 static load (id: number): Promise<MVideoLiveSession> {
135 return VideoLiveSessionModel.findOne({
136 where: { id }
137 })
138 }
139
140 static findSessionOfReplay (replayVideoId: number) {
141 const query = {
142 where: {
143 replayVideoId
144 }
145 }
146
147 return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findOne(query)
148 }
149
0c9668f7 150 static findCurrentSessionOf (videoUUID: string) {
26e3e98f
C
151 return VideoLiveSessionModel.findOne({
152 where: {
26e3e98f
C
153 endDate: null
154 },
0c9668f7
C
155 include: [
156 {
157 model: VideoModel.unscoped(),
158 as: 'LiveVideo',
159 required: true,
160 where: {
161 uuid: videoUUID
162 }
163 }
164 ],
26e3e98f
C
165 order: [ [ 'startDate', 'DESC' ] ]
166 })
167 }
168
46f7cd68
C
169 static findLatestSessionOf (videoId: number) {
170 return VideoLiveSessionModel.findOne({
171 where: {
172 liveVideoId: videoId
173 },
174 order: [ [ 'startDate', 'DESC' ] ]
175 })
176 }
177
26e3e98f
C
178 static listSessionsOfLiveForAPI (options: { videoId: number }) {
179 const { videoId } = options
180
a91e9beb 181 const query: FindOptions<AttributesOnly<VideoLiveSessionModel>> = {
26e3e98f
C
182 where: {
183 liveVideoId: videoId
184 },
185 order: [ [ 'startDate', 'ASC' ] ]
186 }
187
188 return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findAll(query)
189 }
190
191 toFormattedJSON (this: MVideoLiveSessionReplay): LiveVideoSession {
192 const replayVideo = this.ReplayVideo
193 ? {
194 id: this.ReplayVideo.id,
195 uuid: this.ReplayVideo.uuid,
196 shortUUID: uuidToShort(this.ReplayVideo.uuid)
197 }
198 : undefined
199
05a60d85
W
200 const replaySettings = this.replaySettingId
201 ? this.ReplaySetting.toFormattedJSON()
202 : undefined
203
26e3e98f
C
204 return {
205 id: this.id,
206 startDate: this.startDate.toISOString(),
207 endDate: this.endDate
208 ? this.endDate.toISOString()
209 : null,
c8fa571f
C
210 endingProcessed: this.endingProcessed,
211 saveReplay: this.saveReplay,
05a60d85 212 replaySettings,
26e3e98f
C
213 replayVideo,
214 error: this.error
215 }
216 }
217}