Merge branch 'feature/webtorrent-disabling' into develop
[oweals/peertube.git] / server / models / utils.ts
1 import { Sequelize } from 'sequelize-typescript'
2
3 type SortType = { sortModel: any, sortValue: string }
4
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)
8
9   if (field.toLowerCase() === 'match') { // Search
10     field = Sequelize.col('similarity')
11   }
12
13   return [ [ field, direction ], lastSort ]
14 }
15
16 function getVideoSort (value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
17   let { direction, field } = buildDirectionAndField(value)
18
19   // Alias
20   if (field.toLowerCase() === 'match') { // Search
21     field = Sequelize.col('similarity')
22   } else if (field.toLowerCase() === 'trending') { // Sort by aggregation
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
32   return [ [ field, direction ], lastSort ]
33 }
34
35 function getSortOnModel (model: any, value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
36   let [ firstSort ] = getSort(value)
37
38   if (model) return [ [ model, firstSort[0], firstSort[1] ], lastSort ]
39   return [ firstSort, lastSort ]
40 }
41
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}.`)
45   }
46 }
47
48 function buildTrigramSearchIndex (indexName: string, attribute: string) {
49   return {
50     name: indexName,
51     fields: [ Sequelize.literal('lower(immutable_unaccent(' + attribute + '))') as any ],
52     using: 'gin',
53     operator: 'gin_trgm_ops'
54   }
55 }
56
57 function createSimilarityAttribute (col: string, value: string) {
58   return Sequelize.fn(
59     'similarity',
60
61     searchTrigramNormalizeCol(col),
62
63     searchTrigramNormalizeValue(value)
64   )
65 }
66
67 function buildBlockedAccountSQL (serverAccountId: number, userAccountId?: number) {
68   const blockerIds = [ serverAccountId ]
69   if (userAccountId) blockerIds.push(userAccountId)
70
71   const blockerIdsString = blockerIds.join(', ')
72
73   const query = 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' +
74     ' UNION ALL ' +
75     'SELECT "account"."id" AS "id" FROM account INNER JOIN "actor" ON account."actorId" = actor.id ' +
76     'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' +
77     'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')'
78
79   return query
80 }
81
82 // ---------------------------------------------------------------------------
83
84 export {
85   buildBlockedAccountSQL,
86   SortType,
87   getSort,
88   getVideoSort,
89   getSortOnModel,
90   createSimilarityAttribute,
91   throwIfNotValid,
92   buildTrigramSearchIndex
93 }
94
95 // ---------------------------------------------------------------------------
96
97 function searchTrigramNormalizeValue (value: string) {
98   return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', value))
99 }
100
101 function searchTrigramNormalizeCol (col: string) {
102   return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', Sequelize.col(col)))
103 }
104
105 function buildDirectionAndField (value: string) {
106   let field: any
107   let direction: 'ASC' | 'DESC'
108
109   if (value.substring(0, 1) === '-') {
110     direction = 'DESC'
111     field = value.substring(1)
112   } else {
113     direction = 'ASC'
114     field = value
115   }
116
117   return { direction, field }
118 }