add offset pagination and permalinking to posts
This commit is contained in:
parent
85b1319c79
commit
6181701da6
@ -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)
|
||||
|
||||
|
@ -1,10 +1,11 @@
|
||||
<h1><%= thread.title %></h1>
|
||||
<p>Posted under <a href="<%= url_for("topic", {slug = topic.slug}) %>"><%= topic.name %></a>
|
||||
<% for _, post in ipairs(posts) do %>
|
||||
<div>
|
||||
<div id="post-<%= post.id %>">
|
||||
<img src="<%= post.avatar_path or "/avatars/default.webp" %>"><br>
|
||||
<a href="<%= url_for("user", {username = post.username}) %>"><%= post.username %></a>
|
||||
<div><p><%- post.content %></p></div>
|
||||
<a href="#post-<%= post.id %>">permalink</a>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
@ -15,8 +16,12 @@
|
||||
<input type="submit" value="Reply">
|
||||
</form>
|
||||
<% end %>
|
||||
<% if next_cursor then %>
|
||||
<a href="<%= url_for('thread', {slug = thread.slug}, {cursor = next_cursor}) %>">
|
||||
Older posts →
|
||||
</a>
|
||||
<span>
|
||||
<% for i = 1, math.max(pages, 1) do %>
|
||||
<% if i == page then %>
|
||||
<%= tostring(i)%>
|
||||
<% else %>
|
||||
<a href="?page=<%= i %>"><%= tostring(i)%></a>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</span>
|
||||
|
Loading…
Reference in New Issue
Block a user