diff --git a/apps/topics.lua b/apps/topics.lua index fadf4a9..2b49a0b 100644 --- a/apps/topics.lua +++ b/apps/topics.lua @@ -24,7 +24,7 @@ local ThreadCreateError = { app:get("all_topics", "", function(self) self.topic_list = db.query([[ SELECT - topics.name, topics.slug, topics.description, topics.is_locked, + topics.id, topics.name, topics.slug, topics.description, topics.is_locked, users.username AS latest_thread_username, threads.title AS latest_thread_title, threads.slug AS latest_thread_slug, @@ -43,6 +43,44 @@ app:get("all_topics", "", function(self) ORDER BY topics.sort_order ASC ]]) + + local active_threads_raw = db.query([[ + WITH ranked_threads AS ( + SELECT + threads.topic_id, threads.id AS thread_id, threads.title AS thread_title, threads.slug AS thread_slug, + posts.id AS post_id, posts.created_at AS post_created_at, + users.username, + ROW_NUMBER() OVER (PARTITION BY threads.topic_id ORDER BY posts.created_at DESC) AS rn + FROM + threads + JOIN + posts ON threads.id = posts.thread_id + LEFT JOIN + users ON posts.user_id = users.id + ) + SELECT + topic_id, + thread_id, thread_title, thread_slug, + post_id, post_created_at, + username + FROM + ranked_threads + WHERE + rn = 1 + ORDER BY + topic_id + ]]) + self.active_threads = {} + for _, thread in ipairs(active_threads_raw) do + self.active_threads[tonumber(thread.topic_id)] = { + thread_title = thread.thread_title, + thread_slug = thread.thread_slug, + post_id = thread.post_id, + username = thread.username, + post_created_at = thread.post_created_at, + } + end + self.me = util.get_logged_in_user_or_transient(self) return {render = "topics.topics"} end) diff --git a/views/topics/topics.etlua b/views/topics/topics.etlua index 95d3d35..bd11fcb 100644 --- a/views/topics/topics.etlua +++ b/views/topics/topics.etlua @@ -20,9 +20,15 @@ ><%= topic.name %> <%= topic.description %> <% if topic.latest_thread_username then %> - - Latest thread: "><%= topic.latest_thread_title %> by "><%= topic.latest_thread_username %> on <% render("views.common.timestamp", {timestamp = topic.latest_thread_created_at}) -%> - + + Latest thread: "><%= topic.latest_thread_title %> by "><%= topic.latest_thread_username %> on <% render("views.common.timestamp", {timestamp = topic.latest_thread_created_at}) -%> + + <% if active_threads[topic.id] then %> + <% local thread = active_threads[topic.id] %> + + Latest post in: "><%= thread.thread_title %> by "><%= thread.username %> at <% render("views.common.timestamp", {timestamp = thread.post_created_at}) -%> + + <% end %> <% else %> No threads yet. <% end %>