2020-12-08 14:30:29 +01:00
|
|
|
import { FindOptions } from 'sequelize'
|
2019-04-18 11:28:17 +02:00
|
|
|
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
|
2020-12-08 14:30:29 +01:00
|
|
|
import { MVideoBlacklist, MVideoBlacklistFormattable } from '@server/types/models'
|
2021-12-16 18:04:16 +01:00
|
|
|
import { AttributesOnly } from '@shared/typescript-utils'
|
2019-04-02 11:26:47 +02:00
|
|
|
import { VideoBlacklist, VideoBlacklistType } from '../../../shared/models/videos'
|
2020-12-08 14:30:29 +01:00
|
|
|
import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist'
|
2019-04-11 14:26:41 +02:00
|
|
|
import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
|
2023-01-10 11:09:30 +01:00
|
|
|
import { getBlacklistSort, searchAttribute, throwIfNotValid } from '../shared'
|
2019-05-21 12:09:58 +02:00
|
|
|
import { ThumbnailModel } from './thumbnail'
|
2020-12-08 14:30:29 +01:00
|
|
|
import { VideoModel } from './video'
|
|
|
|
import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel'
|
2017-05-22 20:58:25 +02:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
@Table({
|
|
|
|
tableName: 'videoBlacklist',
|
|
|
|
indexes: [
|
2017-04-26 21:22:10 +02:00
|
|
|
{
|
2017-12-12 17:53:50 +01:00
|
|
|
fields: [ 'videoId' ],
|
|
|
|
unique: true
|
2017-04-26 21:22:10 +02:00
|
|
|
}
|
2017-05-22 20:58:25 +02:00
|
|
|
]
|
2017-12-12 17:53:50 +01:00
|
|
|
})
|
2021-05-12 14:09:04 +02:00
|
|
|
export class VideoBlacklistModel extends Model<Partial<AttributesOnly<VideoBlacklistModel>>> {
|
2017-04-26 21:22:10 +02:00
|
|
|
|
2018-08-13 16:57:13 +02:00
|
|
|
@AllowNull(true)
|
2019-04-18 11:28:17 +02:00
|
|
|
@Is('VideoBlacklistReason', value => throwIfNotValid(value, isVideoBlacklistReasonValid, 'reason', true))
|
2018-08-13 16:57:13 +02:00
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_BLACKLIST.REASON.max))
|
|
|
|
reason: string
|
|
|
|
|
2019-01-10 15:39:51 +01:00
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
unfederated: boolean
|
|
|
|
|
2019-04-02 11:26:47 +02:00
|
|
|
@AllowNull(false)
|
|
|
|
@Default(null)
|
|
|
|
@Is('VideoBlacklistType', value => throwIfNotValid(value, isVideoBlacklistTypeValid, 'type'))
|
|
|
|
@Column
|
|
|
|
type: VideoBlacklistType
|
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
2017-04-26 21:22:10 +02:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
2017-04-26 21:22:10 +02:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
@ForeignKey(() => VideoModel)
|
|
|
|
@Column
|
|
|
|
videoId: number
|
2017-04-26 21:22:10 +02:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
@BelongsTo(() => VideoModel, {
|
2017-07-11 16:01:56 +02:00
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
2017-12-12 17:53:50 +01:00
|
|
|
onDelete: 'cascade'
|
2017-04-26 21:22:10 +02:00
|
|
|
})
|
2017-12-12 17:53:50 +01:00
|
|
|
Video: VideoModel
|
|
|
|
|
2020-04-19 14:11:40 +02:00
|
|
|
static listForApi (parameters: {
|
|
|
|
start: number
|
|
|
|
count: number
|
2023-01-10 11:09:30 +01:00
|
|
|
sort: string
|
2020-04-19 14:11:40 +02:00
|
|
|
search?: string
|
|
|
|
type?: VideoBlacklistType
|
|
|
|
}) {
|
|
|
|
const { start, count, sort, search, type } = parameters
|
|
|
|
|
2019-05-21 12:09:58 +02:00
|
|
|
function buildBaseQuery (): FindOptions {
|
|
|
|
return {
|
|
|
|
offset: start,
|
|
|
|
limit: count,
|
2023-01-10 11:09:30 +01:00
|
|
|
order: getBlacklistSort(sort)
|
2019-05-21 12:09:58 +02:00
|
|
|
}
|
2017-12-12 17:53:50 +01:00
|
|
|
}
|
2017-04-26 21:22:10 +02:00
|
|
|
|
2019-05-21 12:09:58 +02:00
|
|
|
const countQuery = buildBaseQuery()
|
|
|
|
|
|
|
|
const findQuery = buildBaseQuery()
|
|
|
|
findQuery.include = [
|
|
|
|
{
|
|
|
|
model: VideoModel,
|
|
|
|
required: true,
|
2020-04-20 11:20:12 +02:00
|
|
|
where: searchAttribute(search, 'name'),
|
2019-05-21 12:09:58 +02:00
|
|
|
include: [
|
|
|
|
{
|
2019-07-31 15:57:32 +02:00
|
|
|
model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
|
2019-05-21 12:09:58 +02:00
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: ThumbnailModel,
|
|
|
|
attributes: [ 'type', 'filename' ],
|
|
|
|
required: false
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2019-04-02 11:26:47 +02:00
|
|
|
if (type) {
|
2019-05-21 12:09:58 +02:00
|
|
|
countQuery.where = { type }
|
|
|
|
findQuery.where = { type }
|
2019-04-02 11:26:47 +02:00
|
|
|
}
|
|
|
|
|
2019-05-21 12:09:58 +02:00
|
|
|
return Promise.all([
|
|
|
|
VideoBlacklistModel.count(countQuery),
|
|
|
|
VideoBlacklistModel.findAll(findQuery)
|
|
|
|
]).then(([ count, rows ]) => {
|
|
|
|
return {
|
|
|
|
data: rows,
|
|
|
|
total: count
|
|
|
|
}
|
|
|
|
})
|
2017-04-26 21:22:10 +02:00
|
|
|
}
|
|
|
|
|
2020-12-08 14:30:29 +01:00
|
|
|
static loadByVideoId (id: number): Promise<MVideoBlacklist> {
|
2017-12-12 17:53:50 +01:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
videoId: id
|
|
|
|
}
|
2017-07-05 13:26:25 +02:00
|
|
|
}
|
2017-04-26 21:22:10 +02:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
return VideoBlacklistModel.findOne(query)
|
2017-04-26 21:22:10 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 19:05:31 +02:00
|
|
|
toFormattedJSON (this: MVideoBlacklistFormattable): VideoBlacklist {
|
2017-12-12 17:53:50 +01:00
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
createdAt: this.createdAt,
|
|
|
|
updatedAt: this.updatedAt,
|
2018-08-13 16:57:13 +02:00
|
|
|
reason: this.reason,
|
2019-01-10 15:39:51 +01:00
|
|
|
unfederated: this.unfederated,
|
2019-04-02 11:26:47 +02:00
|
|
|
type: this.type,
|
2018-08-13 16:57:13 +02:00
|
|
|
|
2019-04-02 11:26:47 +02:00
|
|
|
video: this.Video.toFormattedJSON()
|
2017-12-12 17:53:50 +01:00
|
|
|
}
|
|
|
|
}
|
2017-04-26 21:22:10 +02:00
|
|
|
}
|