]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-live.ts
Feature/Add replay privacy (#5692)
[github/Chocobozzz/PeerTube.git] / server / models / video / video-live.ts
CommitLineData
05a60d85
W
1import {
2 BeforeDestroy,
3 AllowNull,
4 BelongsTo,
5 Column,
6 CreatedAt,
7 DataType,
8 DefaultScope,
9 ForeignKey,
10 Model,
11 Table,
12 UpdatedAt
13} from 'sequelize-typescript'
f443a746 14import { CONFIG } from '@server/initializers/config'
c6c0fa6c 15import { WEBSERVER } from '@server/initializers/constants'
05a60d85 16import { MVideoLive, MVideoLiveVideoWithSetting } from '@server/types/models'
5d9b867e 17import { LiveVideo, LiveVideoLatencyMode, VideoState } from '@shared/models'
6b5f72be 18import { AttributesOnly } from '@shared/typescript-utils'
c6c0fa6c 19import { VideoModel } from './video'
a5cf76af 20import { VideoBlacklistModel } from './video-blacklist'
05a60d85 21import { VideoLiveReplaySettingModel } from './video-live-replay-setting'
c6c0fa6c
C
22
23@DefaultScope(() => ({
24 include: [
25 {
26 model: VideoModel,
a5cf76af
C
27 required: true,
28 include: [
29 {
30 model: VideoBlacklistModel,
31 required: false
32 }
33 ]
05a60d85
W
34 },
35 {
36 model: VideoLiveReplaySettingModel,
37 required: false
c6c0fa6c
C
38 }
39 ]
40}))
41@Table({
42 tableName: 'videoLive',
43 indexes: [
44 {
45 fields: [ 'videoId' ],
46 unique: true
05a60d85
W
47 },
48 {
49 fields: [ 'replaySettingId' ],
50 unique: true
c6c0fa6c
C
51 }
52 ]
53})
16c016e8 54export class VideoLiveModel extends Model<Partial<AttributesOnly<VideoLiveModel>>> {
c6c0fa6c 55
fb719404 56 @AllowNull(true)
c6c0fa6c
C
57 @Column(DataType.STRING)
58 streamKey: string
59
fb719404
C
60 @AllowNull(false)
61 @Column
62 saveReplay: boolean
63
bb4ba6d9
C
64 @AllowNull(false)
65 @Column
66 permanentLive: boolean
67
f443a746
C
68 @AllowNull(false)
69 @Column
70 latencyMode: LiveVideoLatencyMode
71
c6c0fa6c
C
72 @CreatedAt
73 createdAt: Date
74
75 @UpdatedAt
76 updatedAt: Date
77
78 @ForeignKey(() => VideoModel)
79 @Column
80 videoId: number
81
82 @BelongsTo(() => VideoModel, {
83 foreignKey: {
84 allowNull: false
85 },
86 onDelete: 'cascade'
87 })
88 Video: VideoModel
89
05a60d85
W
90 @ForeignKey(() => VideoLiveReplaySettingModel)
91 @Column
92 replaySettingId: number
93
94 @BelongsTo(() => VideoLiveReplaySettingModel, {
95 foreignKey: {
96 allowNull: true
97 },
98 onDelete: 'set null'
99 })
100 ReplaySetting: VideoLiveReplaySettingModel
101
102 @BeforeDestroy
103 static deleteReplaySetting (instance: VideoLiveModel) {
104 return VideoLiveReplaySettingModel.destroy({
105 where: {
106 id: instance.replaySettingId
107 }
108 })
109 }
110
c6c0fa6c
C
111 static loadByStreamKey (streamKey: string) {
112 const query = {
113 where: {
114 streamKey
a5cf76af
C
115 },
116 include: [
117 {
118 model: VideoModel.unscoped(),
119 required: true,
120 where: {
121 state: VideoState.WAITING_FOR_LIVE
122 },
123 include: [
124 {
125 model: VideoBlacklistModel.unscoped(),
126 required: false
127 }
128 ]
05a60d85
W
129 },
130 {
131 model: VideoLiveReplaySettingModel.unscoped(),
132 required: false
a5cf76af
C
133 }
134 ]
c6c0fa6c
C
135 }
136
05a60d85 137 return VideoLiveModel.findOne<MVideoLiveVideoWithSetting>(query)
c6c0fa6c
C
138 }
139
140 static loadByVideoId (videoId: number) {
141 const query = {
142 where: {
143 videoId
144 }
145 }
146
147 return VideoLiveModel.findOne<MVideoLive>(query)
148 }
149
961cbe42
C
150 toFormattedJSON (canSeePrivateInformation: boolean): LiveVideo {
151 let privateInformation: Pick<LiveVideo, 'rtmpUrl' | 'rtmpsUrl' | 'streamKey'> | {} = {}
df1db951
C
152
153 // If we don't have a stream key, it means this is a remote live so we don't specify the rtmp URL
961cbe42
C
154 // We also display these private information only to the live owne/moderators
155 if (this.streamKey && canSeePrivateInformation === true) {
156 privateInformation = {
157 streamKey: this.streamKey,
158
159 rtmpUrl: CONFIG.LIVE.RTMP.ENABLED
160 ? WEBSERVER.RTMP_URL
161 : null,
162
163 rtmpsUrl: CONFIG.LIVE.RTMPS.ENABLED
164 ? WEBSERVER.RTMPS_URL
165 : null
166 }
df1db951
C
167 }
168
05a60d85
W
169 const replaySettings = this.replaySettingId
170 ? this.ReplaySetting.toFormattedJSON()
171 : undefined
172
c6c0fa6c 173 return {
961cbe42 174 ...privateInformation,
af4ae64f 175
bb4ba6d9 176 permanentLive: this.permanentLive,
f443a746 177 saveReplay: this.saveReplay,
05a60d85 178 replaySettings,
f443a746 179 latencyMode: this.latencyMode
c6c0fa6c
C
180 }
181 }
182}