]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/utils.ts
Merge branch 'release/3.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / models / utils.ts
index a06d578312197e7d0b6e9a8d2cf00e6591fbf864..e27625bc862c8a20504b0e12c0224d9539c598b3 100644 (file)
@@ -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
     ]
   }
@@ -113,7 +120,8 @@ function throwIfNotValid (value: any, validator: (value: any) => boolean, fieldN
 function buildTrigramSearchIndex (indexName: string, attribute: string) {
   return {
     name: indexName,
-    fields: [ Sequelize.literal('lower(immutable_unaccent(' + attribute + '))') as any ],
+    // FIXME: gin_trgm_ops is not taken into account in Sequelize 6, so adding it ourselves in the literal function
+    fields: [ Sequelize.literal('lower(immutable_unaccent(' + attribute + ')) gin_trgm_ops') as any ],
     using: 'gin',
     operator: 'gin_trgm_ops'
   }
@@ -133,7 +141,7 @@ function buildBlockedAccountSQL (blockerIds: number[]) {
   const blockerIdsString = blockerIds.join(', ')
 
   return 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' +
-    ' UNION ALL ' +
+    ' UNION ' +
     'SELECT "account"."id" AS "id" FROM account INNER JOIN "actor" ON account."actorId" = actor.id ' +
     'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' +
     'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')'
@@ -186,11 +194,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(', ')
 }