X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Futils.ts;h=e7e6ddde1f61e6c86117b4fa073f32bf6ebfe45e;hb=a0e6d267598839c8a5508a65876ce0e07d1b3d74;hp=2b172f6081e3eea7437a11010968af414ae76ab2;hpb=3acc50844047a37698f0618fa235c138e386a053;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/utils.ts b/server/models/utils.ts index 2b172f608..e7e6ddde1 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts @@ -1,9 +1,9 @@ -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 { col, 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[] { @@ -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 } @@ -49,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 ] } @@ -127,21 +129,41 @@ function parseAggregateResult (result: any) { 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, + getBlacklistSort, createSimilarityAttribute, throwIfNotValid, buildServerIdsFollowedBy, buildTrigramSearchIndex, buildWhereIdOrUUID, isOutdated, - parseAggregateResult + parseAggregateResult, + createSafeIn } // ---------------------------------------------------------------------------