add offset pagination and permalinking to posts

This commit is contained in:
Lera Elvoé 2025-05-19 09:33:30 +03:00
parent 85b1319c79
commit 6181701da6
Signed by: yagich
SSH Key Fingerprint: SHA256:6xjGb6uA7lAVcULa7byPEN//rQ0wPoG+UzYVMfZnbvc
2 changed files with 16 additions and 10 deletions

View File

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

View File

@ -1,10 +1,11 @@
<h1><%= thread.title %></h1> <h1><%= thread.title %></h1>
<p>Posted under <a href="<%= url_for("topic", {slug = topic.slug}) %>"><%= topic.name %></a> <p>Posted under <a href="<%= url_for("topic", {slug = topic.slug}) %>"><%= topic.name %></a>
<% for _, post in ipairs(posts) do %> <% for _, post in ipairs(posts) do %>
<div> <div id="post-<%= post.id %>">
<img src="<%= post.avatar_path or "/avatars/default.webp" %>"><br> <img src="<%= post.avatar_path or "/avatars/default.webp" %>"><br>
<a href="<%= url_for("user", {username = post.username}) %>"><%= post.username %></a> <a href="<%= url_for("user", {username = post.username}) %>"><%= post.username %></a>
<div><p><%- post.content %></p></div> <div><p><%- post.content %></p></div>
<a href="#post-<%= post.id %>">permalink</a>
</div> </div>
<% end %> <% end %>
@ -15,8 +16,12 @@
<input type="submit" value="Reply"> <input type="submit" value="Reply">
</form> </form>
<% end %> <% end %>
<% if next_cursor then %> <span>
<a href="<%= url_for('thread', {slug = thread.slug}, {cursor = next_cursor}) %>"> <% for i = 1, math.max(pages, 1) do %>
Older posts → <% if i == page then %>
</a> <%= tostring(i)%>
<% else %>
<a href="?page=<%= i %>"><%= tostring(i)%></a>
<% end %>
<% end %> <% end %>
</span>