]>
Commit | Line | Data |
---|---|---|
3caf77d3 | 1 | import { Model, Sequelize } from 'sequelize-typescript' |
7cde3b9c | 2 | import validator from 'validator' |
1735c825 | 3 | import { Col } from 'sequelize/types/lib/utils' |
e0a92917 | 4 | import { literal, OrderItem, Op } from 'sequelize' |
57c36b27 | 5 | |
8319d6ae RK |
6 | type Primitive = string | Function | number | boolean | Symbol | undefined | null |
7 | type DeepOmitHelper<T, K extends keyof T> = { | |
8 | [P in K]: // extra level of indirection needed to trigger homomorhic behavior | |
9 | T[P] extends infer TP // distribute over unions | |
10 | ? TP extends Primitive | |
11 | ? TP // leave primitives and functions alone | |
12 | : TP extends any[] | |
13 | ? DeepOmitArray<TP, K> // Array special handling | |
14 | : DeepOmit<TP, K> | |
15 | : never | |
16 | } | |
17 | type DeepOmit<T, K> = T extends Primitive ? T : DeepOmitHelper<T, Exclude<keyof T, K>> | |
18 | ||
19 | type DeepOmitArray<T extends any[], K> = { | |
20 | [P in keyof T]: DeepOmit<T[P], K> | |
21 | } | |
22 | ||
95153292 | 23 | type SortType = { sortModel: string, sortValue: string } |
06215f15 | 24 | |
9a629c6e | 25 | // Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ] |
1735c825 C |
26 | function getSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { |
27 | const { direction, field } = buildDirectionAndField(value) | |
28 | ||
29 | let finalField: string | Col | |
4b54f136 C |
30 | |
31 | if (field.toLowerCase() === 'match') { // Search | |
1735c825 | 32 | finalField = Sequelize.col('similarity') |
aa3796bd C |
33 | } else if (field === 'videoQuotaUsed') { // Users list |
34 | finalField = Sequelize.col('videoQuotaUsed') | |
1735c825 C |
35 | } else { |
36 | finalField = field | |
4b54f136 | 37 | } |
5c39adb7 | 38 | |
1735c825 | 39 | return [ [ finalField, direction ], lastSort ] |
9a629c6e C |
40 | } |
41 | ||
c1125bca RK |
42 | function getCommentSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { |
43 | const { direction, field } = buildDirectionAndField(value) | |
44 | ||
45 | if (field === 'totalReplies') { | |
46 | return [ | |
47 | [ Sequelize.literal('"totalReplies"'), direction ], | |
48 | lastSort | |
49 | ] | |
50 | } | |
51 | ||
52 | return getSort(value, lastSort) | |
53 | } | |
54 | ||
1735c825 C |
55 | function getVideoSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { |
56 | const { direction, field } = buildDirectionAndField(value) | |
5c39adb7 | 57 | |
1735c825 | 58 | if (field.toLowerCase() === 'trending') { // Sort by aggregation |
9a629c6e C |
59 | return [ |
60 | [ Sequelize.fn('COALESCE', Sequelize.fn('SUM', Sequelize.col('VideoViews.views')), '0'), direction ], | |
61 | ||
62 | [ Sequelize.col('VideoModel.views'), direction ], | |
63 | ||
64 | lastSort | |
65 | ] | |
66 | } | |
67 | ||
1735c825 C |
68 | let finalField: string | 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 = typeof finalField === 'string' | |
78 | ? finalField.split('.').concat([ direction ]) as any // FIXME: sequelize typings | |
79 | : [ finalField, direction ] | |
afa4374a C |
80 | |
81 | return [ firstSort, lastSort ] | |
5c39adb7 C |
82 | } |
83 | ||
95153292 | 84 | function getBlacklistSort (model: any, value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { |
1735c825 | 85 | const [ firstSort ] = getSort(value) |
792dbaf0 | 86 | |
a1587156 | 87 | if (model) return [ [ literal(`"${model}.${firstSort[0]}" ${firstSort[1]}`) ], lastSort ] as any[] // FIXME: typings |
3bb6c526 | 88 | return [ firstSort, lastSort ] |
792dbaf0 GS |
89 | } |
90 | ||
cb5ce4cb C |
91 | function getFollowsSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { |
92 | const { direction, field } = buildDirectionAndField(value) | |
93 | ||
94 | if (field === 'redundancyAllowed') { | |
95 | return [ | |
96 | [ 'ActorFollowing', 'Server', 'redundancyAllowed', direction ], | |
97 | lastSort | |
98 | ] | |
99 | } | |
100 | ||
101 | return getSort(value, lastSort) | |
102 | } | |
103 | ||
9f79ade6 C |
104 | function isOutdated (model: { createdAt: Date, updatedAt: Date }, refreshInterval: number) { |
105 | const now = Date.now() | |
106 | const createdAtTime = model.createdAt.getTime() | |
107 | const updatedAtTime = model.updatedAt.getTime() | |
108 | ||
109 | return (now - createdAtTime) > refreshInterval && (now - updatedAtTime) > refreshInterval | |
110 | } | |
111 | ||
1735c825 C |
112 | function throwIfNotValid (value: any, validator: (value: any) => boolean, fieldName = 'value', nullable = false) { |
113 | if (nullable && (value === null || value === undefined)) return | |
114 | ||
3fd3ab2d C |
115 | if (validator(value) === false) { |
116 | throw new Error(`"${value}" is not a valid ${fieldName}.`) | |
117 | } | |
118 | } | |
119 | ||
57c36b27 C |
120 | function buildTrigramSearchIndex (indexName: string, attribute: string) { |
121 | return { | |
122 | name: indexName, | |
123 | fields: [ Sequelize.literal('lower(immutable_unaccent(' + attribute + '))') as any ], | |
124 | using: 'gin', | |
125 | operator: 'gin_trgm_ops' | |
126 | } | |
127 | } | |
128 | ||
129 | function createSimilarityAttribute (col: string, value: string) { | |
130 | return Sequelize.fn( | |
131 | 'similarity', | |
132 | ||
133 | searchTrigramNormalizeCol(col), | |
134 | ||
135 | searchTrigramNormalizeValue(value) | |
136 | ) | |
137 | } | |
138 | ||
7ad9b984 C |
139 | function buildBlockedAccountSQL (serverAccountId: number, userAccountId?: number) { |
140 | const blockerIds = [ serverAccountId ] | |
141 | if (userAccountId) blockerIds.push(userAccountId) | |
142 | ||
143 | const blockerIdsString = blockerIds.join(', ') | |
144 | ||
418d092a | 145 | return 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' + |
7ad9b984 | 146 | ' UNION ALL ' + |
7ad9b984 C |
147 | 'SELECT "account"."id" AS "id" FROM account INNER JOIN "actor" ON account."actorId" = actor.id ' + |
148 | 'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' + | |
149 | 'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')' | |
418d092a C |
150 | } |
151 | ||
152 | function buildServerIdsFollowedBy (actorId: any) { | |
153 | const actorIdNumber = parseInt(actorId + '', 10) | |
154 | ||
155 | return '(' + | |
156 | 'SELECT "actor"."serverId" FROM "actorFollow" ' + | |
157 | 'INNER JOIN "actor" ON actor.id = "actorFollow"."targetActorId" ' + | |
158 | 'WHERE "actorFollow"."actorId" = ' + actorIdNumber + | |
a1587156 | 159 | ')' |
418d092a | 160 | } |
7ad9b984 | 161 | |
418d092a C |
162 | function buildWhereIdOrUUID (id: number | string) { |
163 | return validator.isInt('' + id) ? { id } : { uuid: id } | |
7ad9b984 C |
164 | } |
165 | ||
3acc5084 C |
166 | function parseAggregateResult (result: any) { |
167 | if (!result) return 0 | |
168 | ||
169 | const total = parseInt(result + '', 10) | |
170 | if (isNaN(total)) return 0 | |
171 | ||
172 | return total | |
173 | } | |
174 | ||
6b9c966f | 175 | const createSafeIn = (model: typeof Model, stringArr: (string | number)[]) => { |
5f3e2425 C |
176 | return stringArr.map(t => { |
177 | return t === null | |
178 | ? null | |
179 | : model.sequelize.escape('' + t) | |
180 | }).join(', ') | |
3caf77d3 C |
181 | } |
182 | ||
6b9c966f C |
183 | function buildLocalAccountIdsIn () { |
184 | return literal( | |
185 | '(SELECT "account"."id" FROM "account" INNER JOIN "actor" ON "actor"."id" = "account"."actorId" AND "actor"."serverId" IS NULL)' | |
186 | ) | |
187 | } | |
188 | ||
189 | function buildLocalActorIdsIn () { | |
190 | return literal( | |
191 | '(SELECT "actor"."id" FROM "actor" WHERE "actor"."serverId" IS NULL)' | |
192 | ) | |
193 | } | |
194 | ||
5f3e2425 C |
195 | function buildDirectionAndField (value: string) { |
196 | let field: string | |
197 | let direction: 'ASC' | 'DESC' | |
198 | ||
199 | if (value.substring(0, 1) === '-') { | |
200 | direction = 'DESC' | |
201 | field = value.substring(1) | |
202 | } else { | |
203 | direction = 'ASC' | |
204 | field = value | |
205 | } | |
206 | ||
207 | return { direction, field } | |
208 | } | |
209 | ||
e0a92917 | 210 | function searchAttribute (sourceField, targetField) { |
0251197e RK |
211 | if (sourceField) { |
212 | return { | |
e0a92917 RK |
213 | [targetField]: { |
214 | [Op.iLike]: `%${sourceField}%` | |
215 | } | |
216 | } | |
0251197e RK |
217 | } else { |
218 | return {} | |
219 | } | |
e0a92917 RK |
220 | } |
221 | ||
0d3a2982 RK |
222 | interface QueryStringFilterPrefixes { |
223 | [key: string]: string | { prefix: string, handler: Function, multiple?: boolean } | |
224 | } | |
225 | ||
25a42e29 RK |
226 | function parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes): { |
227 | search: string | |
228 | [key: string]: string | number | string[] | number[] | |
229 | } { | |
0d3a2982 | 230 | const tokens = q // tokenize only if we have a querystring |
25a42e29 | 231 | ? [].concat.apply([], q.split('"').map((v, i) => i % 2 ? v : v.split(' '))).filter(Boolean) // split by space unless using double quotes |
0d3a2982 RK |
232 | : [] |
233 | ||
234 | // TODO: when Typescript supports Object.fromEntries, replace with the Object method | |
235 | function fromEntries<T> (entries: [keyof T, T[keyof T]][]): T { | |
236 | return entries.reduce( | |
237 | (acc, [ key, value ]) => ({ ...acc, [key]: value }), | |
238 | {} as T | |
239 | ) | |
240 | } | |
241 | ||
242 | const objectMap = (obj, fn) => fromEntries( | |
243 | Object.entries(obj).map( | |
244 | ([ k, v ], i) => [ k, fn(v, k, i) ] | |
245 | ) | |
246 | ) | |
247 | ||
248 | return { | |
249 | // search is the querystring minus defined filters | |
250 | search: tokens.filter(e => !Object.values(prefixes).some(p => { | |
251 | if (typeof p === "string") { | |
252 | return e.startsWith(p) | |
253 | } else { | |
254 | return e.startsWith(p.prefix) | |
255 | } | |
256 | })).join(' '), | |
257 | // filters defined in prefixes are added under their own name | |
25a42e29 RK |
258 | ...objectMap(prefixes, p => { |
259 | if (typeof p === "string") { | |
260 | return tokens.filter(e => e.startsWith(p)).map(e => e.slice(p.length)) // we keep the matched item, and remove its prefix | |
0d3a2982 | 261 | } else { |
25a42e29 RK |
262 | const _tokens = tokens.filter(e => e.startsWith(p.prefix)).map(e => e.slice(p.prefix.length)).map(p.handler) |
263 | // multiple is false by default, meaning we usually just keep the first occurence of a given prefix | |
264 | if (!p.multiple && _tokens.length > 0) { | |
265 | return _tokens[0] | |
266 | } else if (!p.multiple) { | |
267 | return '' | |
268 | } | |
269 | return _tokens | |
0d3a2982 RK |
270 | } |
271 | }) | |
272 | } | |
273 | } | |
274 | ||
5c39adb7 C |
275 | // --------------------------------------------------------------------------- |
276 | ||
65fcc311 | 277 | export { |
8319d6ae | 278 | DeepOmit, |
7ad9b984 | 279 | buildBlockedAccountSQL, |
6b9c966f | 280 | buildLocalActorIdsIn, |
06215f15 | 281 | SortType, |
6b9c966f | 282 | buildLocalAccountIdsIn, |
792dbaf0 | 283 | getSort, |
c1125bca | 284 | getCommentSort, |
9a629c6e | 285 | getVideoSort, |
95153292 | 286 | getBlacklistSort, |
57c36b27 C |
287 | createSimilarityAttribute, |
288 | throwIfNotValid, | |
418d092a C |
289 | buildServerIdsFollowedBy, |
290 | buildTrigramSearchIndex, | |
9f79ade6 | 291 | buildWhereIdOrUUID, |
3acc5084 | 292 | isOutdated, |
3caf77d3 | 293 | parseAggregateResult, |
cb5ce4cb | 294 | getFollowsSort, |
5f3e2425 | 295 | buildDirectionAndField, |
e0a92917 | 296 | createSafeIn, |
0d3a2982 RK |
297 | searchAttribute, |
298 | parseQueryStringFilter | |
57c36b27 C |
299 | } |
300 | ||
301 | // --------------------------------------------------------------------------- | |
302 | ||
303 | function searchTrigramNormalizeValue (value: string) { | |
2cebd797 | 304 | return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', value)) |
57c36b27 C |
305 | } |
306 | ||
307 | function searchTrigramNormalizeCol (col: string) { | |
308 | return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', Sequelize.col(col))) | |
65fcc311 | 309 | } |