1 import { Sequelize } from 'sequelize-typescript'
3 type SortType = { sortModel: any, sortValue: string }
5 // Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ]
6 function getSort (value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
7 let { direction, field } = buildDirectionAndField(value)
9 if (field.toLowerCase() === 'match') { // Search
10 field = Sequelize.col('similarity')
13 return [ [ field, direction ], lastSort ]
16 function getVideoSort (value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
17 let { direction, field } = buildDirectionAndField(value)
20 if (field.toLowerCase() === 'match') { // Search
21 field = Sequelize.col('similarity')
22 } else if (field.toLowerCase() === 'trending') { // Sort by aggregation
24 [ Sequelize.fn('COALESCE', Sequelize.fn('SUM', Sequelize.col('VideoViews.views')), '0'), direction ],
26 [ Sequelize.col('VideoModel.views'), direction ],
32 return [ [ field, direction ], lastSort ]
35 function getSortOnModel (model: any, value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
36 let [ firstSort ] = getSort(value)
38 if (model) return [ [ model, firstSort[0], firstSort[1] ], lastSort ]
39 return [ firstSort, lastSort ]
42 function throwIfNotValid (value: any, validator: (value: any) => boolean, fieldName = 'value') {
43 if (validator(value) === false) {
44 throw new Error(`"${value}" is not a valid ${fieldName}.`)
48 function buildTrigramSearchIndex (indexName: string, attribute: string) {
51 fields: [ Sequelize.literal('lower(immutable_unaccent(' + attribute + '))') as any ],
53 operator: 'gin_trgm_ops'
57 function createSimilarityAttribute (col: string, value: string) {
61 searchTrigramNormalizeCol(col),
63 searchTrigramNormalizeValue(value)
67 function buildBlockedAccountSQL (serverAccountId: number, userAccountId?: number) {
68 const blockerIds = [ serverAccountId ]
69 if (userAccountId) blockerIds.push(userAccountId)
71 const blockerIdsString = blockerIds.join(', ')
73 const query = 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' +
75 // 'SELECT "accountId" FROM "accountBlocklist" WHERE "targetAccountId" = user.account.id
77 'SELECT "account"."id" AS "id" FROM account INNER JOIN "actor" ON account."actorId" = actor.id ' +
78 'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' +
79 'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')'
84 // ---------------------------------------------------------------------------
87 buildBlockedAccountSQL,
92 createSimilarityAttribute,
94 buildTrigramSearchIndex
97 // ---------------------------------------------------------------------------
99 function searchTrigramNormalizeValue (value: string) {
100 return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', value))
103 function searchTrigramNormalizeCol (col: string) {
104 return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', Sequelize.col(col)))
107 function buildDirectionAndField (value: string) {
109 let direction: 'ASC' | 'DESC'
111 if (value.substring(0, 1) === '-') {
113 field = value.substring(1)
119 return { direction, field }