X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Futils.ts;h=6a109056ff964c621d2694edb961c8380e1a126b;hb=d324756edb836672f12284cd18e642a658b273d8;hp=5337ae75dc38f924b3a89974e9b28c930a735eb4;hpb=9d6b9d10ef8cbef39e89bc709285abffb0d8caa1;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/utils.ts b/server/models/utils.ts index 5337ae75d..6a109056f 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts @@ -1,5 +1,4 @@ -import { literal, Op, OrderItem } from 'sequelize' -import { Model, Sequelize } from 'sequelize-typescript' +import { literal, Op, OrderItem, Sequelize } from 'sequelize' import { Col } from 'sequelize/types/lib/utils' import validator from 'validator' @@ -54,6 +53,14 @@ function getVideoSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): Or [ Sequelize.col('VideoModel.views'), direction ], + lastSort + ] + } else if (field === 'publishedAt') { + return [ + [ 'ScheduleVideoUpdate', 'updateAt', direction + ' NULLS LAST' ], + + [ Sequelize.col('VideoModel.publishedAt'), direction ], + lastSort ] } @@ -67,8 +74,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 ] @@ -77,7 +84,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 ] } @@ -95,6 +102,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() @@ -187,11 +198,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(', ') }