add latest post info to topics view
This commit is contained in:
		@@ -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)
 | 
			
		||||
 
 | 
			
		||||
@@ -20,9 +20,15 @@
 | 
			
		||||
        <a class="thread-title" href=<%= url_for("topic", {slug = topic.slug}) %>><%= topic.name %></a>
 | 
			
		||||
        <%= topic.description %>
 | 
			
		||||
        <% if topic.latest_thread_username then %>
 | 
			
		||||
        <span>
 | 
			
		||||
          Latest thread: <a href="<%= url_for("thread", {slug = topic.latest_thread_slug}) %>"><%= topic.latest_thread_title %></a> by <a href="<%= url_for("user", {username = topic.latest_thread_username}) %>"><%= topic.latest_thread_username %></a> on <% render("views.common.timestamp", {timestamp = topic.latest_thread_created_at}) -%>
 | 
			
		||||
        </span>
 | 
			
		||||
          <span>
 | 
			
		||||
            Latest thread: <a href="<%= url_for("thread", {slug = topic.latest_thread_slug}) %>"><%= topic.latest_thread_title %></a> by <a href="<%= url_for("user", {username = topic.latest_thread_username}) %>"><%= topic.latest_thread_username %></a> on <% render("views.common.timestamp", {timestamp = topic.latest_thread_created_at}) -%>
 | 
			
		||||
          </span>
 | 
			
		||||
          <% if active_threads[topic.id] then %>
 | 
			
		||||
            <% local thread = active_threads[topic.id] %>
 | 
			
		||||
            <span>
 | 
			
		||||
              Latest post in: <a href="<%= url_for("thread", {slug = thread.thread_slug}) %>"><%= thread.thread_title %></a> by <a href="<%= url_for("user", {username = thread.username}) %>"><%= thread.username %></a> at <a href="<%= get_post_url(thread.post_id) %>"><% render("views.common.timestamp", {timestamp = thread.post_created_at}) -%></a>
 | 
			
		||||
            </span>
 | 
			
		||||
          <% end %>
 | 
			
		||||
        <% else %>
 | 
			
		||||
        <i>No threads yet.</i>
 | 
			
		||||
        <% end %>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user