1 import { FindOptions } from 'sequelize'
2 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
3 import { MVideoBlacklist, MVideoBlacklistFormattable } from '@server/types/models'
4 import { VideoBlacklist, VideoBlacklistType } from '../../../shared/models/videos'
5 import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist'
6 import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
7 import { getBlacklistSort, searchAttribute, SortType, throwIfNotValid } from '../utils'
8 import { ThumbnailModel } from './thumbnail'
9 import { VideoModel } from './video'
10 import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel'
13 tableName: 'videoBlacklist',
16 fields: [ 'videoId' ],
21 export class VideoBlacklistModel extends Model {
24 @Is('VideoBlacklistReason', value => throwIfNotValid(value, isVideoBlacklistReasonValid, 'reason', true))
25 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_BLACKLIST.REASON.max))
34 @Is('VideoBlacklistType', value => throwIfNotValid(value, isVideoBlacklistTypeValid, 'type'))
36 type: VideoBlacklistType
44 @ForeignKey(() => VideoModel)
48 @BelongsTo(() => VideoModel, {
56 static listForApi (parameters: {
61 type?: VideoBlacklistType
63 const { start, count, sort, search, type } = parameters
65 function buildBaseQuery (): FindOptions {
69 order: getBlacklistSort(sort.sortModel, sort.sortValue)
73 const countQuery = buildBaseQuery()
75 const findQuery = buildBaseQuery()
80 where: searchAttribute(search, 'name'),
83 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
87 model: ThumbnailModel,
88 attributes: [ 'type', 'filename' ],
96 countQuery.where = { type }
97 findQuery.where = { type }
101 VideoBlacklistModel.count(countQuery),
102 VideoBlacklistModel.findAll(findQuery)
103 ]).then(([ count, rows ]) => {
111 static loadByVideoId (id: number): Promise<MVideoBlacklist> {
118 return VideoBlacklistModel.findOne(query)
121 toFormattedJSON (this: MVideoBlacklistFormattable): VideoBlacklist {
124 createdAt: this.createdAt,
125 updatedAt: this.updatedAt,
127 unfederated: this.unfederated,
130 video: this.Video.toFormattedJSON()