]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-live.ts
Move typescript utils in its own directory
[github/Chocobozzz/PeerTube.git] / server / models / video / video-live.ts
CommitLineData
c6c0fa6c
C
1import { AllowNull, BelongsTo, Column, CreatedAt, DataType, DefaultScope, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
2import { WEBSERVER } from '@server/initializers/constants'
3import { MVideoLive, MVideoLiveVideo } from '@server/types/models'
6b5f72be 4import { AttributesOnly } from '@shared/typescript-utils'
a5cf76af 5import { LiveVideo, VideoState } from '@shared/models'
c6c0fa6c 6import { VideoModel } from './video'
a5cf76af 7import { VideoBlacklistModel } from './video-blacklist'
df1db951 8import { CONFIG } from '@server/initializers/config'
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
c6c0fa6c
C
47 @CreatedAt
48 createdAt: Date
49
50 @UpdatedAt
51 updatedAt: Date
52
53 @ForeignKey(() => VideoModel)
54 @Column
55 videoId: number
56
57 @BelongsTo(() => VideoModel, {
58 foreignKey: {
59 allowNull: false
60 },
61 onDelete: 'cascade'
62 })
63 Video: VideoModel
64
65 static loadByStreamKey (streamKey: string) {
66 const query = {
67 where: {
68 streamKey
a5cf76af
C
69 },
70 include: [
71 {
72 model: VideoModel.unscoped(),
73 required: true,
74 where: {
75 state: VideoState.WAITING_FOR_LIVE
76 },
77 include: [
78 {
79 model: VideoBlacklistModel.unscoped(),
80 required: false
81 }
82 ]
83 }
84 ]
c6c0fa6c
C
85 }
86
87 return VideoLiveModel.findOne<MVideoLiveVideo>(query)
88 }
89
90 static loadByVideoId (videoId: number) {
91 const query = {
92 where: {
93 videoId
94 }
95 }
96
97 return VideoLiveModel.findOne<MVideoLive>(query)
98 }
99
a5cf76af 100 toFormattedJSON (): LiveVideo {
df1db951
C
101 let rtmpUrl: string = null
102 let rtmpsUrl: string = null
103
104 // If we don't have a stream key, it means this is a remote live so we don't specify the rtmp URL
105 if (this.streamKey) {
106 if (CONFIG.LIVE.RTMP.ENABLED) rtmpUrl = WEBSERVER.RTMP_URL
107 if (CONFIG.LIVE.RTMPS.ENABLED) rtmpsUrl = WEBSERVER.RTMPS_URL
108 }
109
c6c0fa6c 110 return {
df1db951
C
111 rtmpUrl,
112 rtmpsUrl,
af4ae64f 113
b5b68755 114 streamKey: this.streamKey,
bb4ba6d9 115 permanentLive: this.permanentLive,
b5b68755 116 saveReplay: this.saveReplay
c6c0fa6c
C
117 }
118 }
119}