From 0214dea9bd6c2e41e7a95a473a804bc8165d7438 Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+github@gmail.com>
Date: Thu, 14 Oct 2021 14:04:10 +0200
Subject: [PATCH] fix ordering of blubber threads, fixes #319

---
 lib/classes/sidebar/BlubberThreadsWidget.php     |  1 +
 lib/models/BlubberThread.php                     | 11 +++++++----
 resources/vue/components/BlubberThreadWidget.vue |  6 +++++-
 3 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/lib/classes/sidebar/BlubberThreadsWidget.php b/lib/classes/sidebar/BlubberThreadsWidget.php
index ab0bab738b8..b02e027e2d1 100644
--- a/lib/classes/sidebar/BlubberThreadsWidget.php
+++ b/lib/classes/sidebar/BlubberThreadsWidget.php
@@ -40,6 +40,7 @@ class BlubberThreadsWidget extends SidebarWidget
                 'avatar' => $thread->getAvatar(),
                 'name' => $thread->getName(),
                 'timestamp' => (int) $thread->getLatestActivity(),
+                'mkdate' => (int) $thread->mkdate,
                 'unseen_comments' => $unseen_comments,
                 'notifications' => $thread->id === 'global' || ($thread->context_type === 'course' && !$GLOBALS['perm']->have_perm('admin')),
                 'followed' => $thread->isFollowedByUser(),
diff --git a/lib/models/BlubberThread.php b/lib/models/BlubberThread.php
index 7b0af06d919..cccaa3de928 100644
--- a/lib/models/BlubberThread.php
+++ b/lib/models/BlubberThread.php
@@ -279,7 +279,7 @@ class BlubberThread extends SimpleORMap implements PrivacyObject
                 ['olderthan' => $olderthan]
             );
         }
-        $query->orderBy("IFNULL(MAX(blubber_comments.mkdate), blubber_threads.mkdate) DESC");
+        $query->orderBy("MAX(blubber_comments.mkdate) DESC, blubber_threads.mkdate DESC");
         $query->limit($limit);
 
         $threads = $query->fetchAll(static::class);
@@ -291,7 +291,7 @@ class BlubberThread extends SimpleORMap implements PrivacyObject
         $since = 0;
         $olderthan = time();
         foreach ($upgraded_threads as $thread) {
-            $active_time = $thread->getLatestActivity();
+            $active_time = $thread->getLatestActivity(true);
             $since = max($since, $active_time);
             $olderthan = min($olderthan, $active_time);
         }
@@ -581,10 +581,13 @@ class BlubberThread extends SimpleORMap implements PrivacyObject
         return OpenGraph::extract($this['content']);
     }
 
-    public function getLatestActivity()
+    public function getLatestActivity(bool $include_mkdate = false)
     {
         $newest_comment = BlubberComment::findOneBySQL("thread_id = ? ORDER BY mkdate DESC", [$this->getId()]);
-        return $newest_comment ? $newest_comment['mkdate'] : $this['mkdate'];
+        if ($newest_comment) {
+            return $newest_comment->mkdate;
+        }
+        return $include_mkdate ? $this->mkdate : null;
     }
 
     public function getURL()
diff --git a/resources/vue/components/BlubberThreadWidget.vue b/resources/vue/components/BlubberThreadWidget.vue
index 12514eb2039..2a2de1ca5f7 100644
--- a/resources/vue/components/BlubberThreadWidget.vue
+++ b/resources/vue/components/BlubberThreadWidget.vue
@@ -104,7 +104,11 @@
         },
         computed: {
             sortedThreads () {
-                return this.threads.sort((a, b) => b.timestamp - a.timestamp);
+                return this.threads.sort((a, b) => {
+                    return b.timestamp - a.timestamp
+                        || b.mkdate - a.mkdate
+                        || b.name.localeCompare(a.name);
+                });
             }
         }
     }
-- 
GitLab