]>
Commit | Line | Data |
---|---|---|
26e3e98f C |
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' | |
8 | ||
9 | export enum ScopeNames { | |
10 | WITH_REPLAY = 'WITH_REPLAY' | |
11 | } | |
12 | ||
13 | @Scopes(() => ({ | |
14 | [ScopeNames.WITH_REPLAY]: { | |
15 | include: [ | |
16 | { | |
17 | model: VideoModel.unscoped(), | |
18 | as: 'ReplayVideo', | |
19 | required: false | |
20 | } | |
21 | ] | |
22 | } | |
23 | })) | |
24 | @Table({ | |
25 | tableName: 'videoLiveSession', | |
26 | indexes: [ | |
27 | { | |
28 | fields: [ 'replayVideoId' ], | |
29 | unique: true | |
30 | }, | |
31 | { | |
32 | fields: [ 'liveVideoId' ] | |
33 | } | |
34 | ] | |
35 | }) | |
36 | export class VideoLiveSessionModel extends Model<Partial<AttributesOnly<VideoLiveSessionModel>>> { | |
37 | ||
38 | @CreatedAt | |
39 | createdAt: Date | |
40 | ||
41 | @UpdatedAt | |
42 | updatedAt: Date | |
43 | ||
44 | @AllowNull(false) | |
45 | @Column(DataType.DATE) | |
46 | startDate: Date | |
47 | ||
48 | @AllowNull(true) | |
49 | @Column(DataType.DATE) | |
50 | endDate: Date | |
51 | ||
52 | @AllowNull(true) | |
53 | @Column | |
54 | error: LiveVideoError | |
55 | ||
c8fa571f C |
56 | @AllowNull(false) |
57 | @Column | |
58 | saveReplay: boolean | |
59 | ||
60 | @AllowNull(false) | |
61 | @Column | |
62 | endingProcessed: boolean | |
63 | ||
26e3e98f C |
64 | @ForeignKey(() => VideoModel) |
65 | @Column | |
66 | replayVideoId: number | |
67 | ||
68 | @BelongsTo(() => VideoModel, { | |
69 | foreignKey: { | |
70 | allowNull: true, | |
71 | name: 'replayVideoId' | |
72 | }, | |
73 | as: 'ReplayVideo', | |
74 | onDelete: 'set null' | |
75 | }) | |
76 | ReplayVideo: VideoModel | |
77 | ||
78 | @ForeignKey(() => VideoModel) | |
79 | @Column | |
80 | liveVideoId: number | |
81 | ||
82 | @BelongsTo(() => VideoModel, { | |
83 | foreignKey: { | |
84 | allowNull: true, | |
85 | name: 'liveVideoId' | |
86 | }, | |
87 | as: 'LiveVideo', | |
88 | onDelete: 'set null' | |
89 | }) | |
90 | LiveVideo: VideoModel | |
91 | ||
92 | static load (id: number): Promise<MVideoLiveSession> { | |
93 | return VideoLiveSessionModel.findOne({ | |
94 | where: { id } | |
95 | }) | |
96 | } | |
97 | ||
98 | static findSessionOfReplay (replayVideoId: number) { | |
99 | const query = { | |
100 | where: { | |
101 | replayVideoId | |
102 | } | |
103 | } | |
104 | ||
105 | return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findOne(query) | |
106 | } | |
107 | ||
108 | static findCurrentSessionOf (videoId: number) { | |
109 | return VideoLiveSessionModel.findOne({ | |
110 | where: { | |
111 | liveVideoId: videoId, | |
112 | endDate: null | |
113 | }, | |
114 | order: [ [ 'startDate', 'DESC' ] ] | |
115 | }) | |
116 | } | |
117 | ||
46f7cd68 C |
118 | static findLatestSessionOf (videoId: number) { |
119 | return VideoLiveSessionModel.findOne({ | |
120 | where: { | |
121 | liveVideoId: videoId | |
122 | }, | |
123 | order: [ [ 'startDate', 'DESC' ] ] | |
124 | }) | |
125 | } | |
126 | ||
26e3e98f C |
127 | static listSessionsOfLiveForAPI (options: { videoId: number }) { |
128 | const { videoId } = options | |
129 | ||
a91e9beb | 130 | const query: FindOptions<AttributesOnly<VideoLiveSessionModel>> = { |
26e3e98f C |
131 | where: { |
132 | liveVideoId: videoId | |
133 | }, | |
134 | order: [ [ 'startDate', 'ASC' ] ] | |
135 | } | |
136 | ||
137 | return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findAll(query) | |
138 | } | |
139 | ||
140 | toFormattedJSON (this: MVideoLiveSessionReplay): LiveVideoSession { | |
141 | const replayVideo = this.ReplayVideo | |
142 | ? { | |
143 | id: this.ReplayVideo.id, | |
144 | uuid: this.ReplayVideo.uuid, | |
145 | shortUUID: uuidToShort(this.ReplayVideo.uuid) | |
146 | } | |
147 | : undefined | |
148 | ||
149 | return { | |
150 | id: this.id, | |
151 | startDate: this.startDate.toISOString(), | |
152 | endDate: this.endDate | |
153 | ? this.endDate.toISOString() | |
154 | : null, | |
c8fa571f C |
155 | endingProcessed: this.endingProcessed, |
156 | saveReplay: this.saveReplay, | |
26e3e98f C |
157 | replayVideo, |
158 | error: this.error | |
159 | } | |
160 | } | |
161 | } |