]>
Commit | Line | Data |
---|---|---|
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(true) | |
34 | @Column(DataType.STRING) | |
35 | streamKey: string | |
36 | ||
37 | @AllowNull(false) | |
38 | @Column | |
39 | saveReplay: boolean | |
40 | ||
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 | |
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 | ] | |
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 | ||
94 | toFormattedJSON (): LiveVideo { | |
95 | return { | |
96 | rtmpUrl: WEBSERVER.RTMP_URL, | |
97 | streamKey: this.streamKey, | |
98 | saveReplay: this.saveReplay | |
99 | } | |
100 | } | |
101 | } |