diff options
Diffstat (limited to 'server/models/utils.ts')
-rw-r--r-- | server/models/utils.ts | 52 |
1 files changed, 26 insertions, 26 deletions
diff --git a/server/models/utils.ts b/server/models/utils.ts index 0b6ac8340..69ad123ac 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts | |||
@@ -1,6 +1,7 @@ | |||
1 | import { literal, Op, OrderItem, Sequelize } from 'sequelize' | 1 | import { literal, Model, ModelStatic, Op, OrderItem, Sequelize } from 'sequelize' |
2 | import validator from 'validator' | 2 | import validator from 'validator' |
3 | import { forceNumber } from '@shared/core-utils' | 3 | import { forceNumber } from '@shared/core-utils' |
4 | import { AttributesOnly } from '@shared/typescript-utils' | ||
4 | 5 | ||
5 | type SortType = { sortModel: string, sortValue: string } | 6 | type SortType = { sortModel: string, sortValue: string } |
6 | 7 | ||
@@ -178,30 +179,6 @@ function buildBlockedAccountSQL (blockerIds: number[]) { | |||
178 | 'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')' | 179 | 'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')' |
179 | } | 180 | } |
180 | 181 | ||
181 | function buildBlockedAccountSQLOptimized (columnNameJoin: string, blockerIds: number[]) { | ||
182 | const blockerIdsString = blockerIds.join(', ') | ||
183 | |||
184 | return [ | ||
185 | literal( | ||
186 | `NOT EXISTS (` + | ||
187 | ` SELECT 1 FROM "accountBlocklist" ` + | ||
188 | ` WHERE "targetAccountId" = ${columnNameJoin} ` + | ||
189 | ` AND "accountId" IN (${blockerIdsString})` + | ||
190 | `)` | ||
191 | ), | ||
192 | |||
193 | literal( | ||
194 | `NOT EXISTS (` + | ||
195 | ` SELECT 1 FROM "account" ` + | ||
196 | ` INNER JOIN "actor" ON account."actorId" = actor.id ` + | ||
197 | ` INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ` + | ||
198 | ` WHERE "account"."id" = ${columnNameJoin} ` + | ||
199 | ` AND "serverBlocklist"."accountId" IN (${blockerIdsString})` + | ||
200 | `)` | ||
201 | ) | ||
202 | ] | ||
203 | } | ||
204 | |||
205 | function buildServerIdsFollowedBy (actorId: any) { | 182 | function buildServerIdsFollowedBy (actorId: any) { |
206 | const actorIdNumber = forceNumber(actorId) | 183 | const actorIdNumber = forceNumber(actorId) |
207 | 184 | ||
@@ -277,11 +254,34 @@ function searchAttribute (sourceField?: string, targetField?: string) { | |||
277 | } | 254 | } |
278 | } | 255 | } |
279 | 256 | ||
257 | function buildSQLAttributes <M extends Model> (options: { | ||
258 | model: ModelStatic<M> | ||
259 | tableName: string | ||
260 | |||
261 | excludeAttributes?: (keyof AttributesOnly<M>)[] | ||
262 | aliasPrefix?: string | ||
263 | }) { | ||
264 | const { model, tableName, aliasPrefix, excludeAttributes } = options | ||
265 | |||
266 | const attributes = Object.keys(model.getAttributes()) | ||
267 | |||
268 | return attributes | ||
269 | .filter(a => { | ||
270 | if (!excludeAttributes) return true | ||
271 | if (excludeAttributes.includes(a)) return false | ||
272 | |||
273 | return true | ||
274 | }) | ||
275 | .map(a => { | ||
276 | return `"${tableName}"."${a}" AS "${aliasPrefix || ''}${a}"` | ||
277 | }) | ||
278 | } | ||
279 | |||
280 | // --------------------------------------------------------------------------- | 280 | // --------------------------------------------------------------------------- |
281 | 281 | ||
282 | export { | 282 | export { |
283 | buildSQLAttributes, | ||
283 | buildBlockedAccountSQL, | 284 | buildBlockedAccountSQL, |
284 | buildBlockedAccountSQLOptimized, | ||
285 | buildLocalActorIdsIn, | 285 | buildLocalActorIdsIn, |
286 | getPlaylistSort, | 286 | getPlaylistSort, |
287 | SortType, | 287 | SortType, |