]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-live.ts
refactor 404 page
[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})
31export class VideoLiveModel extends Model<VideoLiveModel> {
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
c6c0fa6c
C
41 @CreatedAt
42 createdAt: Date
43
44 @UpdatedAt
45 updatedAt: Date
46
47 @ForeignKey(() => VideoModel)
48 @Column
49 videoId: number
50
51 @BelongsTo(() => VideoModel, {
52 foreignKey: {
53 allowNull: false
54 },
55 onDelete: 'cascade'
56 })
57 Video: VideoModel
58
59 static loadByStreamKey (streamKey: string) {
60 const query = {
61 where: {
62 streamKey
a5cf76af
C
63 },
64 include: [
65 {
66 model: VideoModel.unscoped(),
67 required: true,
68 where: {
69 state: VideoState.WAITING_FOR_LIVE
70 },
71 include: [
72 {
73 model: VideoBlacklistModel.unscoped(),
74 required: false
75 }
76 ]
77 }
78 ]
c6c0fa6c
C
79 }
80
81 return VideoLiveModel.findOne<MVideoLiveVideo>(query)
82 }
83
84 static loadByVideoId (videoId: number) {
85 const query = {
86 where: {
87 videoId
88 }
89 }
90
91 return VideoLiveModel.findOne<MVideoLive>(query)
92 }
93
a5cf76af 94 toFormattedJSON (): LiveVideo {
c6c0fa6c 95 return {
af4ae64f
C
96 // If we don't have a stream key, it means this is a remote live so we don't specify the rtmp URL
97 rtmpUrl: this.streamKey
98 ? WEBSERVER.RTMP_URL
99 : null,
100
b5b68755
C
101 streamKey: this.streamKey,
102 saveReplay: this.saveReplay
c6c0fa6c
C
103 }
104 }
105}