X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-blacklist.ts;h=1cd8224c0a97a8b0dbf63b6a105f4cec2014c219;hb=ba2684ceddf9b76312635b9cddc6bf6975ce436a;hp=ae8286285cd1bb37d1a29591625fc27834f7c8eb;hpb=39445ead45aaaea801ec09991b8dd2464f722e47;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-blacklist.ts b/server/models/video/video-blacklist.ts index ae8286285..1cd8224c0 100644 --- a/server/models/video/video-blacklist.ts +++ b/server/models/video/video-blacklist.ts @@ -1,104 +1,134 @@ -import * as Sequelize from 'sequelize' - -import { SortType } from '../../helpers' -import { addMethodsToModel, getSortOnModel } from '../utils' -import { VideoInstance } from './video-interface' -import { - BlacklistedVideoInstance, - BlacklistedVideoAttributes, - - BlacklistedVideoMethods -} from './video-blacklist-interface' - -let BlacklistedVideo: Sequelize.Model -let toFormattedJSON: BlacklistedVideoMethods.ToFormattedJSON -let listForApi: BlacklistedVideoMethods.ListForApi -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, SortType, throwIfNotValid } from '../utils' +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 - listForApi, - loadByVideoId - ] - const instanceMethods = [ - toFormattedJSON - ] - 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 ------------------------------ - -toFormattedJSON = function (this: BlacklistedVideoInstance) { - let video: VideoInstance - - video = this.Video - - return { - id: this.id, - videoId: this.videoId, - createdAt: this.createdAt, - updatedAt: this.updatedAt, - name: video.name, - uuid: video.uuid, - description: video.description, - duration: video.duration, - views: video.views, - likes: video.likes, - dislikes: video.dislikes, - nsfw: video.nsfw - } -} + @CreatedAt + createdAt: Date + + @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' }) -} + Video: VideoModel + + static listForApi (parameters: { + start: number + count: number + sort: SortType + search?: string + type?: VideoBlacklistType + }) { + const { start, count, sort, search, type } = parameters + + function buildBaseQuery (): FindOptions { + return { + offset: start, + limit: count, + order: getBlacklistSort(sort.sortModel, sort.sortValue) + } + } -listForApi = function (start: number, count: number, sort: SortType) { - const query = { - offset: start, - limit: count, - order: [ getSortOnModel(sort.sortModel, sort.sortValue) ], - include: [ { model: BlacklistedVideo['sequelize'].models.Video } ] + 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 } + } + + 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 + } } - }) -} -loadByVideoId = function (id: number) { - const query = { - where: { - videoId: id - } + return VideoBlacklistModel.findOne(query) } - return BlacklistedVideo.findOne(query) + 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() + } + } }