aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
diff options
context:
space:
mode:
Diffstat (limited to 'server/models')
-rw-r--r--server/models/video/video-blacklist.ts66
1 files changed, 41 insertions, 25 deletions
diff --git a/server/models/video/video-blacklist.ts b/server/models/video/video-blacklist.ts
index 3bd74460d..baef1d6ce 100644
--- a/server/models/video/video-blacklist.ts
+++ b/server/models/video/video-blacklist.ts
@@ -1,11 +1,12 @@
1import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' 1import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2import { getSortOnModel, SortType, throwIfNotValid } from '../utils' 2import { getSortOnModel, SortType, throwIfNotValid } from '../utils'
3import { VideoModel, ScopeNames as VideoModelScopeNames } from './video' 3import { ScopeNames as VideoModelScopeNames, VideoModel } from './video'
4import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from './video-channel' 4import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from './video-channel'
5import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist' 5import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist'
6import { VideoBlacklist, VideoBlacklistType } from '../../../shared/models/videos' 6import { VideoBlacklist, VideoBlacklistType } from '../../../shared/models/videos'
7import { CONSTRAINTS_FIELDS } from '../../initializers/constants' 7import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
8import { FindOptions } from 'sequelize' 8import { FindOptions } from 'sequelize'
9import { ThumbnailModel } from './thumbnail'
9 10
10@Table({ 11@Table({
11 tableName: 'videoBlacklist', 12 tableName: 'videoBlacklist',
@@ -52,35 +53,50 @@ export class VideoBlacklistModel extends Model<VideoBlacklistModel> {
52 Video: VideoModel 53 Video: VideoModel
53 54
54 static listForApi (start: number, count: number, sort: SortType, type?: VideoBlacklistType) { 55 static listForApi (start: number, count: number, sort: SortType, type?: VideoBlacklistType) {
55 const query: FindOptions = { 56 function buildBaseQuery (): FindOptions {
56 offset: start, 57 return {
57 limit: count, 58 offset: start,
58 order: getSortOnModel(sort.sortModel, sort.sortValue), 59 limit: count,
59 include: [ 60 order: getSortOnModel(sort.sortModel, sort.sortValue)
60 { 61 }
61 model: VideoModel.scope(VideoModelScopeNames.WITH_THUMBNAILS),
62 required: true,
63 include: [
64 {
65 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, true ] }),
66 required: true
67 }
68 ]
69 }
70 ]
71 } 62 }
72 63
64 const countQuery = buildBaseQuery()
65
66 const findQuery = buildBaseQuery()
67 findQuery.subQuery = false
68 findQuery.include = [
69 {
70 model: VideoModel,
71 required: true,
72 include: [
73 {
74 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, true ] }),
75 required: true
76 },
77 {
78 model: ThumbnailModel,
79 attributes: [ 'type', 'filename' ],
80 required: false
81 }
82 ]
83 }
84 ]
85
73 if (type) { 86 if (type) {
74 query.where = { type } 87 countQuery.where = { type }
88 findQuery.where = { type }
75 } 89 }
76 90
77 return VideoBlacklistModel.findAndCountAll(query) 91 return Promise.all([
78 .then(({ rows, count }) => { 92 VideoBlacklistModel.count(countQuery),
79 return { 93 VideoBlacklistModel.findAll(findQuery)
80 data: rows, 94 ]).then(([ count, rows ]) => {
81 total: count 95 return {
82 } 96 data: rows,
83 }) 97 total: count
98 }
99 })
84 } 100 }
85 101
86 static loadByVideoId (id: number) { 102 static loadByVideoId (id: number) {