]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-query-builder.ts
Use raw sql for abuses
[github/Chocobozzz/PeerTube.git] / server / models / video / video-query-builder.ts
index 61f628c0683d4ed64ac3c6c4be2ce003a445bba3..466890364522889f5c9074f18961b0fedb5f6eb0 100644 (file)
@@ -1,8 +1,9 @@
 import { VideoFilter, VideoPrivacy, VideoState } from '@shared/models'
 import { buildDirectionAndField, createSafeIn } from '@server/models/utils'
 import { Model } from 'sequelize-typescript'
-import { MUserAccountId, MUserId } from '@server/typings/models'
+import { MUserAccountId, MUserId } from '@server/types/models'
 import validator from 'validator'
+import { exists } from '@server/helpers/custom-validators/misc'
 
 export type BuildVideosQueryOptions = {
   attributes?: string[]
@@ -134,12 +135,14 @@ function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions)
       '  EXISTS (' +
       '    SELECT 1 FROM "videoShare" ' +
       '    INNER JOIN "actorFollow" "actorFollowShare" ON "actorFollowShare"."targetActorId" = "videoShare"."actorId" ' +
-      '    AND "actorFollowShare"."actorId" = :followerActorId WHERE "videoShare"."videoId" = "video"."id"' +
+      '    AND "actorFollowShare"."actorId" = :followerActorId AND "actorFollowShare"."state" = \'accepted\' ' +
+      '    WHERE "videoShare"."videoId" = "video"."id"' +
       '  )' +
       '  OR' +
       '  EXISTS (' +
       '    SELECT 1 from "actorFollow" ' +
-      '    WHERE "actorFollow"."targetActorId" = "videoChannel"."actorId" AND "actorFollow"."actorId" = :followerActorId' +
+      '    WHERE "actorFollow"."targetActorId" = "videoChannel"."actorId" AND "actorFollow"."actorId" = :followerActorId ' +
+      '    AND "actorFollow"."state" = \'accepted\'' +
       '  )'
 
     if (options.includeLocalVideos) {
@@ -202,23 +205,29 @@ function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions)
   }
 
   if (options.languageOneOf) {
-    replacements.languageOneOf = options.languageOneOf.filter(l => l && l !== '_unknown')
-
-    let languagesQuery = '("video"."language" IN (:languageOneOf) OR '
+    const languages = options.languageOneOf.filter(l => l && l !== '_unknown')
+    const languagesQueryParts: string[] = []
+
+    if (languages.length !== 0) {
+      languagesQueryParts.push('"video"."language" IN (:languageOneOf)')
+      replacements.languageOneOf = languages
+
+      languagesQueryParts.push(
+        'EXISTS (' +
+        '  SELECT 1 FROM "videoCaption" WHERE "videoCaption"."language" ' +
+        '  IN (' + createSafeIn(model, languages) + ') AND ' +
+        '  "videoCaption"."videoId" = "video"."id"' +
+        ')'
+      )
+    }
 
     if (options.languageOneOf.includes('_unknown')) {
-      languagesQuery += '"video"."language" IS NULL OR '
+      languagesQueryParts.push('"video"."language" IS NULL')
     }
 
-    and.push(
-      languagesQuery +
-      '  EXISTS (' +
-      '    SELECT 1 FROM "videoCaption" WHERE "videoCaption"."language" ' +
-      '    IN (' + createSafeIn(model, options.languageOneOf) + ') AND ' +
-      '    "videoCaption"."videoId" = "video"."id"' +
-      '  )' +
-      ')'
-    )
+    if (languagesQueryParts.length !== 0) {
+      and.push('(' + languagesQueryParts.join(' OR ') + ')')
+    }
   }
 
   // We don't exclude results in this if so if we do a count we don't need to add this complex clauses
@@ -312,14 +321,25 @@ function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions)
   let suffix = ''
   let order = ''
   if (options.isCount !== true) {
-    const count = parseInt(options.count + '', 10)
-    const start = parseInt(options.start + '', 10)
 
-    order = buildOrder(model, options.sort)
+    if (exists(options.sort)) {
+      if (options.sort === '-originallyPublishedAt' || options.sort === 'originallyPublishedAt') {
+        attributes.push('COALESCE("video"."originallyPublishedAt", "video"."publishedAt") AS "publishedAtForOrder"')
+      }
+
+      order = buildOrder(options.sort)
+      suffix += `${order} `
+    }
 
-    suffix = order + ' ' +
-      'LIMIT ' + count + ' ' +
-      'OFFSET ' + start
+    if (exists(options.count)) {
+      const count = parseInt(options.count + '', 10)
+      suffix += `LIMIT ${count} `
+    }
+
+    if (exists(options.start)) {
+      const start = parseInt(options.start + '', 10)
+      suffix += `OFFSET ${start} `
+    }
   }
 
   const cteString = cte.length !== 0
@@ -337,7 +357,7 @@ function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions)
   return { query, replacements, order }
 }
 
-function buildOrder (model: typeof Model, value: string) {
+function buildOrder (value: string) {
   const { direction, field } = buildDirectionAndField(value)
   if (field.match(/^[a-zA-Z."]+$/) === null) throw new Error('Invalid sort column ' + field)
 
@@ -351,6 +371,8 @@ function buildOrder (model: typeof Model, value: string) {
 
   if (field.toLowerCase() === 'match') { // Search
     firstSort = '"similarity"'
+  } else if (field === 'originallyPublishedAt') {
+    firstSort = '"publishedAtForOrder"'
   } else if (field.includes('.')) {
     firstSort = field
   } else {