diff options
author | Chocobozzz <me@florianbigard.com> | 2019-05-21 12:09:58 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-05-21 12:09:58 +0200 |
commit | ac3d2e05695883dabb435868272709b955dcb77a (patch) | |
tree | d9f6a8d1c4d972f2dd2f607b5e0e49400eab69b1 | |
parent | b9dde7d96b28a8004a2698f465447f0f9b6b6dbb (diff) | |
download | PeerTube-ac3d2e05695883dabb435868272709b955dcb77a.tar.gz PeerTube-ac3d2e05695883dabb435868272709b955dcb77a.tar.zst PeerTube-ac3d2e05695883dabb435868272709b955dcb77a.zip |
Fix blacklist sort
-rw-r--r-- | server/models/video/video-blacklist.ts | 66 |
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 @@ | |||
1 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' | 1 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' |
2 | import { getSortOnModel, SortType, throwIfNotValid } from '../utils' | 2 | import { getSortOnModel, SortType, throwIfNotValid } from '../utils' |
3 | import { VideoModel, ScopeNames as VideoModelScopeNames } from './video' | 3 | import { ScopeNames as VideoModelScopeNames, VideoModel } from './video' |
4 | import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from './video-channel' | 4 | import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from './video-channel' |
5 | import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist' | 5 | import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist' |
6 | import { VideoBlacklist, VideoBlacklistType } from '../../../shared/models/videos' | 6 | import { VideoBlacklist, VideoBlacklistType } from '../../../shared/models/videos' |
7 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' | 7 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' |
8 | import { FindOptions } from 'sequelize' | 8 | import { FindOptions } from 'sequelize' |
9 | import { 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) { |