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