X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Futils.ts;h=24890f961e5d1d2738d0148b37d6561469cf445b;hb=dedc7abb7beccb0b3a4a4fa4e8ccfef8ceecd5a6;hp=98170a00e14ec2c7275e8992b9874d3c2f3e1b09;hpb=1735c825726edaa0af5035cb6cbb0cc0db502c6d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/utils.ts b/server/models/utils.ts index 98170a00e..24890f961 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts @@ -1,7 +1,7 @@ -import { Sequelize } from 'sequelize-typescript' +import { Model, Sequelize } from 'sequelize-typescript' import * as validator from 'validator' -import { OrderItem } from 'sequelize' import { Col } from 'sequelize/types/lib/utils' +import { OrderItem, literal } from 'sequelize' type SortType = { sortModel: any, sortValue: string } @@ -13,6 +13,8 @@ function getSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderIt if (field.toLowerCase() === 'match') { // Search finalField = Sequelize.col('similarity') + } else if (field === 'videoQuotaUsed') { // Users list + finalField = Sequelize.col('videoQuotaUsed') } else { finalField = field } @@ -118,11 +120,39 @@ function buildWhereIdOrUUID (id: number | string) { return validator.isInt('' + id) ? { id } : { uuid: id } } +function parseAggregateResult (result: any) { + if (!result) return 0 + + const total = parseInt(result + '', 10) + if (isNaN(total)) return 0 + + return total +} + +const createSafeIn = (model: typeof Model, stringArr: (string | number)[]) => { + return stringArr.map(t => model.sequelize.escape('' + t)) + .join(', ') +} + +function buildLocalAccountIdsIn () { + return literal( + '(SELECT "account"."id" FROM "account" INNER JOIN "actor" ON "actor"."id" = "account"."actorId" AND "actor"."serverId" IS NULL)' + ) +} + +function buildLocalActorIdsIn () { + return literal( + '(SELECT "actor"."id" FROM "actor" WHERE "actor"."serverId" IS NULL)' + ) +} + // --------------------------------------------------------------------------- export { buildBlockedAccountSQL, + buildLocalActorIdsIn, SortType, + buildLocalAccountIdsIn, getSort, getVideoSort, getSortOnModel, @@ -131,7 +161,9 @@ export { buildServerIdsFollowedBy, buildTrigramSearchIndex, buildWhereIdOrUUID, - isOutdated + isOutdated, + parseAggregateResult, + createSafeIn } // ---------------------------------------------------------------------------