]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/utils.ts
Factorize video display in table for moderation components, apply it to blacklisted...
[github/Chocobozzz/PeerTube.git] / server / models / utils.ts
CommitLineData
3caf77d3 1import { Model, Sequelize } from 'sequelize-typescript'
7cde3b9c 2import validator from 'validator'
1735c825 3import { Col } from 'sequelize/types/lib/utils'
d7a25329 4import { literal, OrderItem } from 'sequelize'
57c36b27 5
8319d6ae
RK
6type Primitive = string | Function | number | boolean | Symbol | undefined | null
7type DeepOmitHelper<T, K extends keyof T> = {
8 [P in K]: // extra level of indirection needed to trigger homomorhic behavior
9 T[P] extends infer TP // distribute over unions
10 ? TP extends Primitive
11 ? TP // leave primitives and functions alone
12 : TP extends any[]
13 ? DeepOmitArray<TP, K> // Array special handling
14 : DeepOmit<TP, K>
15 : never
16}
17type DeepOmit<T, K> = T extends Primitive ? T : DeepOmitHelper<T, Exclude<keyof T, K>>
18
19type DeepOmitArray<T extends any[], K> = {
20 [P in keyof T]: DeepOmit<T[P], K>
21}
22
95153292 23type SortType = { sortModel: string, sortValue: string }
06215f15 24
9a629c6e 25// Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ]
1735c825
C
26function getSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
27 const { direction, field } = buildDirectionAndField(value)
28
29 let finalField: string | Col
4b54f136
C
30
31 if (field.toLowerCase() === 'match') { // Search
1735c825 32 finalField = Sequelize.col('similarity')
aa3796bd
C
33 } else if (field === 'videoQuotaUsed') { // Users list
34 finalField = Sequelize.col('videoQuotaUsed')
1735c825
C
35 } else {
36 finalField = field
4b54f136 37 }
5c39adb7 38
1735c825 39 return [ [ finalField, direction ], lastSort ]
9a629c6e
C
40}
41
c1125bca
RK
42function getCommentSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
43 const { direction, field } = buildDirectionAndField(value)
44
45 if (field === 'totalReplies') {
46 return [
47 [ Sequelize.literal('"totalReplies"'), direction ],
48 lastSort
49 ]
50 }
51
52 return getSort(value, lastSort)
53}
54
1735c825
C
55function getVideoSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
56 const { direction, field } = buildDirectionAndField(value)
5c39adb7 57
1735c825 58 if (field.toLowerCase() === 'trending') { // Sort by aggregation
9a629c6e
C
59 return [
60 [ Sequelize.fn('COALESCE', Sequelize.fn('SUM', Sequelize.col('VideoViews.views')), '0'), direction ],
61
62 [ Sequelize.col('VideoModel.views'), direction ],
63
64 lastSort
65 ]
66 }
67
1735c825
C
68 let finalField: string | Col
69
70 // Alias
71 if (field.toLowerCase() === 'match') { // Search
72 finalField = Sequelize.col('similarity')
73 } else {
74 finalField = field
75 }
76
77 const firstSort = typeof finalField === 'string'
78 ? finalField.split('.').concat([ direction ]) as any // FIXME: sequelize typings
79 : [ finalField, direction ]
afa4374a
C
80
81 return [ firstSort, lastSort ]
5c39adb7
C
82}
83
95153292 84function getBlacklistSort (model: any, value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
1735c825 85 const [ firstSort ] = getSort(value)
792dbaf0 86
a1587156 87 if (model) return [ [ literal(`"${model}.${firstSort[0]}" ${firstSort[1]}`) ], lastSort ] as any[] // FIXME: typings
3bb6c526 88 return [ firstSort, lastSort ]
792dbaf0
GS
89}
90
cb5ce4cb
C
91function getFollowsSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
92 const { direction, field } = buildDirectionAndField(value)
93
94 if (field === 'redundancyAllowed') {
95 return [
96 [ 'ActorFollowing', 'Server', 'redundancyAllowed', direction ],
97 lastSort
98 ]
99 }
100
101 return getSort(value, lastSort)
102}
103
9f79ade6
C
104function isOutdated (model: { createdAt: Date, updatedAt: Date }, refreshInterval: number) {
105 const now = Date.now()
106 const createdAtTime = model.createdAt.getTime()
107 const updatedAtTime = model.updatedAt.getTime()
108
109 return (now - createdAtTime) > refreshInterval && (now - updatedAtTime) > refreshInterval
110}
111
1735c825
C
112function throwIfNotValid (value: any, validator: (value: any) => boolean, fieldName = 'value', nullable = false) {
113 if (nullable && (value === null || value === undefined)) return
114
3fd3ab2d
C
115 if (validator(value) === false) {
116 throw new Error(`"${value}" is not a valid ${fieldName}.`)
117 }
118}
119
57c36b27
C
120function buildTrigramSearchIndex (indexName: string, attribute: string) {
121 return {
122 name: indexName,
123 fields: [ Sequelize.literal('lower(immutable_unaccent(' + attribute + '))') as any ],
124 using: 'gin',
125 operator: 'gin_trgm_ops'
126 }
127}
128
129function createSimilarityAttribute (col: string, value: string) {
130 return Sequelize.fn(
131 'similarity',
132
133 searchTrigramNormalizeCol(col),
134
135 searchTrigramNormalizeValue(value)
136 )
137}
138
7ad9b984
C
139function buildBlockedAccountSQL (serverAccountId: number, userAccountId?: number) {
140 const blockerIds = [ serverAccountId ]
141 if (userAccountId) blockerIds.push(userAccountId)
142
143 const blockerIdsString = blockerIds.join(', ')
144
418d092a 145 return 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' +
7ad9b984 146 ' UNION ALL ' +
7ad9b984
C
147 'SELECT "account"."id" AS "id" FROM account INNER JOIN "actor" ON account."actorId" = actor.id ' +
148 'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' +
149 'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')'
418d092a
C
150}
151
152function buildServerIdsFollowedBy (actorId: any) {
153 const actorIdNumber = parseInt(actorId + '', 10)
154
155 return '(' +
156 'SELECT "actor"."serverId" FROM "actorFollow" ' +
157 'INNER JOIN "actor" ON actor.id = "actorFollow"."targetActorId" ' +
158 'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
a1587156 159 ')'
418d092a 160}
7ad9b984 161
418d092a
C
162function buildWhereIdOrUUID (id: number | string) {
163 return validator.isInt('' + id) ? { id } : { uuid: id }
7ad9b984
C
164}
165
3acc5084
C
166function parseAggregateResult (result: any) {
167 if (!result) return 0
168
169 const total = parseInt(result + '', 10)
170 if (isNaN(total)) return 0
171
172 return total
173}
174
6b9c966f 175const createSafeIn = (model: typeof Model, stringArr: (string | number)[]) => {
5f3e2425
C
176 return stringArr.map(t => {
177 return t === null
178 ? null
179 : model.sequelize.escape('' + t)
180 }).join(', ')
3caf77d3
C
181}
182
6b9c966f
C
183function buildLocalAccountIdsIn () {
184 return literal(
185 '(SELECT "account"."id" FROM "account" INNER JOIN "actor" ON "actor"."id" = "account"."actorId" AND "actor"."serverId" IS NULL)'
186 )
187}
188
189function buildLocalActorIdsIn () {
190 return literal(
191 '(SELECT "actor"."id" FROM "actor" WHERE "actor"."serverId" IS NULL)'
192 )
193}
194
5f3e2425
C
195function buildDirectionAndField (value: string) {
196 let field: string
197 let direction: 'ASC' | 'DESC'
198
199 if (value.substring(0, 1) === '-') {
200 direction = 'DESC'
201 field = value.substring(1)
202 } else {
203 direction = 'ASC'
204 field = value
205 }
206
207 return { direction, field }
208}
209
5c39adb7
C
210// ---------------------------------------------------------------------------
211
65fcc311 212export {
8319d6ae 213 DeepOmit,
7ad9b984 214 buildBlockedAccountSQL,
6b9c966f 215 buildLocalActorIdsIn,
06215f15 216 SortType,
6b9c966f 217 buildLocalAccountIdsIn,
792dbaf0 218 getSort,
c1125bca 219 getCommentSort,
9a629c6e 220 getVideoSort,
95153292 221 getBlacklistSort,
57c36b27
C
222 createSimilarityAttribute,
223 throwIfNotValid,
418d092a
C
224 buildServerIdsFollowedBy,
225 buildTrigramSearchIndex,
9f79ade6 226 buildWhereIdOrUUID,
3acc5084 227 isOutdated,
3caf77d3 228 parseAggregateResult,
cb5ce4cb 229 getFollowsSort,
5f3e2425 230 buildDirectionAndField,
3caf77d3 231 createSafeIn
57c36b27
C
232}
233
234// ---------------------------------------------------------------------------
235
236function searchTrigramNormalizeValue (value: string) {
2cebd797 237 return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', value))
57c36b27
C
238}
239
240function searchTrigramNormalizeCol (col: string) {
241 return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', Sequelize.col(col)))
65fcc311 242}