]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-live.ts
Feature/Add replay privacy (#5692)
[github/Chocobozzz/PeerTube.git] / server / models / video / video-live.ts
1 import {
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'
14 import { CONFIG } from '@server/initializers/config'
15 import { WEBSERVER } from '@server/initializers/constants'
16 import { MVideoLive, MVideoLiveVideoWithSetting } from '@server/types/models'
17 import { LiveVideo, LiveVideoLatencyMode, VideoState } from '@shared/models'
18 import { AttributesOnly } from '@shared/typescript-utils'
19 import { VideoModel } from './video'
20 import { VideoBlacklistModel } from './video-blacklist'
21 import { VideoLiveReplaySettingModel } from './video-live-replay-setting'
22
23 @DefaultScope(() => ({
24 include: [
25 {
26 model: VideoModel,
27 required: true,
28 include: [
29 {
30 model: VideoBlacklistModel,
31 required: false
32 }
33 ]
34 },
35 {
36 model: VideoLiveReplaySettingModel,
37 required: false
38 }
39 ]
40 }))
41 @Table({
42 tableName: 'videoLive',
43 indexes: [
44 {
45 fields: [ 'videoId' ],
46 unique: true
47 },
48 {
49 fields: [ 'replaySettingId' ],
50 unique: true
51 }
52 ]
53 })
54 export class VideoLiveModel extends Model<Partial<AttributesOnly<VideoLiveModel>>> {
55
56 @AllowNull(true)
57 @Column(DataType.STRING)
58 streamKey: string
59
60 @AllowNull(false)
61 @Column
62 saveReplay: boolean
63
64 @AllowNull(false)
65 @Column
66 permanentLive: boolean
67
68 @AllowNull(false)
69 @Column
70 latencyMode: LiveVideoLatencyMode
71
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
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
111 static loadByStreamKey (streamKey: string) {
112 const query = {
113 where: {
114 streamKey
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 ]
129 },
130 {
131 model: VideoLiveReplaySettingModel.unscoped(),
132 required: false
133 }
134 ]
135 }
136
137 return VideoLiveModel.findOne<MVideoLiveVideoWithSetting>(query)
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
150 toFormattedJSON (canSeePrivateInformation: boolean): LiveVideo {
151 let privateInformation: Pick<LiveVideo, 'rtmpUrl' | 'rtmpsUrl' | 'streamKey'> | {} = {}
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
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 }
167 }
168
169 const replaySettings = this.replaySettingId
170 ? this.ReplaySetting.toFormattedJSON()
171 : undefined
172
173 return {
174 ...privateInformation,
175
176 permanentLive: this.permanentLive,
177 saveReplay: this.saveReplay,
178 replaySettings,
179 latencyMode: this.latencyMode
180 }
181 }
182 }