1 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2 import { getSortOnModel, SortType, throwIfNotValid } from '../utils'
3 import { ScopeNames as VideoModelScopeNames, VideoModel } from './video'
4 import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from './video-channel'
5 import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist'
6 import { VideoBlacklist, VideoBlacklistType } from '../../../shared/models/videos'
7 import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
8 import { FindOptions } from 'sequelize'
9 import { ThumbnailModel } from './thumbnail'
12 tableName: 'videoBlacklist',
15 fields: [ 'videoId' ],
20 export class VideoBlacklistModel extends Model<VideoBlacklistModel> {
23 @Is('VideoBlacklistReason', value => throwIfNotValid(value, isVideoBlacklistReasonValid, 'reason', true))
24 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_BLACKLIST.REASON.max))
33 @Is('VideoBlacklistType', value => throwIfNotValid(value, isVideoBlacklistTypeValid, 'type'))
35 type: VideoBlacklistType
43 @ForeignKey(() => VideoModel)
47 @BelongsTo(() => VideoModel, {
55 static listForApi (start: number, count: number, sort: SortType, type?: VideoBlacklistType) {
56 function buildBaseQuery (): FindOptions {
60 order: getSortOnModel(sort.sortModel, sort.sortValue)
64 const countQuery = buildBaseQuery()
66 const findQuery = buildBaseQuery()
67 findQuery.subQuery = false
74 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, true ] }),
78 model: ThumbnailModel,
79 attributes: [ 'type', 'filename' ],
87 countQuery.where = { type }
88 findQuery.where = { type }
92 VideoBlacklistModel.count(countQuery),
93 VideoBlacklistModel.findAll(findQuery)
94 ]).then(([ count, rows ]) => {
102 static loadByVideoId (id: number) {
109 return VideoBlacklistModel.findOne(query)
112 toFormattedJSON (): VideoBlacklist {
115 createdAt: this.createdAt,
116 updatedAt: this.updatedAt,
118 unfederated: this.unfederated,
121 video: this.Video.toFormattedJSON()