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, SummaryOptions, 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()
73 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
77 model: ThumbnailModel,
78 attributes: [ 'type', 'filename' ],
86 countQuery.where = { type }
87 findQuery.where = { type }
91 VideoBlacklistModel.count(countQuery),
92 VideoBlacklistModel.findAll(findQuery)
93 ]).then(([ count, rows ]) => {
101 static loadByVideoId (id: number) {
108 return VideoBlacklistModel.findOne(query)
111 toFormattedJSON (): VideoBlacklist {
114 createdAt: this.createdAt,
115 updatedAt: this.updatedAt,
117 unfederated: this.unfederated,
120 video: this.Video.toFormattedJSON()