1 import { Sequelize } from 'sequelize-typescript'
2 import * as validator from 'validator'
4 type SortType = { sortModel: any, sortValue: string }
6 // Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ]
7 function getSort (value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
8 let { direction, field } = buildDirectionAndField(value)
10 if (field.toLowerCase() === 'match') { // Search
11 field = Sequelize.col('similarity')
14 return [ [ field, direction ], lastSort ]
17 function getVideoSort (value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
18 let { direction, field } = buildDirectionAndField(value)
21 if (field.toLowerCase() === 'match') { // Search
22 field = Sequelize.col('similarity')
23 } else if (field.toLowerCase() === 'trending') { // Sort by aggregation
25 [ Sequelize.fn('COALESCE', Sequelize.fn('SUM', Sequelize.col('VideoViews.views')), '0'), direction ],
27 [ Sequelize.col('VideoModel.views'), direction ],
33 const firstSort = typeof field === 'string' ?
34 field.split('.').concat([ direction ]) :
37 return [ firstSort, lastSort ]
40 function getSortOnModel (model: any, value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
41 let [ firstSort ] = getSort(value)
43 if (model) return [ [ model, firstSort[0], firstSort[1] ], lastSort ]
44 return [ firstSort, lastSort ]
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()
52 return (now - createdAtTime) > refreshInterval && (now - updatedAtTime) > refreshInterval
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}.`)
61 function buildTrigramSearchIndex (indexName: string, attribute: string) {
64 fields: [ Sequelize.literal('lower(immutable_unaccent(' + attribute + '))') as any ],
66 operator: 'gin_trgm_ops'
70 function createSimilarityAttribute (col: string, value: string) {
74 searchTrigramNormalizeCol(col),
76 searchTrigramNormalizeValue(value)
80 function buildBlockedAccountSQL (serverAccountId: number, userAccountId?: number) {
81 const blockerIds = [ serverAccountId ]
82 if (userAccountId) blockerIds.push(userAccountId)
84 const blockerIdsString = blockerIds.join(', ')
86 return 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' +
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 + ')'
93 function buildServerIdsFollowedBy (actorId: any) {
94 const actorIdNumber = parseInt(actorId + '', 10)
97 'SELECT "actor"."serverId" FROM "actorFollow" ' +
98 'INNER JOIN "actor" ON actor.id = "actorFollow"."targetActorId" ' +
99 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
103 function buildWhereIdOrUUID (id: number | string) {
104 return validator.isInt('' + id) ? { id } : { uuid: id }
107 // ---------------------------------------------------------------------------
110 buildBlockedAccountSQL,
115 createSimilarityAttribute,
117 buildServerIdsFollowedBy,
118 buildTrigramSearchIndex,
123 // ---------------------------------------------------------------------------
125 function searchTrigramNormalizeValue (value: string) {
126 return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', value))
129 function searchTrigramNormalizeCol (col: string) {
130 return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', Sequelize.col(col)))
133 function buildDirectionAndField (value: string) {
135 let direction: 'ASC' | 'DESC'
137 if (value.substring(0, 1) === '-') {
139 field = value.substring(1)
145 return { direction, field }