X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-blacklist.ts;h=9247d0e2b1fd5fff6964382b44879768d2eb4755;hb=2f63f629add5d24f8c01f309c7cae43b667b0c2a;hp=4d1b45aa57d91a7626055a76db6f8557a9403501;hpb=0a6658fdcbd779ada8f3758048c326e997902d5a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-blacklist.ts b/server/models/video/video-blacklist.ts index 4d1b45aa5..9247d0e2b 100644 --- a/server/models/video/video-blacklist.ts +++ b/server/models/video/video-blacklist.ts @@ -1,106 +1,134 @@ -import * as Sequelize from 'sequelize' - -import { addMethodsToModel, getSort } from '../utils' -import { - BlacklistedVideoInstance, - BlacklistedVideoAttributes, - - BlacklistedVideoMethods -} from './video-blacklist-interface' - -let BlacklistedVideo: Sequelize.Model -let toFormatedJSON: BlacklistedVideoMethods.ToFormatedJSON -let countTotal: BlacklistedVideoMethods.CountTotal -let list: BlacklistedVideoMethods.List -let listForApi: BlacklistedVideoMethods.ListForApi -let loadById: BlacklistedVideoMethods.LoadById -let loadByVideoId: BlacklistedVideoMethods.LoadByVideoId - -export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { - BlacklistedVideo = sequelize.define('BlacklistedVideo', - {}, +import { FindOptions } from 'sequelize' +import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' +import { MVideoBlacklist, MVideoBlacklistFormattable } from '@server/types/models' +import { AttributesOnly } from '@shared/typescript-utils' +import { VideoBlacklist, VideoBlacklistType } from '../../../shared/models/videos' +import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist' +import { CONSTRAINTS_FIELDS } from '../../initializers/constants' +import { getBlacklistSort, searchAttribute, throwIfNotValid } from '../shared' +import { ThumbnailModel } from './thumbnail' +import { VideoModel } from './video' +import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel' + +@Table({ + tableName: 'videoBlacklist', + indexes: [ { - indexes: [ - { - fields: [ 'videoId' ], - unique: true - } - ] + fields: [ 'videoId' ], + unique: true } - ) + ] +}) +export class VideoBlacklistModel extends Model>> { - const classMethods = [ - associate, + @AllowNull(true) + @Is('VideoBlacklistReason', value => throwIfNotValid(value, isVideoBlacklistReasonValid, 'reason', true)) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_BLACKLIST.REASON.max)) + reason: string - countTotal, - list, - listForApi, - loadById, - loadByVideoId - ] - const instanceMethods = [ - toFormatedJSON - ] - addMethodsToModel(BlacklistedVideo, classMethods, instanceMethods) + @AllowNull(false) + @Column + unfederated: boolean - return BlacklistedVideo -} + @AllowNull(false) + @Default(null) + @Is('VideoBlacklistType', value => throwIfNotValid(value, isVideoBlacklistTypeValid, 'type')) + @Column + type: VideoBlacklistType -// ------------------------------ METHODS ------------------------------ + @CreatedAt + createdAt: Date -toFormatedJSON = function (this: BlacklistedVideoInstance) { - return { - id: this.id, - videoId: this.videoId, - createdAt: this.createdAt - } -} + @UpdatedAt + updatedAt: Date -// ------------------------------ STATICS ------------------------------ + @ForeignKey(() => VideoModel) + @Column + videoId: number -function associate (models) { - BlacklistedVideo.belongsTo(models.Video, { + @BelongsTo(() => VideoModel, { foreignKey: { - name: 'videoId', allowNull: false }, - onDelete: 'CASCADE' + onDelete: 'cascade' }) -} - -countTotal = function () { - return BlacklistedVideo.count() -} + Video: VideoModel + + static listForApi (parameters: { + start: number + count: number + sort: string + search?: string + type?: VideoBlacklistType + }) { + const { start, count, sort, search, type } = parameters + + function buildBaseQuery (): FindOptions { + return { + offset: start, + limit: count, + order: getBlacklistSort(sort) + } + } -list = function () { - return BlacklistedVideo.findAll() -} + const countQuery = buildBaseQuery() + + const findQuery = buildBaseQuery() + findQuery.include = [ + { + model: VideoModel, + required: true, + where: searchAttribute(search, 'name'), + include: [ + { + model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }), + required: true + }, + { + model: ThumbnailModel, + attributes: [ 'type', 'filename' ], + required: false + } + ] + } + ] + + if (type) { + countQuery.where = { type } + findQuery.where = { type } + } -listForApi = function (start: number, count: number, sort: string) { - const query = { - offset: start, - limit: count, - order: [ getSort(sort) ] + return Promise.all([ + VideoBlacklistModel.count(countQuery), + VideoBlacklistModel.findAll(findQuery) + ]).then(([ count, rows ]) => { + return { + data: rows, + total: count + } + }) } - return BlacklistedVideo.findAndCountAll(query).then(({ rows, count }) => { - return { - data: rows, - total: count + static loadByVideoId (id: number): Promise { + const query = { + where: { + videoId: id + } } - }) -} -loadById = function (id: number) { - return BlacklistedVideo.findById(id) -} + return VideoBlacklistModel.findOne(query) + } -loadByVideoId = function (id: number) { - const query = { - where: { - videoId: id + toFormattedJSON (this: MVideoBlacklistFormattable): VideoBlacklist { + return { + id: this.id, + createdAt: this.createdAt, + updatedAt: this.updatedAt, + reason: this.reason, + unfederated: this.unfederated, + type: this.type, + + video: this.Video.toFormattedJSON() } } - - return BlacklistedVideo.findOne(query) }