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