]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-live-session.ts
Translated using Weblate (Persian)
[github/Chocobozzz/PeerTube.git] / server / models / video / video-live-session.ts
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 (videoId: number) {
151 return VideoLiveSessionModel.findOne({
152 where: {
153 liveVideoId: videoId,
154 endDate: null
155 },
156 order: [ [ 'startDate', 'DESC' ] ]
157 })
158 }
159
160 static findLatestSessionOf (videoId: number) {
161 return VideoLiveSessionModel.findOne({
162 where: {
163 liveVideoId: videoId
164 },
165 order: [ [ 'startDate', 'DESC' ] ]
166 })
167 }
168
169 static listSessionsOfLiveForAPI (options: { videoId: number }) {
170 const { videoId } = options
171
172 const query: FindOptions<AttributesOnly<VideoLiveSessionModel>> = {
173 where: {
174 liveVideoId: videoId
175 },
176 order: [ [ 'startDate', 'ASC' ] ]
177 }
178
179 return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findAll(query)
180 }
181
182 toFormattedJSON (this: MVideoLiveSessionReplay): LiveVideoSession {
183 const replayVideo = this.ReplayVideo
184 ? {
185 id: this.ReplayVideo.id,
186 uuid: this.ReplayVideo.uuid,
187 shortUUID: uuidToShort(this.ReplayVideo.uuid)
188 }
189 : undefined
190
191 const replaySettings = this.replaySettingId
192 ? this.ReplaySetting.toFormattedJSON()
193 : undefined
194
195 return {
196 id: this.id,
197 startDate: this.startDate.toISOString(),
198 endDate: this.endDate
199 ? this.endDate.toISOString()
200 : null,
201 endingProcessed: this.endingProcessed,
202 saveReplay: this.saveReplay,
203 replaySettings,
204 replayVideo,
205 error: this.error
206 }
207 }
208 }