X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Futils.ts;h=6e5522346dcca1273671d9b34ce52d2c20b7c601;hb=97816649b793bdd0f3df64631ae0ef7cf3d7461c;hp=f7afb8d4b854181622ffa5f9e9c28de42fc1e7d1;hpb=a15871560f80e07386c1dabb8370cd2664ecfd1f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/utils.ts b/server/models/utils.ts index f7afb8d4b..6e5522346 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts @@ -1,7 +1,7 @@ import { Model, Sequelize } from 'sequelize-typescript' import validator from 'validator' import { Col } from 'sequelize/types/lib/utils' -import { literal, OrderItem } from 'sequelize' +import { literal, OrderItem, Op } from 'sequelize' type SortType = { sortModel: string, sortValue: string } @@ -119,10 +119,7 @@ function createSimilarityAttribute (col: string, value: string) { ) } -function buildBlockedAccountSQL (serverAccountId: number, userAccountId?: number) { - const blockerIds = [ serverAccountId ] - if (userAccountId) blockerIds.push(userAccountId) - +function buildBlockedAccountSQL (blockerIds: number[]) { const blockerIdsString = blockerIds.join(', ') return 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' + @@ -132,6 +129,30 @@ function buildBlockedAccountSQL (serverAccountId: number, userAccountId?: number 'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')' } +function buildBlockedAccountSQLOptimized (columnNameJoin: string, blockerIds: number[]) { + const blockerIdsString = blockerIds.join(', ') + + return [ + literal( + `NOT EXISTS (` + + ` SELECT 1 FROM "accountBlocklist" ` + + ` WHERE "targetAccountId" = ${columnNameJoin} ` + + ` AND "accountId" IN (${blockerIdsString})` + + `)` + ), + + literal( + `NOT EXISTS (` + + ` SELECT 1 FROM "account" ` + + ` INNER JOIN "actor" ON account."actorId" = actor.id ` + + ` INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ` + + ` WHERE "account"."id" = ${columnNameJoin} ` + + ` AND "serverBlocklist"."accountId" IN (${blockerIdsString})` + + `)` + ) + ] +} + function buildServerIdsFollowedBy (actorId: any) { const actorIdNumber = parseInt(actorId + '', 10) @@ -156,8 +177,11 @@ function parseAggregateResult (result: any) { } const createSafeIn = (model: typeof Model, stringArr: (string | number)[]) => { - return stringArr.map(t => model.sequelize.escape('' + t)) - .join(', ') + return stringArr.map(t => { + return t === null + ? null + : model.sequelize.escape('' + t) + }).join(', ') } function buildLocalAccountIdsIn () { @@ -172,10 +196,36 @@ function buildLocalActorIdsIn () { ) } +function buildDirectionAndField (value: string) { + let field: string + let direction: 'ASC' | 'DESC' + + if (value.substring(0, 1) === '-') { + direction = 'DESC' + field = value.substring(1) + } else { + direction = 'ASC' + field = value + } + + return { direction, field } +} + +function searchAttribute (sourceField?: string, targetField?: string) { + if (!sourceField) return {} + + return { + [targetField]: { + [Op.iLike]: `%${sourceField}%` + } + } +} + // --------------------------------------------------------------------------- export { buildBlockedAccountSQL, + buildBlockedAccountSQLOptimized, buildLocalActorIdsIn, SortType, buildLocalAccountIdsIn, @@ -191,7 +241,9 @@ export { isOutdated, parseAggregateResult, getFollowsSort, - createSafeIn + buildDirectionAndField, + createSafeIn, + searchAttribute } // --------------------------------------------------------------------------- @@ -203,18 +255,3 @@ function searchTrigramNormalizeValue (value: string) { function searchTrigramNormalizeCol (col: string) { return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', Sequelize.col(col))) } - -function buildDirectionAndField (value: string) { - let field: string - let direction: 'ASC' | 'DESC' - - if (value.substring(0, 1) === '-') { - direction = 'DESC' - field = value.substring(1) - } else { - direction = 'ASC' - field = value - } - - return { direction, field } -}