]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-live-session.ts
758906a42ae9cf375c5377fb36bf858a547ea9d9
[github/Chocobozzz/PeerTube.git] / server / models / video / video-live-session.ts
1 import { FindOptions } from 'sequelize'
2 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
3 import { MVideoLiveSession, MVideoLiveSessionReplay } from '@server/types/models'
4 import { uuidToShort } from '@shared/extra-utils'
5 import { LiveVideoError, LiveVideoSession } from '@shared/models'
6 import { AttributesOnly } from '@shared/typescript-utils'
7 import { VideoModel } from './video'
8
9 export enum ScopeNames {
10 WITH_REPLAY = 'WITH_REPLAY'
11 }
12
13 @Scopes(() => ({
14 [ScopeNames.WITH_REPLAY]: {
15 include: [
16 {
17 model: VideoModel.unscoped(),
18 as: 'ReplayVideo',
19 required: false
20 }
21 ]
22 }
23 }))
24 @Table({
25 tableName: 'videoLiveSession',
26 indexes: [
27 {
28 fields: [ 'replayVideoId' ],
29 unique: true
30 },
31 {
32 fields: [ 'liveVideoId' ]
33 }
34 ]
35 })
36 export class VideoLiveSessionModel extends Model<Partial<AttributesOnly<VideoLiveSessionModel>>> {
37
38 @CreatedAt
39 createdAt: Date
40
41 @UpdatedAt
42 updatedAt: Date
43
44 @AllowNull(false)
45 @Column(DataType.DATE)
46 startDate: Date
47
48 @AllowNull(true)
49 @Column(DataType.DATE)
50 endDate: Date
51
52 @AllowNull(true)
53 @Column
54 error: LiveVideoError
55
56 @ForeignKey(() => VideoModel)
57 @Column
58 replayVideoId: number
59
60 @BelongsTo(() => VideoModel, {
61 foreignKey: {
62 allowNull: true,
63 name: 'replayVideoId'
64 },
65 as: 'ReplayVideo',
66 onDelete: 'set null'
67 })
68 ReplayVideo: VideoModel
69
70 @ForeignKey(() => VideoModel)
71 @Column
72 liveVideoId: number
73
74 @BelongsTo(() => VideoModel, {
75 foreignKey: {
76 allowNull: true,
77 name: 'liveVideoId'
78 },
79 as: 'LiveVideo',
80 onDelete: 'set null'
81 })
82 LiveVideo: VideoModel
83
84 static load (id: number): Promise<MVideoLiveSession> {
85 return VideoLiveSessionModel.findOne({
86 where: { id }
87 })
88 }
89
90 static findSessionOfReplay (replayVideoId: number) {
91 const query = {
92 where: {
93 replayVideoId
94 }
95 }
96
97 return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findOne(query)
98 }
99
100 static findCurrentSessionOf (videoId: number) {
101 return VideoLiveSessionModel.findOne({
102 where: {
103 liveVideoId: videoId,
104 endDate: null
105 },
106 order: [ [ 'startDate', 'DESC' ] ]
107 })
108 }
109
110 static findLatestSessionOf (videoId: number) {
111 return VideoLiveSessionModel.findOne({
112 where: {
113 liveVideoId: videoId
114 },
115 order: [ [ 'startDate', 'DESC' ] ]
116 })
117 }
118
119 static listSessionsOfLiveForAPI (options: { videoId: number }) {
120 const { videoId } = options
121
122 const query: FindOptions<AttributesOnly<VideoLiveSessionModel>> = {
123 where: {
124 liveVideoId: videoId
125 },
126 order: [ [ 'startDate', 'ASC' ] ]
127 }
128
129 return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findAll(query)
130 }
131
132 toFormattedJSON (this: MVideoLiveSessionReplay): LiveVideoSession {
133 const replayVideo = this.ReplayVideo
134 ? {
135 id: this.ReplayVideo.id,
136 uuid: this.ReplayVideo.uuid,
137 shortUUID: uuidToShort(this.ReplayVideo.uuid)
138 }
139 : undefined
140
141 return {
142 id: this.id,
143 startDate: this.startDate.toISOString(),
144 endDate: this.endDate
145 ? this.endDate.toISOString()
146 : null,
147 replayVideo,
148 error: this.error
149 }
150 }
151 }