1 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2 import { getBlacklistSort, 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: getBlacklistSort(sort.sortModel, sort.sortValue)
66 const countQuery = buildBaseQuery()
68 const findQuery = buildBaseQuery()
75 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
79 model: ThumbnailModel,
80 attributes: [ 'type', 'filename' ],
88 countQuery.where = { type }
89 findQuery.where = { type }
93 VideoBlacklistModel.count(countQuery),
94 VideoBlacklistModel.findAll(findQuery)
95 ]).then(([ count, rows ]) => {
103 static loadByVideoId (id: number): Bluebird<MVideoBlacklist> {
110 return VideoBlacklistModel.findOne(query)
113 toFormattedJSON (this: MVideoBlacklistFormattable): VideoBlacklist {
116 createdAt: this.createdAt,
117 updatedAt: this.updatedAt,
119 unfederated: this.unfederated,
122 video: this.Video.toFormattedJSON()