]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/utils.ts
Add index for recently added SQL query
[github/Chocobozzz/PeerTube.git] / server / models / utils.ts
index 24890f961e5d1d2738d0148b37d6561469cf445b..b53a52a053f9e86276a2632a3185ef2665044832 100644 (file)
@@ -1,9 +1,9 @@
 import { Model, Sequelize } from 'sequelize-typescript'
 import * as validator from 'validator'
 import { Col } from 'sequelize/types/lib/utils'
-import { OrderItem, literal } from 'sequelize'
+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,13 +51,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()
@@ -155,7 +168,7 @@ export {
   buildLocalAccountIdsIn,
   getSort,
   getVideoSort,
-  getSortOnModel,
+  getBlacklistSort,
   createSimilarityAttribute,
   throwIfNotValid,
   buildServerIdsFollowedBy,
@@ -163,6 +176,7 @@ export {
   buildWhereIdOrUUID,
   isOutdated,
   parseAggregateResult,
+  getFollowsSort,
   createSafeIn
 }