X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Futils.ts;h=ccdbcd1cf7d8a81eafebc5668152315f44003a2a;hb=b8f4167fb6fa448125aeecff80b201d74e27fe6a;hp=30de91e1d305ab9b300beedceb6b15678ba52a8e;hpb=aa3796bda5a6776e0d21048239f25d385a8e7545;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/utils.ts b/server/models/utils.ts index 30de91e1d..ccdbcd1cf 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts @@ -1,9 +1,9 @@ import { Model, Sequelize } from 'sequelize-typescript' import * as validator from 'validator' import { Col } from 'sequelize/types/lib/utils' -import { OrderItem } from 'sequelize/types' +import { literal, OrderItem } from 'sequelize' -type SortType = { sortModel: any, sortValue: string } +type SortType = { sortModel: string, sortValue: string } // Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ] function getSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { @@ -51,10 +51,10 @@ function getVideoSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): Or return [ firstSort, lastSort ] } -function getSortOnModel (model: any, value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { +function getBlacklistSort (model: any, value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { const [ firstSort ] = getSort(value) - if (model) return [ [ model, firstSort[0], firstSort[1] ], lastSort ] + if (model) return [ [ literal(`"${model}.${firstSort[ 0 ]}" ${firstSort[ 1 ]}`) ], lastSort ] as any[] // FIXME: typings return [ firstSort, lastSort ] } @@ -129,19 +129,33 @@ function parseAggregateResult (result: any) { return total } -const createSafeIn = (model: typeof Model, stringArr: string[]) => { - return stringArr.map(t => model.sequelize.escape(t)) +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, + getBlacklistSort, createSimilarityAttribute, throwIfNotValid, buildServerIdsFollowedBy,