From 8e7b167bc255cc3e4b4ae5b37ff79c0a31061454 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Wed, 21 May 2025 17:34:02 +0300 Subject: [PATCH] clamp page query param in thread view --- apps/threads.lua | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/apps/threads.lua b/apps/threads.lua index 417f8ad..343011b 100644 --- a/apps/threads.lua +++ b/apps/threads.lua @@ -69,6 +69,11 @@ app:get("thread", "/:slug", function(self) end self.thread = thread + local post_count = Posts:count(db.clause({ + thread_id = thread.id + })) + self.pages = math.ceil(post_count / POSTS_PER_PAGE) + if self.params.after then local after_id = tonumber(self.params.after) local post_position = Posts:count(db.clause({ @@ -77,13 +82,9 @@ app:get("thread", "/:slug", function(self) })) self.page = math.floor((post_position - 1) / POSTS_PER_PAGE) + 1 else - self.page = tonumber(self.params.page) or 1 + self.page = math.max(1, math.min(tonumber(self.params.page) or 1, self.pages)) end - local post_count = Posts:count(db.clause({ - thread_id = thread.id - })) - self.pages = math.ceil(post_count / POSTS_PER_PAGE) -- self.page = math.max(1, math.min(self.page, self.pages)) local posts = db.query([[ SELECT