]>
Commit | Line | Data |
---|---|---|
8c4bbd94 C |
1 | import { literal, OrderItem, Sequelize } from 'sequelize' |
2 | ||
3 | // Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ] | |
4 | function getSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { | |
5 | const { direction, field } = buildSortDirectionAndField(value) | |
6 | ||
7 | let finalField: string | ReturnType<typeof Sequelize.col> | |
8 | ||
9 | if (field.toLowerCase() === 'match') { // Search | |
10 | finalField = Sequelize.col('similarity') | |
11 | } else { | |
12 | finalField = field | |
13 | } | |
14 | ||
15 | return [ [ finalField, direction ], lastSort ] | |
16 | } | |
17 | ||
18 | function getAdminUsersSort (value: string): OrderItem[] { | |
19 | const { direction, field } = buildSortDirectionAndField(value) | |
20 | ||
21 | let finalField: string | ReturnType<typeof Sequelize.col> | |
22 | ||
23 | if (field === 'videoQuotaUsed') { // Users list | |
24 | finalField = Sequelize.col('videoQuotaUsed') | |
25 | } else { | |
26 | finalField = field | |
27 | } | |
28 | ||
29 | const nullPolicy = direction === 'ASC' | |
30 | ? 'NULLS FIRST' | |
31 | : 'NULLS LAST' | |
32 | ||
33 | // FIXME: typings | |
34 | return [ [ finalField as any, direction, nullPolicy ], [ 'id', 'ASC' ] ] | |
35 | } | |
36 | ||
37 | function getPlaylistSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { | |
38 | const { direction, field } = buildSortDirectionAndField(value) | |
39 | ||
40 | if (field.toLowerCase() === 'name') { | |
41 | return [ [ 'displayName', direction ], lastSort ] | |
42 | } | |
43 | ||
44 | return getSort(value, lastSort) | |
45 | } | |
46 | ||
8c4bbd94 C |
47 | function getVideoSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { |
48 | const { direction, field } = buildSortDirectionAndField(value) | |
49 | ||
50 | if (field.toLowerCase() === 'trending') { // Sort by aggregation | |
51 | return [ | |
52 | [ Sequelize.fn('COALESCE', Sequelize.fn('SUM', Sequelize.col('VideoViews.views')), '0'), direction ], | |
53 | ||
54 | [ Sequelize.col('VideoModel.views'), direction ], | |
55 | ||
56 | lastSort | |
57 | ] | |
58 | } else if (field === 'publishedAt') { | |
59 | return [ | |
60 | [ 'ScheduleVideoUpdate', 'updateAt', direction + ' NULLS LAST' ], | |
61 | ||
62 | [ Sequelize.col('VideoModel.publishedAt'), direction ], | |
63 | ||
64 | lastSort | |
65 | ] | |
66 | } | |
67 | ||
68 | let finalField: string | ReturnType<typeof Sequelize.col> | |
69 | ||
70 | // Alias | |
71 | if (field.toLowerCase() === 'match') { // Search | |
72 | finalField = Sequelize.col('similarity') | |
73 | } else { | |
74 | finalField = field | |
75 | } | |
76 | ||
77 | const firstSort: OrderItem = typeof finalField === 'string' | |
78 | ? finalField.split('.').concat([ direction ]) as OrderItem | |
79 | : [ finalField, direction ] | |
80 | ||
81 | return [ firstSort, lastSort ] | |
82 | } | |
83 | ||
84 | function getBlacklistSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { | |
85 | const { direction, field } = buildSortDirectionAndField(value) | |
86 | ||
87 | const videoFields = new Set([ 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid' ]) | |
88 | ||
89 | if (videoFields.has(field)) { | |
90 | return [ | |
91 | [ literal(`"Video.${field}" ${direction}`) ], | |
92 | lastSort | |
93 | ] as OrderItem[] | |
94 | } | |
95 | ||
96 | return getSort(value, lastSort) | |
97 | } | |
98 | ||
99 | function getInstanceFollowsSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { | |
100 | const { direction, field } = buildSortDirectionAndField(value) | |
101 | ||
102 | if (field === 'redundancyAllowed') { | |
103 | return [ | |
104 | [ 'ActorFollowing.Server.redundancyAllowed', direction ], | |
105 | lastSort | |
106 | ] | |
107 | } | |
108 | ||
109 | return getSort(value, lastSort) | |
110 | } | |
111 | ||
112 | function getChannelSyncSort (value: string): OrderItem[] { | |
113 | const { direction, field } = buildSortDirectionAndField(value) | |
114 | if (field.toLowerCase() === 'videochannel') { | |
115 | return [ | |
116 | [ literal('"VideoChannel.name"'), direction ] | |
117 | ] | |
118 | } | |
119 | return [ [ field, direction ] ] | |
120 | } | |
121 | ||
122 | function buildSortDirectionAndField (value: string) { | |
123 | let field: string | |
124 | let direction: 'ASC' | 'DESC' | |
125 | ||
126 | if (value.substring(0, 1) === '-') { | |
127 | direction = 'DESC' | |
128 | field = value.substring(1) | |
129 | } else { | |
130 | direction = 'ASC' | |
131 | field = value | |
132 | } | |
133 | ||
134 | return { direction, field } | |
135 | } | |
136 | ||
137 | export { | |
138 | buildSortDirectionAndField, | |
139 | getPlaylistSort, | |
140 | getSort, | |
8c4bbd94 C |
141 | getAdminUsersSort, |
142 | getVideoSort, | |
143 | getBlacklistSort, | |
144 | getChannelSyncSort, | |
145 | getInstanceFollowsSort | |
146 | } |