From b56ab2522c87813302a613a0e0107407a3fe159a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Sun, 1 Jun 2025 14:04:45 +0300 Subject: [PATCH] sort threads in topic by activity by default (bump) and add setting --- apps/topics.lua | 24 ++++++++++++++---------- apps/users.lua | 4 +++- views/user/settings.etlua | 5 +++++ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/apps/topics.lua b/apps/topics.lua index 51cd75b..fadf4a9 100644 --- a/apps/topics.lua +++ b/apps/topics.lua @@ -94,11 +94,15 @@ app:get("topic", "/:slug", function(self) topic_id = topic.id })) self.topic = topic - - self.pages = math.max(math.ceil(threads_count / THREADS_PER_PAGE), 1) - self.page = math.max(1, math.min(tonumber(self.params.page) or 1, self.pages)) - -- self.threads_list = db.query("SELECT * FROM threads WHERE topic_id = ? ORDER BY is_stickied DESC, created_at DESC", topic.id) - self.threads_list = db.query([[ + local sort_by = self.session.sort_by or "activity" + local order_clause = "" + if sort_by == "thread" then + order_clause = "ORDER BY threads.is_stickied DESC, threads.created_at DESC" + else + order_clause = "ORDER BY threads.is_stickied DESC, latest_post_created_at DESC" + end + + local query = [[ SELECT threads.title, threads.slug, threads.created_at, threads.is_locked, threads.is_stickied, users.username AS started_by, @@ -126,11 +130,11 @@ app:get("topic", "/:slug", function(self) users u ON u.id = posts.user_id WHERE threads.topic_id = ? - ORDER BY - threads.is_stickied DESC, - threads.created_at DESC - LIMIT ? OFFSET ? - ]], topic.id, THREADS_PER_PAGE, (self.page - 1) * THREADS_PER_PAGE) + ]] .. order_clause .. " LIMIT ? OFFSET ?" + + self.pages = math.max(math.ceil(threads_count / THREADS_PER_PAGE), 1) + self.page = math.max(1, math.min(tonumber(self.params.page) or 1, self.pages)) + self.threads_list = db.query(query, topic.id, THREADS_PER_PAGE, (self.page - 1) * THREADS_PER_PAGE) local user = util.get_logged_in_user_or_transient(self) self.me = user diff --git a/apps/users.lua b/apps/users.lua index 0d5512a..12307c2 100644 --- a/apps/users.lua +++ b/apps/users.lua @@ -261,7 +261,9 @@ app:post("user_settings", "/:username/settings", function(self) if me.id ~= target_user.id then return {redirect_to = self:url_for("user", {username = self.params.username})} end - + if self.params.topic_sort_by == "activity" or self.params.topic_sort_by == "thread" then + self.session.sort_by = self.params.topic_sort_by + end local status = self.params.status:sub(1, 100) local original_sig = self.params.signature or "" local rendered_sig = babycode.to_html(original_sig, html_escape) diff --git a/views/user/settings.etlua b/views/user/settings.etlua index 8583095..84b28d4 100644 --- a/views/user/settings.etlua +++ b/views/user/settings.etlua @@ -15,6 +15,11 @@
+ +