X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Futils.ts;h=f7afb8d4b854181622ffa5f9e9c28de42fc1e7d1;hb=a30a136c9896c656cab98d2c92cde32c534dc098;hp=2b172f6081e3eea7437a11010968af414ae76ab2;hpb=97567dd81f508dd6295ac4d73d849aa2ce0a6549;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/utils.ts b/server/models/utils.ts index 2b172f608..f7afb8d4b 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts @@ -1,9 +1,9 @@ -import { Sequelize } from 'sequelize-typescript' -import * as validator from 'validator' -import { OrderItem } from 'sequelize' +import { Model, Sequelize } from 'sequelize-typescript' +import validator from 'validator' import { Col } from 'sequelize/types/lib/utils' +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[] { @@ -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 } @@ -20,6 +22,19 @@ function getSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderIt return [ [ finalField, direction ], lastSort ] } +function getCommentSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { + const { direction, field } = buildDirectionAndField(value) + + if (field === 'totalReplies') { + return [ + [ Sequelize.literal('"totalReplies"'), direction ], + lastSort + ] + } + + return getSort(value, lastSort) +} + function getVideoSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { const { direction, field } = buildDirectionAndField(value) @@ -49,13 +64,26 @@ 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 ] } +function getFollowsSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { + const { direction, field } = buildDirectionAndField(value) + + if (field === 'redundancyAllowed') { + return [ + [ 'ActorFollowing', 'Server', 'redundancyAllowed', direction ], + lastSort + ] + } + + return getSort(value, lastSort) +} + function isOutdated (model: { createdAt: Date, updatedAt: Date }, refreshInterval: number) { const now = Date.now() const createdAtTime = model.createdAt.getTime() @@ -111,7 +139,7 @@ function buildServerIdsFollowedBy (actorId: any) { 'SELECT "actor"."serverId" FROM "actorFollow" ' + 'INNER JOIN "actor" ON actor.id = "actorFollow"."targetActorId" ' + 'WHERE "actorFollow"."actorId" = ' + actorIdNumber + - ')' + ')' } function buildWhereIdOrUUID (id: number | string) { @@ -127,21 +155,43 @@ 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, + getCommentSort, getVideoSort, - getSortOnModel, + getBlacklistSort, createSimilarityAttribute, throwIfNotValid, buildServerIdsFollowedBy, buildTrigramSearchIndex, buildWhereIdOrUUID, isOutdated, - parseAggregateResult + parseAggregateResult, + getFollowsSort, + createSafeIn } // ---------------------------------------------------------------------------