diff options
Diffstat (limited to 'server/models/utils.ts')
-rw-r--r-- | server/models/utils.ts | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/server/models/utils.ts b/server/models/utils.ts index eb6653f3d..edb8e1161 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts | |||
@@ -1,23 +1,31 @@ | |||
1 | // Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ] | ||
2 | import { Sequelize } from 'sequelize-typescript' | 1 | import { Sequelize } from 'sequelize-typescript' |
3 | 2 | ||
4 | type SortType = { sortModel: any, sortValue: string } | 3 | type SortType = { sortModel: any, sortValue: string } |
5 | 4 | ||
5 | // Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ] | ||
6 | function getSort (value: string, lastSort: string[] = [ 'id', 'ASC' ]) { | 6 | function getSort (value: string, lastSort: string[] = [ 'id', 'ASC' ]) { |
7 | let field: any | 7 | const { direction, field } = buildDirectionAndField(value) |
8 | let direction: 'ASC' | 'DESC' | ||
9 | 8 | ||
10 | if (value.substring(0, 1) === '-') { | 9 | return [ [ field, direction ], lastSort ] |
11 | direction = 'DESC' | 10 | } |
12 | field = value.substring(1) | 11 | |
13 | } else { | 12 | function getVideoSort (value: string, lastSort: string[] = [ 'id', 'ASC' ]) { |
14 | direction = 'ASC' | 13 | let { direction, field } = buildDirectionAndField(value) |
15 | field = value | ||
16 | } | ||
17 | 14 | ||
18 | // Alias | 15 | // Alias |
19 | if (field.toLowerCase() === 'match') field = Sequelize.col('similarity') | 16 | if (field.toLowerCase() === 'match') field = Sequelize.col('similarity') |
20 | 17 | ||
18 | // Sort by aggregation | ||
19 | if (field.toLowerCase() === 'trending') { | ||
20 | return [ | ||
21 | [ Sequelize.fn('COALESCE', Sequelize.fn('SUM', Sequelize.col('VideoViews.views')), '0'), direction ], | ||
22 | |||
23 | [ Sequelize.col('VideoModel.views'), direction ], | ||
24 | |||
25 | lastSort | ||
26 | ] | ||
27 | } | ||
28 | |||
21 | return [ [ field, direction ], lastSort ] | 29 | return [ [ field, direction ], lastSort ] |
22 | } | 30 | } |
23 | 31 | ||
@@ -58,6 +66,7 @@ function createSimilarityAttribute (col: string, value: string) { | |||
58 | export { | 66 | export { |
59 | SortType, | 67 | SortType, |
60 | getSort, | 68 | getSort, |
69 | getVideoSort, | ||
61 | getSortOnModel, | 70 | getSortOnModel, |
62 | createSimilarityAttribute, | 71 | createSimilarityAttribute, |
63 | throwIfNotValid, | 72 | throwIfNotValid, |
@@ -73,3 +82,18 @@ function searchTrigramNormalizeValue (value: string) { | |||
73 | function searchTrigramNormalizeCol (col: string) { | 82 | function searchTrigramNormalizeCol (col: string) { |
74 | return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', Sequelize.col(col))) | 83 | return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', Sequelize.col(col))) |
75 | } | 84 | } |
85 | |||
86 | function buildDirectionAndField (value: string) { | ||
87 | let field: any | ||
88 | let direction: 'ASC' | 'DESC' | ||
89 | |||
90 | if (value.substring(0, 1) === '-') { | ||
91 | direction = 'DESC' | ||
92 | field = value.substring(1) | ||
93 | } else { | ||
94 | direction = 'ASC' | ||
95 | field = value | ||
96 | } | ||
97 | |||
98 | return { direction, field } | ||
99 | } | ||