]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-live.ts
Fix getting live by anonymous user
[github/Chocobozzz/PeerTube.git] / server / models / video / video-live.ts
1 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, DefaultScope, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
2 import { CONFIG } from '@server/initializers/config'
3 import { WEBSERVER } from '@server/initializers/constants'
4 import { MVideoLive, MVideoLiveVideo } from '@server/types/models'
5 import { LiveVideo, LiveVideoLatencyMode, VideoPrivacy, VideoState } from '@shared/models'
6 import { AttributesOnly } from '@shared/typescript-utils'
7 import { VideoModel } from './video'
8 import { VideoBlacklistModel } from './video-blacklist'
9
10 @DefaultScope(() => ({
11 include: [
12 {
13 model: VideoModel,
14 required: true,
15 include: [
16 {
17 model: VideoBlacklistModel,
18 required: false
19 }
20 ]
21 }
22 ]
23 }))
24 @Table({
25 tableName: 'videoLive',
26 indexes: [
27 {
28 fields: [ 'videoId' ],
29 unique: true
30 }
31 ]
32 })
33 export class VideoLiveModel extends Model<Partial<AttributesOnly<VideoLiveModel>>> {
34
35 @AllowNull(true)
36 @Column(DataType.STRING)
37 streamKey: string
38
39 @AllowNull(false)
40 @Column
41 saveReplay: boolean
42
43 @AllowNull(false)
44 @Column
45 permanentLive: boolean
46
47 @AllowNull(false)
48 @Column
49 latencyMode: LiveVideoLatencyMode
50
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
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 ]
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
104 toFormattedJSON (canSeePrivateInformation: boolean): LiveVideo {
105 let privateInformation: Pick<LiveVideo, 'rtmpUrl' | 'rtmpsUrl' | 'streamKey'> | {} = {}
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
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 }
121 }
122
123 return {
124 ...privateInformation,
125
126 permanentLive: this.permanentLive,
127 saveReplay: this.saveReplay,
128 latencyMode: this.latencyMode
129 }
130 }
131 }