Init commit
This commit is contained in:
139
server/core/models/video/video-password.ts
Normal file
139
server/core/models/video/video-password.ts
Normal file
@@ -0,0 +1,139 @@
|
||||
import { AllowNull, BelongsTo, Column, CreatedAt, DefaultScope, ForeignKey, Is, Table, UpdatedAt } from 'sequelize-typescript'
|
||||
import { VideoModel } from './video.js'
|
||||
import { ResultList, VideoPassword } from '@peertube/peertube-models'
|
||||
import { SequelizeModel, getSort, throwIfNotValid } from '../shared/index.js'
|
||||
import { Transaction } from 'sequelize'
|
||||
import { MVideoPassword } from '@server/types/models/index.js'
|
||||
import { isPasswordValid } from '@server/helpers/custom-validators/videos.js'
|
||||
import { pick } from '@peertube/peertube-core-utils'
|
||||
|
||||
@DefaultScope(() => ({
|
||||
include: [
|
||||
{
|
||||
model: VideoModel.unscoped(),
|
||||
required: true
|
||||
}
|
||||
]
|
||||
}))
|
||||
@Table({
|
||||
tableName: 'videoPassword',
|
||||
indexes: [
|
||||
{
|
||||
fields: [ 'videoId', 'password' ],
|
||||
unique: true
|
||||
}
|
||||
]
|
||||
})
|
||||
export class VideoPasswordModel extends SequelizeModel<VideoPasswordModel> {
|
||||
@AllowNull(false)
|
||||
@Is('VideoPassword', value => throwIfNotValid(value, isPasswordValid, 'videoPassword'))
|
||||
@Column
|
||||
declare password: string
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => VideoModel)
|
||||
@Column
|
||||
declare videoId: number
|
||||
|
||||
@BelongsTo(() => VideoModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Video: Awaited<VideoModel>
|
||||
|
||||
static async countByVideoId (videoId: number, t?: Transaction) {
|
||||
const query = {
|
||||
where: {
|
||||
videoId
|
||||
},
|
||||
transaction: t
|
||||
}
|
||||
|
||||
return VideoPasswordModel.count(query)
|
||||
}
|
||||
|
||||
static async loadByIdAndVideo (options: { id: number, videoId: number, t?: Transaction }): Promise<MVideoPassword> {
|
||||
const { id, videoId, t } = options
|
||||
const query = {
|
||||
where: {
|
||||
id,
|
||||
videoId
|
||||
},
|
||||
transaction: t
|
||||
}
|
||||
|
||||
return VideoPasswordModel.findOne(query)
|
||||
}
|
||||
|
||||
static async listPasswords (options: {
|
||||
start: number
|
||||
count: number
|
||||
sort: string
|
||||
videoId: number
|
||||
}): Promise<ResultList<MVideoPassword>> {
|
||||
const { start, count, sort, videoId } = options
|
||||
|
||||
const { count: total, rows: data } = await VideoPasswordModel.findAndCountAll({
|
||||
where: { videoId },
|
||||
order: getSort(sort),
|
||||
offset: start,
|
||||
limit: count
|
||||
})
|
||||
|
||||
return { total, data }
|
||||
}
|
||||
|
||||
static async addPasswords (passwords: string[], videoId: number, transaction?: Transaction): Promise<void> {
|
||||
for (const password of passwords) {
|
||||
await VideoPasswordModel.addPassword(password, videoId, transaction)
|
||||
}
|
||||
}
|
||||
|
||||
static async addPassword (password: string, videoId: number, transaction?: Transaction): Promise<VideoPasswordModel> {
|
||||
return await VideoPasswordModel.create({
|
||||
password,
|
||||
videoId
|
||||
}, { transaction })
|
||||
}
|
||||
|
||||
static async deleteAllPasswords (videoId: number, transaction?: Transaction) {
|
||||
await VideoPasswordModel.destroy({
|
||||
where: { videoId },
|
||||
transaction
|
||||
})
|
||||
}
|
||||
|
||||
static async deletePassword (passwordId: number, transaction?: Transaction) {
|
||||
await VideoPasswordModel.destroy({
|
||||
where: { id: passwordId },
|
||||
transaction
|
||||
})
|
||||
}
|
||||
|
||||
static async isACorrectPassword (options: {
|
||||
videoId: number
|
||||
password: string
|
||||
}) {
|
||||
const query = {
|
||||
where: pick(options, [ 'videoId', 'password' ])
|
||||
}
|
||||
return VideoPasswordModel.findOne(query)
|
||||
}
|
||||
|
||||
toFormattedJSON (): VideoPassword {
|
||||
return {
|
||||
id: this.id,
|
||||
password: this.password,
|
||||
videoId: this.videoId,
|
||||
createdAt: this.createdAt,
|
||||
updatedAt: this.updatedAt
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user