]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-live.ts
Add watch messages if live has not started
[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 { WEBSERVER } from '@server/initializers/constants'
3 import { MVideoLive, MVideoLiveVideo } from '@server/types/models'
4 import { LiveVideo, VideoState } from '@shared/models'
5 import { VideoModel } from './video'
6 import { VideoBlacklistModel } from './video-blacklist'
7
8 @DefaultScope(() => ({
9 include: [
10 {
11 model: VideoModel,
12 required: true,
13 include: [
14 {
15 model: VideoBlacklistModel,
16 required: false
17 }
18 ]
19 }
20 ]
21 }))
22 @Table({
23 tableName: 'videoLive',
24 indexes: [
25 {
26 fields: [ 'videoId' ],
27 unique: true
28 }
29 ]
30 })
31 export class VideoLiveModel extends Model<VideoLiveModel> {
32
33 @AllowNull(false)
34 @Column(DataType.STRING)
35 streamKey: string
36
37 @CreatedAt
38 createdAt: Date
39
40 @UpdatedAt
41 updatedAt: Date
42
43 @ForeignKey(() => VideoModel)
44 @Column
45 videoId: number
46
47 @BelongsTo(() => VideoModel, {
48 foreignKey: {
49 allowNull: false
50 },
51 onDelete: 'cascade'
52 })
53 Video: VideoModel
54
55 static loadByStreamKey (streamKey: string) {
56 const query = {
57 where: {
58 streamKey
59 },
60 include: [
61 {
62 model: VideoModel.unscoped(),
63 required: true,
64 where: {
65 state: VideoState.WAITING_FOR_LIVE
66 },
67 include: [
68 {
69 model: VideoBlacklistModel.unscoped(),
70 required: false
71 }
72 ]
73 }
74 ]
75 }
76
77 return VideoLiveModel.findOne<MVideoLiveVideo>(query)
78 }
79
80 static loadByVideoId (videoId: number) {
81 const query = {
82 where: {
83 videoId
84 }
85 }
86
87 return VideoLiveModel.findOne<MVideoLive>(query)
88 }
89
90 toFormattedJSON (): LiveVideo {
91 return {
92 rtmpUrl: WEBSERVER.RTMP_URL,
93 streamKey: this.streamKey
94 }
95 }
96 }