]>
Commit | Line | Data |
---|---|---|
1 | import { FindOptions } from 'sequelize' | |
2 | import { | |
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' | |
15 | import { MVideoLiveSession, MVideoLiveSessionReplay } from '@server/types/models' | |
16 | import { uuidToShort } from '@shared/extra-utils' | |
17 | import { LiveVideoError, LiveVideoSession } from '@shared/models' | |
18 | import { AttributesOnly } from '@shared/typescript-utils' | |
19 | import { VideoModel } from './video' | |
20 | import { VideoLiveReplaySettingModel } from './video-live-replay-setting' | |
21 | ||
22 | export 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 | |
33 | }, | |
34 | { | |
35 | model: VideoLiveReplaySettingModel, | |
36 | required: false | |
37 | } | |
38 | ] | |
39 | } | |
40 | })) | |
41 | @Table({ | |
42 | tableName: 'videoLiveSession', | |
43 | indexes: [ | |
44 | { | |
45 | fields: [ 'replayVideoId' ], | |
46 | unique: true | |
47 | }, | |
48 | { | |
49 | fields: [ 'liveVideoId' ] | |
50 | }, | |
51 | { | |
52 | fields: [ 'replaySettingId' ], | |
53 | unique: true | |
54 | } | |
55 | ] | |
56 | }) | |
57 | export 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 | ||
77 | @AllowNull(false) | |
78 | @Column | |
79 | saveReplay: boolean | |
80 | ||
81 | @AllowNull(false) | |
82 | @Column | |
83 | endingProcessed: boolean | |
84 | ||
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 | ||
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 | ||
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 | ||
150 | static findCurrentSessionOf (videoUUID: string) { | |
151 | return VideoLiveSessionModel.findOne({ | |
152 | where: { | |
153 | endDate: null | |
154 | }, | |
155 | include: [ | |
156 | { | |
157 | model: VideoModel.unscoped(), | |
158 | as: 'LiveVideo', | |
159 | required: true, | |
160 | where: { | |
161 | uuid: videoUUID | |
162 | } | |
163 | } | |
164 | ], | |
165 | order: [ [ 'startDate', 'DESC' ] ] | |
166 | }) | |
167 | } | |
168 | ||
169 | static findLatestSessionOf (videoId: number) { | |
170 | return VideoLiveSessionModel.findOne({ | |
171 | where: { | |
172 | liveVideoId: videoId | |
173 | }, | |
174 | order: [ [ 'startDate', 'DESC' ] ] | |
175 | }) | |
176 | } | |
177 | ||
178 | static listSessionsOfLiveForAPI (options: { videoId: number }) { | |
179 | const { videoId } = options | |
180 | ||
181 | const query: FindOptions<AttributesOnly<VideoLiveSessionModel>> = { | |
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 | ||
200 | const replaySettings = this.replaySettingId | |
201 | ? this.ReplaySetting.toFormattedJSON() | |
202 | : undefined | |
203 | ||
204 | return { | |
205 | id: this.id, | |
206 | startDate: this.startDate.toISOString(), | |
207 | endDate: this.endDate | |
208 | ? this.endDate.toISOString() | |
209 | : null, | |
210 | endingProcessed: this.endingProcessed, | |
211 | saveReplay: this.saveReplay, | |
212 | replaySettings, | |
213 | replayVideo, | |
214 | error: this.error | |
215 | } | |
216 | } | |
217 | } |