]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/utils.ts
Add user/instance block by users in the client
[github/Chocobozzz/PeerTube.git] / server / models / utils.ts
CommitLineData
57c36b27
C
1import { Sequelize } from 'sequelize-typescript'
2
06215f15
C
3type SortType = { sortModel: any, sortValue: string }
4
9a629c6e 5// Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ]
3bb6c526 6function getSort (value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
4b54f136
C
7 let { direction, field } = buildDirectionAndField(value)
8
9 if (field.toLowerCase() === 'match') { // Search
10 field = Sequelize.col('similarity')
11 }
5c39adb7 12
9a629c6e
C
13 return [ [ field, direction ], lastSort ]
14}
15
16function getVideoSort (value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
17 let { direction, field } = buildDirectionAndField(value)
5c39adb7 18
57c36b27 19 // Alias
4b54f136
C
20 if (field.toLowerCase() === 'match') { // Search
21 field = Sequelize.col('similarity')
22 } else if (field.toLowerCase() === 'trending') { // Sort by aggregation
9a629c6e
C
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
3bb6c526 32 return [ [ field, direction ], lastSort ]
5c39adb7
C
33}
34
3bb6c526
C
35function getSortOnModel (model: any, value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
36 let [ firstSort ] = getSort(value)
792dbaf0 37
3bb6c526
C
38 if (model) return [ [ model, firstSort[0], firstSort[1] ], lastSort ]
39 return [ firstSort, lastSort ]
792dbaf0
GS
40}
41
3fd3ab2d
C
42function 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
57c36b27
C
48function 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
57function createSimilarityAttribute (col: string, value: string) {
58 return Sequelize.fn(
59 'similarity',
60
61 searchTrigramNormalizeCol(col),
62
63 searchTrigramNormalizeValue(value)
64 )
65}
66
7ad9b984
C
67function 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 "accountId" FROM "accountBlocklist" WHERE "targetAccountId" = user.account.id
76 // UNION ALL
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 + ')'
80
81 return query
82}
83
5c39adb7
C
84// ---------------------------------------------------------------------------
85
65fcc311 86export {
7ad9b984 87 buildBlockedAccountSQL,
06215f15 88 SortType,
792dbaf0 89 getSort,
9a629c6e 90 getVideoSort,
3fd3ab2d 91 getSortOnModel,
57c36b27
C
92 createSimilarityAttribute,
93 throwIfNotValid,
dbfd3e9b 94 buildTrigramSearchIndex
57c36b27
C
95}
96
97// ---------------------------------------------------------------------------
98
99function searchTrigramNormalizeValue (value: string) {
2cebd797 100 return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', value))
57c36b27
C
101}
102
103function searchTrigramNormalizeCol (col: string) {
104 return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', Sequelize.col(col)))
65fcc311 105}
9a629c6e
C
106
107function buildDirectionAndField (value: string) {
108 let field: any
109 let direction: 'ASC' | 'DESC'
110
111 if (value.substring(0, 1) === '-') {
112 direction = 'DESC'
113 field = value.substring(1)
114 } else {
115 direction = 'ASC'
116 field = value
117 }
118
119 return { direction, field }
120}