add offset pagination and permalinking to posts

This commit is contained in:
2025-05-19 09:33:30 +03:00
parent 85b1319c79
commit 6181701da6
2 changed files with 16 additions and 10 deletions

View File

@ -57,6 +57,7 @@ app:post("thread_create", "/create", function(self)
end)
app:get("thread", "/:slug", function(self)
local posts_per_page = 10
local thread = Threads:find({
slug = self.params.slug
})
@ -67,7 +68,8 @@ app:get("thread", "/:slug", function(self)
local post_count = Posts:count(db.clause({
thread_id = thread.id
}))
local pages = math.floor(post_count / 20)
self.pages = math.ceil(post_count / posts_per_page)
self.page = tonumber(self.params.page) or 1
local posts = db.query([[
SELECT
posts.id, post_history.content, users.username, avatars.file_path AS avatar_path
@ -80,15 +82,14 @@ app:get("thread", "/:slug", function(self)
LEFT JOIN
avatars ON users.avatar_id = avatars.id
WHERE
posts.thread_id = ? and posts.id > ?
posts.thread_id = ?
ORDER BY
posts.created_at ASC
LIMIT 20
]], thread.id, tonumber(self.params.cursor or 0))
LIMIT 20 OFFSET ?
]], thread.id, (self.page - 1) * posts_per_page)
self.topic = Topics:find(thread.topic_id)
self.user = util.get_logged_in_user_or_transient(self)
self.posts = posts
self.next_cursor = #posts > 0 and posts[#posts].id or nil
return {render = "threads.thread"}
end)