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 { AttributesOnly } from '@shared/typescript-utils'
5 import { VideoBlacklist, VideoBlacklistType } from '../../../shared/models/videos'
6 import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist'
7 import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
8 import { getBlacklistSort, searchAttribute, SortType, throwIfNotValid } from '../utils'
9 import { ThumbnailModel } from './thumbnail'
10 import { VideoModel } from './video'
11 import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel'
14 tableName: 'videoBlacklist',
17 fields: [ 'videoId' ],
22 export class VideoBlacklistModel extends Model<Partial<AttributesOnly<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): Promise<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()