]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-query-builder.ts
Fix video follow SQL
[github/Chocobozzz/PeerTube.git] / server / models / video / video-query-builder.ts
index 655abf19871f7e23991e59fc5f5feac6a1a80bf5..6211c9e565acee0c1b2500da83fe5c0f1f6f267e 100644 (file)
@@ -135,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) {
@@ -203,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
@@ -315,6 +323,10 @@ function buildListQuery (model: typeof Model, options: BuildVideosQueryOptions)
   if (options.isCount !== true) {
 
     if (exists(options.sort)) {
+      if (options.sort === '-originallyPublishedAt' || options.sort === 'originallyPublishedAt') {
+        attributes.push('COALESCE("video"."originallyPublishedAt", "video"."publishedAt") AS "publishedAtForOrder"')
+      }
+
       order = buildOrder(model, options.sort)
       suffix += `${order} `
     }
@@ -359,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 {