X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-blacklist.ts;h=b4df6cd6a3707d15bc026bc962cd82c6e68f3441;hb=282e61e6c11f79e919c543871783fe1a00298d18;hp=d9fe9dfc964ed9dc861aa132e8a7a74d3371cbb0;hpb=97567dd81f508dd6295ac4d73d849aa2ce0a6549;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-blacklist.ts b/server/models/video/video-blacklist.ts index d9fe9dfc9..b4df6cd6a 100644 --- a/server/models/video/video-blacklist.ts +++ b/server/models/video/video-blacklist.ts @@ -1,11 +1,14 @@ import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' import { getSortOnModel, SortType, throwIfNotValid } from '../utils' import { VideoModel } from './video' -import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from './video-channel' +import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel' import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist' import { VideoBlacklist, VideoBlacklistType } from '../../../shared/models/videos' import { CONSTRAINTS_FIELDS } from '../../initializers/constants' import { FindOptions } from 'sequelize' +import { ThumbnailModel } from './thumbnail' +import * as Bluebird from 'bluebird' +import { MVideoBlacklist, MVideoBlacklistFormattable } from '@server/typings/models' @Table({ tableName: 'videoBlacklist', @@ -52,38 +55,53 @@ export class VideoBlacklistModel extends Model { Video: VideoModel static listForApi (start: number, count: number, sort: SortType, type?: VideoBlacklistType) { - const query: FindOptions = { - offset: start, - limit: count, - order: getSortOnModel(sort.sortModel, sort.sortValue), - include: [ - { - model: VideoModel, - required: true, - include: [ - { - model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, true ] }), - required: true - } - ] - } - ] + function buildBaseQuery (): FindOptions { + return { + offset: start, + limit: count, + order: getSortOnModel(sort.sortModel, sort.sortValue) + } } + const countQuery = buildBaseQuery() + + const findQuery = buildBaseQuery() + findQuery.subQuery = false + findQuery.include = [ + { + model: VideoModel, + required: true, + include: [ + { + model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }), + required: true + }, + { + model: ThumbnailModel, + attributes: [ 'type', 'filename' ], + required: false + } + ] + } + ] + if (type) { - query.where = { type } + countQuery.where = { type } + findQuery.where = { type } } - return VideoBlacklistModel.findAndCountAll(query) - .then(({ rows, count }) => { - return { - data: rows, - total: count - } - }) + return Promise.all([ + VideoBlacklistModel.count(countQuery), + VideoBlacklistModel.findAll(findQuery) + ]).then(([ count, rows ]) => { + return { + data: rows, + total: count + } + }) } - static loadByVideoId (id: number) { + static loadByVideoId (id: number): Bluebird { const query = { where: { videoId: id @@ -93,7 +111,7 @@ export class VideoBlacklistModel extends Model { return VideoBlacklistModel.findOne(query) } - toFormattedJSON (): VideoBlacklist { + toFormattedJSON (this: MVideoBlacklistFormattable): VideoBlacklist { return { id: this.id, createdAt: this.createdAt,