]>
Commit | Line | Data |
---|---|---|
3caf77d3 | 1 | import { Model, Sequelize } from 'sequelize-typescript' |
418d092a | 2 | import * as validator from 'validator' |
1735c825 | 3 | import { Col } from 'sequelize/types/lib/utils' |
3caf77d3 | 4 | import { OrderItem } from 'sequelize/types' |
57c36b27 | 5 | |
06215f15 C |
6 | type SortType = { sortModel: any, sortValue: string } |
7 | ||
9a629c6e | 8 | // Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ] |
1735c825 C |
9 | function getSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { |
10 | const { direction, field } = buildDirectionAndField(value) | |
11 | ||
12 | let finalField: string | Col | |
4b54f136 C |
13 | |
14 | if (field.toLowerCase() === 'match') { // Search | |
1735c825 C |
15 | finalField = Sequelize.col('similarity') |
16 | } else { | |
17 | finalField = field | |
4b54f136 | 18 | } |
5c39adb7 | 19 | |
1735c825 | 20 | return [ [ finalField, direction ], lastSort ] |
9a629c6e C |
21 | } |
22 | ||
1735c825 C |
23 | function getVideoSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { |
24 | const { direction, field } = buildDirectionAndField(value) | |
5c39adb7 | 25 | |
1735c825 | 26 | if (field.toLowerCase() === 'trending') { // Sort by aggregation |
9a629c6e C |
27 | return [ |
28 | [ Sequelize.fn('COALESCE', Sequelize.fn('SUM', Sequelize.col('VideoViews.views')), '0'), direction ], | |
29 | ||
30 | [ Sequelize.col('VideoModel.views'), direction ], | |
31 | ||
32 | lastSort | |
33 | ] | |
34 | } | |
35 | ||
1735c825 C |
36 | let finalField: string | Col |
37 | ||
38 | // Alias | |
39 | if (field.toLowerCase() === 'match') { // Search | |
40 | finalField = Sequelize.col('similarity') | |
41 | } else { | |
42 | finalField = field | |
43 | } | |
44 | ||
45 | const firstSort = typeof finalField === 'string' | |
46 | ? finalField.split('.').concat([ direction ]) as any // FIXME: sequelize typings | |
47 | : [ finalField, direction ] | |
afa4374a C |
48 | |
49 | return [ firstSort, lastSort ] | |
5c39adb7 C |
50 | } |
51 | ||
1735c825 C |
52 | function getSortOnModel (model: any, value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { |
53 | const [ firstSort ] = getSort(value) | |
792dbaf0 | 54 | |
3bb6c526 C |
55 | if (model) return [ [ model, firstSort[0], firstSort[1] ], lastSort ] |
56 | return [ firstSort, lastSort ] | |
792dbaf0 GS |
57 | } |
58 | ||
9f79ade6 C |
59 | function isOutdated (model: { createdAt: Date, updatedAt: Date }, refreshInterval: number) { |
60 | const now = Date.now() | |
61 | const createdAtTime = model.createdAt.getTime() | |
62 | const updatedAtTime = model.updatedAt.getTime() | |
63 | ||
64 | return (now - createdAtTime) > refreshInterval && (now - updatedAtTime) > refreshInterval | |
65 | } | |
66 | ||
1735c825 C |
67 | function throwIfNotValid (value: any, validator: (value: any) => boolean, fieldName = 'value', nullable = false) { |
68 | if (nullable && (value === null || value === undefined)) return | |
69 | ||
3fd3ab2d C |
70 | if (validator(value) === false) { |
71 | throw new Error(`"${value}" is not a valid ${fieldName}.`) | |
72 | } | |
73 | } | |
74 | ||
57c36b27 C |
75 | function buildTrigramSearchIndex (indexName: string, attribute: string) { |
76 | return { | |
77 | name: indexName, | |
78 | fields: [ Sequelize.literal('lower(immutable_unaccent(' + attribute + '))') as any ], | |
79 | using: 'gin', | |
80 | operator: 'gin_trgm_ops' | |
81 | } | |
82 | } | |
83 | ||
84 | function createSimilarityAttribute (col: string, value: string) { | |
85 | return Sequelize.fn( | |
86 | 'similarity', | |
87 | ||
88 | searchTrigramNormalizeCol(col), | |
89 | ||
90 | searchTrigramNormalizeValue(value) | |
91 | ) | |
92 | } | |
93 | ||
7ad9b984 C |
94 | function buildBlockedAccountSQL (serverAccountId: number, userAccountId?: number) { |
95 | const blockerIds = [ serverAccountId ] | |
96 | if (userAccountId) blockerIds.push(userAccountId) | |
97 | ||
98 | const blockerIdsString = blockerIds.join(', ') | |
99 | ||
418d092a | 100 | return 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' + |
7ad9b984 | 101 | ' UNION ALL ' + |
7ad9b984 C |
102 | 'SELECT "account"."id" AS "id" FROM account INNER JOIN "actor" ON account."actorId" = actor.id ' + |
103 | 'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' + | |
104 | 'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')' | |
418d092a C |
105 | } |
106 | ||
107 | function buildServerIdsFollowedBy (actorId: any) { | |
108 | const actorIdNumber = parseInt(actorId + '', 10) | |
109 | ||
110 | return '(' + | |
111 | 'SELECT "actor"."serverId" FROM "actorFollow" ' + | |
112 | 'INNER JOIN "actor" ON actor.id = "actorFollow"."targetActorId" ' + | |
113 | 'WHERE "actorFollow"."actorId" = ' + actorIdNumber + | |
114 | ')' | |
115 | } | |
7ad9b984 | 116 | |
418d092a C |
117 | function buildWhereIdOrUUID (id: number | string) { |
118 | return validator.isInt('' + id) ? { id } : { uuid: id } | |
7ad9b984 C |
119 | } |
120 | ||
3acc5084 C |
121 | function parseAggregateResult (result: any) { |
122 | if (!result) return 0 | |
123 | ||
124 | const total = parseInt(result + '', 10) | |
125 | if (isNaN(total)) return 0 | |
126 | ||
127 | return total | |
128 | } | |
129 | ||
3caf77d3 C |
130 | const createSafeIn = (model: typeof Model, stringArr: string[]) => { |
131 | return stringArr.map(t => model.sequelize.escape(t)) | |
132 | .join(', ') | |
133 | } | |
134 | ||
5c39adb7 C |
135 | // --------------------------------------------------------------------------- |
136 | ||
65fcc311 | 137 | export { |
7ad9b984 | 138 | buildBlockedAccountSQL, |
06215f15 | 139 | SortType, |
792dbaf0 | 140 | getSort, |
9a629c6e | 141 | getVideoSort, |
3fd3ab2d | 142 | getSortOnModel, |
57c36b27 C |
143 | createSimilarityAttribute, |
144 | throwIfNotValid, | |
418d092a C |
145 | buildServerIdsFollowedBy, |
146 | buildTrigramSearchIndex, | |
9f79ade6 | 147 | buildWhereIdOrUUID, |
3acc5084 | 148 | isOutdated, |
3caf77d3 C |
149 | parseAggregateResult, |
150 | createSafeIn | |
57c36b27 C |
151 | } |
152 | ||
153 | // --------------------------------------------------------------------------- | |
154 | ||
155 | function searchTrigramNormalizeValue (value: string) { | |
2cebd797 | 156 | return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', value)) |
57c36b27 C |
157 | } |
158 | ||
159 | function searchTrigramNormalizeCol (col: string) { | |
160 | return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', Sequelize.col(col))) | |
65fcc311 | 161 | } |
9a629c6e C |
162 | |
163 | function buildDirectionAndField (value: string) { | |
1735c825 | 164 | let field: string |
9a629c6e C |
165 | let direction: 'ASC' | 'DESC' |
166 | ||
167 | if (value.substring(0, 1) === '-') { | |
168 | direction = 'DESC' | |
169 | field = value.substring(1) | |
170 | } else { | |
171 | direction = 'ASC' | |
172 | field = value | |
173 | } | |
174 | ||
175 | return { direction, field } | |
176 | } |