add thread view

This commit is contained in:
2025-05-20 12:30:41 +03:00
parent 9b689a08e2
commit 8609c33f00
10 changed files with 229 additions and 31 deletions

View File

@ -9,6 +9,8 @@ local Topics = models.Topics
local Threads = models.Threads
local Posts = models.Posts
local POSTS_PER_PAGE = 10
app:get("thread_create", "/create", function(self)
local user = util.get_logged_in_user(self)
if not user then
@ -57,7 +59,6 @@ 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
})
@ -68,11 +69,11 @@ app:get("thread", "/:slug", function(self)
local post_count = Posts:count(db.clause({
thread_id = thread.id
}))
self.pages = math.ceil(post_count / posts_per_page)
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
posts.id, posts.created_at, post_history.content, post_history.edited_at, users.username, users.status, avatars.file_path AS avatar_path
FROM
posts
JOIN
@ -86,7 +87,7 @@ app:get("thread", "/:slug", function(self)
ORDER BY
posts.created_at ASC
LIMIT ? OFFSET ?
]], thread.id, posts_per_page, (self.page - 1) * posts_per_page)
]], thread.id, POSTS_PER_PAGE, (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
@ -115,11 +116,15 @@ app:post("thread", "/:slug", function(self)
local post_content = self.params.post_content
local post = util.create_post(thread.id, user.id, post_content)
local post_count = Posts:count(db.clause({
thread_id = thread.id
}))
local last_page = math.ceil(post_count / POSTS_PER_PAGE)
if not post then
return {redirect_to = self:url_for("thread", {slug = thread.slug})}
return {redirect_to = self:url_for("thread", {slug = thread.slug}, {page = last_page}) .. "#latest-post"}
end
return {redirect_to = self:url_for("thread", {slug = thread.slug})}
return {redirect_to = self:url_for("thread", {slug = thread.slug}, {page = last_page}) .. "#latest-post"}
end)
return app