2023-07-31 07:34:36 -05:00
|
|
|
import { LiveVideoSession, type LiveVideoErrorType } from '@peertube/peertube-models'
|
|
|
|
import { uuidToShort } from '@peertube/peertube-node-utils'
|
|
|
|
import { AttributesOnly } from '@peertube/peertube-typescript-utils'
|
|
|
|
import { MVideoLiveSession, MVideoLiveSessionReplay } from '@server/types/models/index.js'
|
2022-05-03 04:38:07 -05:00
|
|
|
import { FindOptions } from 'sequelize'
|
2023-03-31 02:12:21 -05:00
|
|
|
import {
|
|
|
|
AllowNull,
|
|
|
|
BeforeDestroy,
|
|
|
|
BelongsTo,
|
|
|
|
Column,
|
|
|
|
CreatedAt,
|
|
|
|
DataType,
|
2024-02-22 03:12:04 -06:00
|
|
|
ForeignKey, Scopes,
|
2023-03-31 02:12:21 -05:00
|
|
|
Table,
|
|
|
|
UpdatedAt
|
|
|
|
} from 'sequelize-typescript'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { VideoLiveReplaySettingModel } from './video-live-replay-setting.js'
|
|
|
|
import { VideoModel } from './video.js'
|
2024-02-22 03:12:04 -06:00
|
|
|
import { SequelizeModel } from '../shared/index.js'
|
2022-05-03 04:38:07 -05:00
|
|
|
|
|
|
|
export enum ScopeNames {
|
|
|
|
WITH_REPLAY = 'WITH_REPLAY'
|
|
|
|
}
|
|
|
|
|
|
|
|
@Scopes(() => ({
|
|
|
|
[ScopeNames.WITH_REPLAY]: {
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: VideoModel.unscoped(),
|
|
|
|
as: 'ReplayVideo',
|
|
|
|
required: false
|
2023-03-31 02:12:21 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
model: VideoLiveReplaySettingModel,
|
|
|
|
required: false
|
2022-05-03 04:38:07 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
@Table({
|
|
|
|
tableName: 'videoLiveSession',
|
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'replayVideoId' ],
|
|
|
|
unique: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'liveVideoId' ]
|
2023-03-31 02:12:21 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'replaySettingId' ],
|
|
|
|
unique: true
|
2022-05-03 04:38:07 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2024-02-22 03:12:04 -06:00
|
|
|
export class VideoLiveSessionModel extends SequelizeModel<VideoLiveSessionModel> {
|
2022-05-03 04:38:07 -05:00
|
|
|
|
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
|
|
|
|
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Column(DataType.DATE)
|
|
|
|
startDate: Date
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Column(DataType.DATE)
|
|
|
|
endDate: Date
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@Column
|
2023-07-31 07:34:36 -05:00
|
|
|
error: LiveVideoErrorType
|
2022-05-03 04:38:07 -05:00
|
|
|
|
2022-07-22 08:22:21 -05:00
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
saveReplay: boolean
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
endingProcessed: boolean
|
|
|
|
|
2022-05-03 04:38:07 -05:00
|
|
|
@ForeignKey(() => VideoModel)
|
|
|
|
@Column
|
|
|
|
replayVideoId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => VideoModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: true,
|
|
|
|
name: 'replayVideoId'
|
|
|
|
},
|
|
|
|
as: 'ReplayVideo',
|
|
|
|
onDelete: 'set null'
|
|
|
|
})
|
2023-07-31 07:34:36 -05:00
|
|
|
ReplayVideo: Awaited<VideoModel>
|
2022-05-03 04:38:07 -05:00
|
|
|
|
|
|
|
@ForeignKey(() => VideoModel)
|
|
|
|
@Column
|
|
|
|
liveVideoId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => VideoModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: true,
|
|
|
|
name: 'liveVideoId'
|
|
|
|
},
|
|
|
|
as: 'LiveVideo',
|
|
|
|
onDelete: 'set null'
|
|
|
|
})
|
2023-07-31 07:34:36 -05:00
|
|
|
LiveVideo: Awaited<VideoModel>
|
2022-05-03 04:38:07 -05:00
|
|
|
|
2023-03-31 02:12:21 -05:00
|
|
|
@ForeignKey(() => VideoLiveReplaySettingModel)
|
|
|
|
@Column
|
|
|
|
replaySettingId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => VideoLiveReplaySettingModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: true
|
|
|
|
},
|
|
|
|
onDelete: 'set null'
|
|
|
|
})
|
2023-07-31 07:34:36 -05:00
|
|
|
ReplaySetting: Awaited<VideoLiveReplaySettingModel>
|
2023-03-31 02:12:21 -05:00
|
|
|
|
|
|
|
@BeforeDestroy
|
|
|
|
static deleteReplaySetting (instance: VideoLiveSessionModel) {
|
|
|
|
return VideoLiveReplaySettingModel.destroy({
|
|
|
|
where: {
|
|
|
|
id: instance.replaySettingId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-03 04:38:07 -05:00
|
|
|
static load (id: number): Promise<MVideoLiveSession> {
|
|
|
|
return VideoLiveSessionModel.findOne({
|
|
|
|
where: { id }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
static findSessionOfReplay (replayVideoId: number) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
replayVideoId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findOne(query)
|
|
|
|
}
|
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
static findCurrentSessionOf (videoUUID: string) {
|
2022-05-03 04:38:07 -05:00
|
|
|
return VideoLiveSessionModel.findOne({
|
|
|
|
where: {
|
|
|
|
endDate: null
|
|
|
|
},
|
2023-04-21 07:55:10 -05:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: VideoModel.unscoped(),
|
|
|
|
as: 'LiveVideo',
|
|
|
|
required: true,
|
|
|
|
where: {
|
|
|
|
uuid: videoUUID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
2022-05-03 04:38:07 -05:00
|
|
|
order: [ [ 'startDate', 'DESC' ] ]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-12 09:30:59 -05:00
|
|
|
static findLatestSessionOf (videoId: number) {
|
|
|
|
return VideoLiveSessionModel.findOne({
|
|
|
|
where: {
|
|
|
|
liveVideoId: videoId
|
|
|
|
},
|
|
|
|
order: [ [ 'startDate', 'DESC' ] ]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-03 04:38:07 -05:00
|
|
|
static listSessionsOfLiveForAPI (options: { videoId: number }) {
|
|
|
|
const { videoId } = options
|
|
|
|
|
2022-06-21 04:16:38 -05:00
|
|
|
const query: FindOptions<AttributesOnly<VideoLiveSessionModel>> = {
|
2022-05-03 04:38:07 -05:00
|
|
|
where: {
|
|
|
|
liveVideoId: videoId
|
|
|
|
},
|
|
|
|
order: [ [ 'startDate', 'ASC' ] ]
|
|
|
|
}
|
|
|
|
|
|
|
|
return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findAll(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
toFormattedJSON (this: MVideoLiveSessionReplay): LiveVideoSession {
|
|
|
|
const replayVideo = this.ReplayVideo
|
|
|
|
? {
|
|
|
|
id: this.ReplayVideo.id,
|
|
|
|
uuid: this.ReplayVideo.uuid,
|
|
|
|
shortUUID: uuidToShort(this.ReplayVideo.uuid)
|
|
|
|
}
|
|
|
|
: undefined
|
|
|
|
|
2023-03-31 02:12:21 -05:00
|
|
|
const replaySettings = this.replaySettingId
|
|
|
|
? this.ReplaySetting.toFormattedJSON()
|
|
|
|
: undefined
|
|
|
|
|
2022-05-03 04:38:07 -05:00
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
startDate: this.startDate.toISOString(),
|
|
|
|
endDate: this.endDate
|
|
|
|
? this.endDate.toISOString()
|
|
|
|
: null,
|
2022-07-22 08:22:21 -05:00
|
|
|
endingProcessed: this.endingProcessed,
|
|
|
|
saveReplay: this.saveReplay,
|
2023-03-31 02:12:21 -05:00
|
|
|
replaySettings,
|
2022-05-03 04:38:07 -05:00
|
|
|
replayVideo,
|
|
|
|
error: this.error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|