X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Futils.ts;h=70bfbdb8bfa4748dc770186c7a95b913454df570;hb=4b3145a7f8f5f1e24f9ea7874dee387204e65fcb;hp=ec51c66bfb81b9080f42963308c165af44c4a5fd;hpb=dc48fdbe68e9dd3a3a6028181e61d8595d98e654;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/utils.ts b/server/models/utils.ts index ec51c66bf..70bfbdb8b 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts @@ -1,6 +1,4 @@ -import { literal, Op, OrderItem } from 'sequelize' -import { Model, Sequelize } from 'sequelize-typescript' -import { Col } from 'sequelize/types/lib/utils' +import { literal, Op, OrderItem, Sequelize } from 'sequelize' import validator from 'validator' type SortType = { sortModel: string, sortValue: string } @@ -9,7 +7,7 @@ type SortType = { sortModel: string, sortValue: string } function getSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { const { direction, field } = buildDirectionAndField(value) - let finalField: string | Col + let finalField: string | ReturnType if (field.toLowerCase() === 'match') { // Search finalField = Sequelize.col('similarity') @@ -66,7 +64,7 @@ function getVideoSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): Or ] } - let finalField: string | Col + let finalField: string | ReturnType // Alias if (field.toLowerCase() === 'match') { // Search @@ -75,8 +73,8 @@ function getVideoSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): Or finalField = field } - const firstSort = typeof finalField === 'string' - ? finalField.split('.').concat([ direction ]) as any // FIXME: sequelize typings + const firstSort: OrderItem = typeof finalField === 'string' + ? finalField.split('.').concat([ direction ]) as OrderItem : [ finalField, direction ] return [ firstSort, lastSort ] @@ -85,7 +83,7 @@ function getVideoSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): Or function getBlacklistSort (model: any, value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] { const [ firstSort ] = getSort(value) - if (model) return [ [ literal(`"${model}.${firstSort[0]}" ${firstSort[1]}`) ], lastSort ] as any[] // FIXME: typings + if (model) return [ [ literal(`"${model}.${firstSort[0]}" ${firstSort[1]}`) ], lastSort ] as OrderItem[] return [ firstSort, lastSort ] } @@ -103,6 +101,10 @@ function getFollowsSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): } function isOutdated (model: { createdAt: Date, updatedAt: Date }, refreshInterval: number) { + if (!model.createdAt || !model.updatedAt) { + throw new Error('Miss createdAt & updatedAt attribuets to model') + } + const now = Date.now() const createdAtTime = model.createdAt.getTime() const updatedAtTime = model.updatedAt.getTime() @@ -179,7 +181,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) { @@ -195,11 +197,11 @@ function parseAggregateResult (result: any) { return total } -const createSafeIn = (model: typeof Model, stringArr: (string | number)[]) => { +function createSafeIn (sequelize: Sequelize, stringArr: (string | number)[]) { return stringArr.map(t => { return t === null ? null - : model.sequelize.escape('' + t) + : sequelize.escape('' + t) }).join(', ') } @@ -235,7 +237,8 @@ function searchAttribute (sourceField?: string, targetField?: string) { return { [targetField]: { - [Op.iLike]: `%${sourceField}%` + // FIXME: ts error + [Op.iLike as any]: `%${sourceField}%` } } }