]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-live.ts
Fix getting live by anonymous user
[github/Chocobozzz/PeerTube.git] / server / models / video / video-live.ts
CommitLineData
c6c0fa6c 1import { AllowNull, BelongsTo, Column, CreatedAt, DataType, DefaultScope, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
f443a746 2import { CONFIG } from '@server/initializers/config'
c6c0fa6c
C
3import { WEBSERVER } from '@server/initializers/constants'
4import { MVideoLive, MVideoLiveVideo } from '@server/types/models'
4ec52d04 5import { LiveVideo, LiveVideoLatencyMode, VideoPrivacy, VideoState } from '@shared/models'
6b5f72be 6import { AttributesOnly } from '@shared/typescript-utils'
c6c0fa6c 7import { VideoModel } from './video'
a5cf76af 8import { VideoBlacklistModel } from './video-blacklist'
c6c0fa6c
C
9
10@DefaultScope(() => ({
11 include: [
12 {
13 model: VideoModel,
a5cf76af
C
14 required: true,
15 include: [
16 {
17 model: VideoBlacklistModel,
18 required: false
19 }
20 ]
c6c0fa6c
C
21 }
22 ]
23}))
24@Table({
25 tableName: 'videoLive',
26 indexes: [
27 {
28 fields: [ 'videoId' ],
29 unique: true
30 }
31 ]
32})
16c016e8 33export class VideoLiveModel extends Model<Partial<AttributesOnly<VideoLiveModel>>> {
c6c0fa6c 34
fb719404 35 @AllowNull(true)
c6c0fa6c
C
36 @Column(DataType.STRING)
37 streamKey: string
38
fb719404
C
39 @AllowNull(false)
40 @Column
41 saveReplay: boolean
42
bb4ba6d9
C
43 @AllowNull(false)
44 @Column
45 permanentLive: boolean
46
f443a746
C
47 @AllowNull(false)
48 @Column
49 latencyMode: LiveVideoLatencyMode
50
c6c0fa6c
C
51 @CreatedAt
52 createdAt: Date
53
54 @UpdatedAt
55 updatedAt: Date
56
57 @ForeignKey(() => VideoModel)
58 @Column
59 videoId: number
60
61 @BelongsTo(() => VideoModel, {
62 foreignKey: {
63 allowNull: false
64 },
65 onDelete: 'cascade'
66 })
67 Video: VideoModel
68
69 static loadByStreamKey (streamKey: string) {
70 const query = {
71 where: {
72 streamKey
a5cf76af
C
73 },
74 include: [
75 {
76 model: VideoModel.unscoped(),
77 required: true,
78 where: {
79 state: VideoState.WAITING_FOR_LIVE
80 },
81 include: [
82 {
83 model: VideoBlacklistModel.unscoped(),
84 required: false
85 }
86 ]
87 }
88 ]
c6c0fa6c
C
89 }
90
91 return VideoLiveModel.findOne<MVideoLiveVideo>(query)
92 }
93
94 static loadByVideoId (videoId: number) {
95 const query = {
96 where: {
97 videoId
98 }
99 }
100
101 return VideoLiveModel.findOne<MVideoLive>(query)
102 }
103
961cbe42
C
104 toFormattedJSON (canSeePrivateInformation: boolean): LiveVideo {
105 let privateInformation: Pick<LiveVideo, 'rtmpUrl' | 'rtmpsUrl' | 'streamKey'> | {} = {}
df1db951
C
106
107 // 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
108 // We also display these private information only to the live owne/moderators
109 if (this.streamKey && canSeePrivateInformation === true) {
110 privateInformation = {
111 streamKey: this.streamKey,
112
113 rtmpUrl: CONFIG.LIVE.RTMP.ENABLED
114 ? WEBSERVER.RTMP_URL
115 : null,
116
117 rtmpsUrl: CONFIG.LIVE.RTMPS.ENABLED
118 ? WEBSERVER.RTMPS_URL
119 : null
120 }
df1db951
C
121 }
122
c6c0fa6c 123 return {
961cbe42 124 ...privateInformation,
af4ae64f 125
bb4ba6d9 126 permanentLive: this.permanentLive,
f443a746
C
127 saveReplay: this.saveReplay,
128 latencyMode: this.latencyMode
c6c0fa6c
C
129 }
130 }
131}