PeerTube/server/core/models/video/video-chapter.ts

96 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-02-22 10:12:04 +01:00
import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
2023-08-28 10:55:04 +02:00
import { MVideo, MVideoChapter } from '@server/types/models/index.js'
import { VideoChapter, VideoChapterObject } from '@peertube/peertube-models'
import { VideoModel } from './video.js'
import { Transaction } from 'sequelize'
import { getSort } from '../shared/sort.js'
2024-02-22 10:12:04 +01:00
import { SequelizeModel } from '../shared/sequelize-type.js'
2023-08-28 10:55:04 +02:00
@Table({
tableName: 'videoChapter',
indexes: [
{
fields: [ 'videoId', 'timecode' ],
unique: true
}
]
})
2024-02-22 10:12:04 +01:00
export class VideoChapterModel extends SequelizeModel<VideoChapterModel> {
2023-08-28 10:55:04 +02:00
@AllowNull(false)
@Column
timecode: number
@AllowNull(false)
@Column
title: string
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Video: Awaited<VideoModel>
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
static deleteChapters (videoId: number, transaction: Transaction) {
const query = {
where: {
videoId
},
transaction
}
return VideoChapterModel.destroy(query)
}
static listChaptersOfVideo (videoId: number, transaction?: Transaction) {
const query = {
where: {
videoId
},
order: getSort('timecode'),
transaction
}
return VideoChapterModel.findAll<MVideoChapter>(query)
}
static hasVideoChapters (videoId: number, transaction: Transaction) {
return VideoChapterModel.findOne({
where: { videoId },
transaction
}).then(c => !!c)
}
toActivityPubJSON (this: MVideoChapter, options: {
video: MVideo
nextChapter: MVideoChapter
}): VideoChapterObject {
return {
name: this.title,
startOffset: this.timecode,
endOffset: options.nextChapter
? options.nextChapter.timecode
: options.video.duration
}
}
toFormattedJSON (this: MVideoChapter): VideoChapter {
return {
timecode: this.timecode,
title: this.title
}
}
}