]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/utils.ts
Add delete/manual approval instance followers in client
[github/Chocobozzz/PeerTube.git] / server / models / utils.ts
CommitLineData
57c36b27 1import { Sequelize } from 'sequelize-typescript'
418d092a 2import * as validator from 'validator'
9f79ade6 3import { ACTIVITY_PUB } from '../initializers'
57c36b27 4
06215f15
C
5type SortType = { sortModel: any, sortValue: string }
6
9a629c6e 7// Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ]
3bb6c526 8function getSort (value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
4b54f136
C
9 let { direction, field } = buildDirectionAndField(value)
10
11 if (field.toLowerCase() === 'match') { // Search
12 field = Sequelize.col('similarity')
13 }
5c39adb7 14
9a629c6e
C
15 return [ [ field, direction ], lastSort ]
16}
17
18function getVideoSort (value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
19 let { direction, field } = buildDirectionAndField(value)
5c39adb7 20
57c36b27 21 // Alias
4b54f136
C
22 if (field.toLowerCase() === 'match') { // Search
23 field = Sequelize.col('similarity')
24 } else if (field.toLowerCase() === 'trending') { // Sort by aggregation
9a629c6e
C
25 return [
26 [ Sequelize.fn('COALESCE', Sequelize.fn('SUM', Sequelize.col('VideoViews.views')), '0'), direction ],
27
28 [ Sequelize.col('VideoModel.views'), direction ],
29
30 lastSort
31 ]
32 }
33
afa4374a
C
34 const firstSort = typeof field === 'string' ?
35 field.split('.').concat([ direction ]) :
36 [ field, direction ]
37
38 return [ firstSort, lastSort ]
5c39adb7
C
39}
40
3bb6c526
C
41function getSortOnModel (model: any, value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
42 let [ firstSort ] = getSort(value)
792dbaf0 43
3bb6c526
C
44 if (model) return [ [ model, firstSort[0], firstSort[1] ], lastSort ]
45 return [ firstSort, lastSort ]
792dbaf0
GS
46}
47
9f79ade6
C
48function isOutdated (model: { createdAt: Date, updatedAt: Date }, refreshInterval: number) {
49 const now = Date.now()
50 const createdAtTime = model.createdAt.getTime()
51 const updatedAtTime = model.updatedAt.getTime()
52
53 return (now - createdAtTime) > refreshInterval && (now - updatedAtTime) > refreshInterval
54}
55
3fd3ab2d
C
56function throwIfNotValid (value: any, validator: (value: any) => boolean, fieldName = 'value') {
57 if (validator(value) === false) {
58 throw new Error(`"${value}" is not a valid ${fieldName}.`)
59 }
60}
61
57c36b27
C
62function buildTrigramSearchIndex (indexName: string, attribute: string) {
63 return {
64 name: indexName,
65 fields: [ Sequelize.literal('lower(immutable_unaccent(' + attribute + '))') as any ],
66 using: 'gin',
67 operator: 'gin_trgm_ops'
68 }
69}
70
71function createSimilarityAttribute (col: string, value: string) {
72 return Sequelize.fn(
73 'similarity',
74
75 searchTrigramNormalizeCol(col),
76
77 searchTrigramNormalizeValue(value)
78 )
79}
80
7ad9b984
C
81function buildBlockedAccountSQL (serverAccountId: number, userAccountId?: number) {
82 const blockerIds = [ serverAccountId ]
83 if (userAccountId) blockerIds.push(userAccountId)
84
85 const blockerIdsString = blockerIds.join(', ')
86
418d092a 87 return 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' +
7ad9b984 88 ' UNION ALL ' +
7ad9b984
C
89 'SELECT "account"."id" AS "id" FROM account INNER JOIN "actor" ON account."actorId" = actor.id ' +
90 'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' +
91 'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')'
418d092a
C
92}
93
94function buildServerIdsFollowedBy (actorId: any) {
95 const actorIdNumber = parseInt(actorId + '', 10)
96
97 return '(' +
98 'SELECT "actor"."serverId" FROM "actorFollow" ' +
99 'INNER JOIN "actor" ON actor.id = "actorFollow"."targetActorId" ' +
100 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
101 ')'
102}
7ad9b984 103
418d092a
C
104function buildWhereIdOrUUID (id: number | string) {
105 return validator.isInt('' + id) ? { id } : { uuid: id }
7ad9b984
C
106}
107
5c39adb7
C
108// ---------------------------------------------------------------------------
109
65fcc311 110export {
7ad9b984 111 buildBlockedAccountSQL,
06215f15 112 SortType,
792dbaf0 113 getSort,
9a629c6e 114 getVideoSort,
3fd3ab2d 115 getSortOnModel,
57c36b27
C
116 createSimilarityAttribute,
117 throwIfNotValid,
418d092a
C
118 buildServerIdsFollowedBy,
119 buildTrigramSearchIndex,
9f79ade6
C
120 buildWhereIdOrUUID,
121 isOutdated
57c36b27
C
122}
123
124// ---------------------------------------------------------------------------
125
126function searchTrigramNormalizeValue (value: string) {
2cebd797 127 return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', value))
57c36b27
C
128}
129
130function searchTrigramNormalizeCol (col: string) {
131 return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', Sequelize.col(col)))
65fcc311 132}
9a629c6e
C
133
134function buildDirectionAndField (value: string) {
135 let field: any
136 let direction: 'ASC' | 'DESC'
137
138 if (value.substring(0, 1) === '-') {
139 direction = 'DESC'
140 field = value.substring(1)
141 } else {
142 direction = 'ASC'
143 field = value
144 }
145
146 return { direction, field }
147}