]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-live.ts
Optimize rate endpoint
[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'
a5cf76af 4import { LiveVideo, VideoState } from '@shared/models'
c6c0fa6c 5import { VideoModel } from './video'
a5cf76af 6import { VideoBlacklistModel } from './video-blacklist'
c6c0fa6c
C
7
8@DefaultScope(() => ({
9 include: [
10 {
11 model: VideoModel,
a5cf76af
C
12 required: true,
13 include: [
14 {
15 model: VideoBlacklistModel,
16 required: false
17 }
18 ]
c6c0fa6c
C
19 }
20 ]
21}))
22@Table({
23 tableName: 'videoLive',
24 indexes: [
25 {
26 fields: [ 'videoId' ],
27 unique: true
28 }
29 ]
30})
b49f22d8 31export class VideoLiveModel extends Model {
c6c0fa6c 32
fb719404 33 @AllowNull(true)
c6c0fa6c
C
34 @Column(DataType.STRING)
35 streamKey: string
36
fb719404
C
37 @AllowNull(false)
38 @Column
39 saveReplay: boolean
40
bb4ba6d9
C
41 @AllowNull(false)
42 @Column
43 permanentLive: boolean
44
c6c0fa6c
C
45 @CreatedAt
46 createdAt: Date
47
48 @UpdatedAt
49 updatedAt: Date
50
51 @ForeignKey(() => VideoModel)
52 @Column
53 videoId: number
54
55 @BelongsTo(() => VideoModel, {
56 foreignKey: {
57 allowNull: false
58 },
59 onDelete: 'cascade'
60 })
61 Video: VideoModel
62
63 static loadByStreamKey (streamKey: string) {
64 const query = {
65 where: {
66 streamKey
a5cf76af
C
67 },
68 include: [
69 {
70 model: VideoModel.unscoped(),
71 required: true,
72 where: {
73 state: VideoState.WAITING_FOR_LIVE
74 },
75 include: [
76 {
77 model: VideoBlacklistModel.unscoped(),
78 required: false
79 }
80 ]
81 }
82 ]
c6c0fa6c
C
83 }
84
85 return VideoLiveModel.findOne<MVideoLiveVideo>(query)
86 }
87
88 static loadByVideoId (videoId: number) {
89 const query = {
90 where: {
91 videoId
92 }
93 }
94
95 return VideoLiveModel.findOne<MVideoLive>(query)
96 }
97
a5cf76af 98 toFormattedJSON (): LiveVideo {
c6c0fa6c 99 return {
af4ae64f
C
100 // If we don't have a stream key, it means this is a remote live so we don't specify the rtmp URL
101 rtmpUrl: this.streamKey
102 ? WEBSERVER.RTMP_URL
103 : null,
104
b5b68755 105 streamKey: this.streamKey,
bb4ba6d9 106 permanentLive: this.permanentLive,
b5b68755 107 saveReplay: this.saveReplay
c6c0fa6c
C
108 }
109 }
110}