1 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2 import { getBlacklistSort, SortType, throwIfNotValid, searchAttribute } 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 (parameters: {
62 type?: VideoBlacklistType
64 const { start, count, sort, search, type } = parameters
66 function buildBaseQuery (): FindOptions {
70 order: getBlacklistSort(sort.sortModel, sort.sortValue)
74 const countQuery = buildBaseQuery()
76 const findQuery = buildBaseQuery()
81 where: searchAttribute(search, 'name'),
84 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
88 model: ThumbnailModel,
89 attributes: [ 'type', 'filename' ],
97 countQuery.where = { type }
98 findQuery.where = { type }
102 VideoBlacklistModel.count(countQuery),
103 VideoBlacklistModel.findAll(findQuery)
104 ]).then(([ count, rows ]) => {
112 static loadByVideoId (id: number): Bluebird<MVideoBlacklist> {
119 return VideoBlacklistModel.findOne(query)
122 toFormattedJSON (this: MVideoBlacklistFormattable): VideoBlacklist {
125 createdAt: this.createdAt,
126 updatedAt: this.updatedAt,
128 unfederated: this.unfederated,
131 video: this.Video.toFormattedJSON()