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 { 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'
10 import * as Bluebird from 'bluebird'
11 import { MVideoBlacklist, MVideoBlacklistFormattable } from '@server/typings/models'
14 tableName: 'videoBlacklist',
17 fields: [ 'videoId' ],
22 export class VideoBlacklistModel extends Model<VideoBlacklistModel> {
25 @Is('VideoBlacklistReason', value => throwIfNotValid(value, isVideoBlacklistReasonValid, 'reason', true))
26 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_BLACKLIST.REASON.max))
35 @Is('VideoBlacklistType', value => throwIfNotValid(value, isVideoBlacklistTypeValid, 'type'))
37 type: VideoBlacklistType
45 @ForeignKey(() => VideoModel)
49 @BelongsTo(() => VideoModel, {
57 static listForApi (start: number, count: number, sort: SortType, type?: VideoBlacklistType) {
58 function buildBaseQuery (): FindOptions {
62 order: getSortOnModel(sort.sortModel, sort.sortValue)
66 const countQuery = buildBaseQuery()
68 const findQuery = buildBaseQuery()
69 findQuery.subQuery = false
76 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
80 model: ThumbnailModel,
81 attributes: [ 'type', 'filename' ],
89 countQuery.where = { type }
90 findQuery.where = { type }
94 VideoBlacklistModel.count(countQuery),
95 VideoBlacklistModel.findAll(findQuery)
96 ]).then(([ count, rows ]) => {
104 static loadByVideoId (id: number): Bluebird<MVideoBlacklist> {
111 return VideoBlacklistModel.findOne(query)
114 toFormattedJSON (this: MVideoBlacklistFormattable): VideoBlacklist {
117 createdAt: this.createdAt,
118 updatedAt: this.updatedAt,
120 unfederated: this.unfederated,
123 video: this.Video.toFormattedJSON()