]>
Commit | Line | Data |
---|---|---|
57c36b27 C |
1 | import { Sequelize } from 'sequelize-typescript' |
2 | ||
06215f15 C |
3 | type SortType = { sortModel: any, sortValue: string } |
4 | ||
9a629c6e | 5 | // Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ] |
3bb6c526 | 6 | function getSort (value: string, lastSort: string[] = [ 'id', 'ASC' ]) { |
4b54f136 C |
7 | let { direction, field } = buildDirectionAndField(value) |
8 | ||
9 | if (field.toLowerCase() === 'match') { // Search | |
10 | field = Sequelize.col('similarity') | |
11 | } | |
5c39adb7 | 12 | |
9a629c6e C |
13 | return [ [ field, direction ], lastSort ] |
14 | } | |
15 | ||
16 | function getVideoSort (value: string, lastSort: string[] = [ 'id', 'ASC' ]) { | |
17 | let { direction, field } = buildDirectionAndField(value) | |
5c39adb7 | 18 | |
57c36b27 | 19 | // Alias |
4b54f136 C |
20 | if (field.toLowerCase() === 'match') { // Search |
21 | field = Sequelize.col('similarity') | |
22 | } else if (field.toLowerCase() === 'trending') { // Sort by aggregation | |
9a629c6e C |
23 | return [ |
24 | [ Sequelize.fn('COALESCE', Sequelize.fn('SUM', Sequelize.col('VideoViews.views')), '0'), direction ], | |
25 | ||
26 | [ Sequelize.col('VideoModel.views'), direction ], | |
27 | ||
28 | lastSort | |
29 | ] | |
30 | } | |
31 | ||
afa4374a C |
32 | const firstSort = typeof field === 'string' ? |
33 | field.split('.').concat([ direction ]) : | |
34 | [ field, direction ] | |
35 | ||
36 | return [ firstSort, lastSort ] | |
5c39adb7 C |
37 | } |
38 | ||
3bb6c526 C |
39 | function getSortOnModel (model: any, value: string, lastSort: string[] = [ 'id', 'ASC' ]) { |
40 | let [ firstSort ] = getSort(value) | |
792dbaf0 | 41 | |
3bb6c526 C |
42 | if (model) return [ [ model, firstSort[0], firstSort[1] ], lastSort ] |
43 | return [ firstSort, lastSort ] | |
792dbaf0 GS |
44 | } |
45 | ||
3fd3ab2d C |
46 | function throwIfNotValid (value: any, validator: (value: any) => boolean, fieldName = 'value') { |
47 | if (validator(value) === false) { | |
48 | throw new Error(`"${value}" is not a valid ${fieldName}.`) | |
49 | } | |
50 | } | |
51 | ||
57c36b27 C |
52 | function buildTrigramSearchIndex (indexName: string, attribute: string) { |
53 | return { | |
54 | name: indexName, | |
55 | fields: [ Sequelize.literal('lower(immutable_unaccent(' + attribute + '))') as any ], | |
56 | using: 'gin', | |
57 | operator: 'gin_trgm_ops' | |
58 | } | |
59 | } | |
60 | ||
61 | function createSimilarityAttribute (col: string, value: string) { | |
62 | return Sequelize.fn( | |
63 | 'similarity', | |
64 | ||
65 | searchTrigramNormalizeCol(col), | |
66 | ||
67 | searchTrigramNormalizeValue(value) | |
68 | ) | |
69 | } | |
70 | ||
7ad9b984 C |
71 | function buildBlockedAccountSQL (serverAccountId: number, userAccountId?: number) { |
72 | const blockerIds = [ serverAccountId ] | |
73 | if (userAccountId) blockerIds.push(userAccountId) | |
74 | ||
75 | const blockerIdsString = blockerIds.join(', ') | |
76 | ||
77 | const query = 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' + | |
78 | ' UNION ALL ' + | |
7ad9b984 C |
79 | 'SELECT "account"."id" AS "id" FROM account INNER JOIN "actor" ON account."actorId" = actor.id ' + |
80 | 'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' + | |
81 | 'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')' | |
82 | ||
83 | return query | |
84 | } | |
85 | ||
5c39adb7 C |
86 | // --------------------------------------------------------------------------- |
87 | ||
65fcc311 | 88 | export { |
7ad9b984 | 89 | buildBlockedAccountSQL, |
06215f15 | 90 | SortType, |
792dbaf0 | 91 | getSort, |
9a629c6e | 92 | getVideoSort, |
3fd3ab2d | 93 | getSortOnModel, |
57c36b27 C |
94 | createSimilarityAttribute, |
95 | throwIfNotValid, | |
dbfd3e9b | 96 | buildTrigramSearchIndex |
57c36b27 C |
97 | } |
98 | ||
99 | // --------------------------------------------------------------------------- | |
100 | ||
101 | function searchTrigramNormalizeValue (value: string) { | |
2cebd797 | 102 | return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', value)) |
57c36b27 C |
103 | } |
104 | ||
105 | function searchTrigramNormalizeCol (col: string) { | |
106 | return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', Sequelize.col(col))) | |
65fcc311 | 107 | } |
9a629c6e C |
108 | |
109 | function buildDirectionAndField (value: string) { | |
110 | let field: any | |
111 | let direction: 'ASC' | 'DESC' | |
112 | ||
113 | if (value.substring(0, 1) === '-') { | |
114 | direction = 'DESC' | |
115 | field = value.substring(1) | |
116 | } else { | |
117 | direction = 'ASC' | |
118 | field = value | |
119 | } | |
120 | ||
121 | return { direction, field } | |
122 | } |