]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/utils.ts
Increase refresh interval to 2 days
[github/Chocobozzz/PeerTube.git] / server / models / utils.ts
CommitLineData
57c36b27 1import { Sequelize } from 'sequelize-typescript'
418d092a 2import * as validator from 'validator'
57c36b27 3
06215f15
C
4type SortType = { sortModel: any, sortValue: string }
5
9a629c6e 6// Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ]
3bb6c526 7function 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
17function 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
40function 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
3fd3ab2d
C
47function throwIfNotValid (value: any, validator: (value: any) => boolean, fieldName = 'value') {
48 if (validator(value) === false) {
49 throw new Error(`"${value}" is not a valid ${fieldName}.`)
50 }
51}
52
57c36b27
C
53function buildTrigramSearchIndex (indexName: string, attribute: string) {
54 return {
55 name: indexName,
56 fields: [ Sequelize.literal('lower(immutable_unaccent(' + attribute + '))') as any ],
57 using: 'gin',
58 operator: 'gin_trgm_ops'
59 }
60}
61
62function createSimilarityAttribute (col: string, value: string) {
63 return Sequelize.fn(
64 'similarity',
65
66 searchTrigramNormalizeCol(col),
67
68 searchTrigramNormalizeValue(value)
69 )
70}
71
7ad9b984
C
72function buildBlockedAccountSQL (serverAccountId: number, userAccountId?: number) {
73 const blockerIds = [ serverAccountId ]
74 if (userAccountId) blockerIds.push(userAccountId)
75
76 const blockerIdsString = blockerIds.join(', ')
77
418d092a 78 return 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' +
7ad9b984 79 ' UNION ALL ' +
7ad9b984
C
80 'SELECT "account"."id" AS "id" FROM account INNER JOIN "actor" ON account."actorId" = actor.id ' +
81 'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' +
82 'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')'
418d092a
C
83}
84
85function buildServerIdsFollowedBy (actorId: any) {
86 const actorIdNumber = parseInt(actorId + '', 10)
87
88 return '(' +
89 'SELECT "actor"."serverId" FROM "actorFollow" ' +
90 'INNER JOIN "actor" ON actor.id = "actorFollow"."targetActorId" ' +
91 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
92 ')'
93}
7ad9b984 94
418d092a
C
95function buildWhereIdOrUUID (id: number | string) {
96 return validator.isInt('' + id) ? { id } : { uuid: id }
7ad9b984
C
97}
98
5c39adb7
C
99// ---------------------------------------------------------------------------
100
65fcc311 101export {
7ad9b984 102 buildBlockedAccountSQL,
06215f15 103 SortType,
792dbaf0 104 getSort,
9a629c6e 105 getVideoSort,
3fd3ab2d 106 getSortOnModel,
57c36b27
C
107 createSimilarityAttribute,
108 throwIfNotValid,
418d092a
C
109 buildServerIdsFollowedBy,
110 buildTrigramSearchIndex,
111 buildWhereIdOrUUID
57c36b27
C
112}
113
114// ---------------------------------------------------------------------------
115
116function searchTrigramNormalizeValue (value: string) {
2cebd797 117 return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', value))
57c36b27
C
118}
119
120function searchTrigramNormalizeCol (col: string) {
121 return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', Sequelize.col(col)))
65fcc311 122}
9a629c6e
C
123
124function buildDirectionAndField (value: string) {
125 let field: any
126 let direction: 'ASC' | 'DESC'
127
128 if (value.substring(0, 1) === '-') {
129 direction = 'DESC'
130 field = value.substring(1)
131 } else {
132 direction = 'ASC'
133 field = value
134 }
135
136 return { direction, field }
137}