aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/utils.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-08-31 17:18:13 +0200
committerChocobozzz <me@florianbigard.com>2018-08-31 17:22:01 +0200
commit9a629c6efbe39dfac290347670ca41b0d7100f41 (patch)
tree22c491983071aebe928fe73a2c51a8347fc78b8a /server/models/utils.ts
parent4ccb6c083025005a495e8ed40ea352c541f66951 (diff)
downloadPeerTube-9a629c6efbe39dfac290347670ca41b0d7100f41.tar.gz
PeerTube-9a629c6efbe39dfac290347670ca41b0d7100f41.tar.zst
PeerTube-9a629c6efbe39dfac290347670ca41b0d7100f41.zip
Trending by interval
Diffstat (limited to 'server/models/utils.ts')
-rw-r--r--server/models/utils.ts44
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' ] ]
2import { Sequelize } from 'sequelize-typescript' 1import { Sequelize } from 'sequelize-typescript'
3 2
4type SortType = { sortModel: any, sortValue: string } 3type SortType = { sortModel: any, sortValue: string }
5 4
5// Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ]
6function getSort (value: string, lastSort: string[] = [ 'id', 'ASC' ]) { 6function 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 { 12function 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) {
58export { 66export {
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) {
73function searchTrigramNormalizeCol (col: string) { 82function 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
86function 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}