sort threads in topic by activity by default (bump) and add setting

This commit is contained in:
Lera Elvoé 2025-06-01 14:04:45 +03:00
parent 24d6d7cebf
commit b56ab2522c
Signed by: yagich
SSH Key Fingerprint: SHA256:6xjGb6uA7lAVcULa7byPEN//rQ0wPoG+UzYVMfZnbvc
3 changed files with 22 additions and 11 deletions

View File

@ -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

View File

@ -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)

View File

@ -15,6 +15,11 @@
</div>
</form>
<form method="post" action="">
<label for="topic_sort_by">Sort threads by:</label>
<select id="topic_sort_by" name="topic_sort_by">
<option value="activity" <%= session.sort_by == "activity" and "selected" %>>Latest activity</option>
<option value="thread" <%= session.sort_by == "thread" and "selected" %>>Thread creation date</option>
</select>
<label for="status">Status</label>
<input type="text" id="status" name="status" value="<%= me.status %>" maxlength="70" placeholder="Will be shown under your username. Max 70 characters">
<label for="babycode-content">Signature</label><br>