From 6d609f1fae18e964572f1a9bbf0b0d5fe2c4c01c Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Sat, 11 Dec 2021 16:18:58 -0600 Subject: [PATCH] Sort blog index correctly --- src/hmndata/threads_and_posts_helper.go | 9 +++++++-- src/website/blogs.go | 9 +++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/hmndata/threads_and_posts_helper.go b/src/hmndata/threads_and_posts_helper.go index 052cc849..8011b86d 100644 --- a/src/hmndata/threads_and_posts_helper.go +++ b/src/hmndata/threads_and_posts_helper.go @@ -29,7 +29,8 @@ type ThreadsQuery struct { ThreadIDs []int // Ignored when using FetchThread or CountThreads. - Limit, Offset int // if empty, no pagination + Limit, Offset int // if empty, no pagination + OrderByCreated bool // defaults to order by last updated } type ThreadAndStuff struct { @@ -127,7 +128,11 @@ func FetchThreads( currentUserID, ) } - qb.Add(`ORDER BY last_post.postdate DESC`) + if q.OrderByCreated { + qb.Add(`ORDER BY first_post.postdate DESC`) + } else { + qb.Add(`ORDER BY last_post.postdate DESC`) + } if q.Limit > 0 { qb.Add(`LIMIT $? OFFSET $?`, q.Limit, q.Offset) } diff --git a/src/website/blogs.go b/src/website/blogs.go index 6164f7de..70d0af31 100644 --- a/src/website/blogs.go +++ b/src/website/blogs.go @@ -51,10 +51,11 @@ func BlogIndex(c *RequestContext) ResponseData { } threads, err := hmndata.FetchThreads(c.Context(), c.Conn, c.CurrentUser, hmndata.ThreadsQuery{ - ProjectIDs: []int{c.CurrentProject.ID}, - ThreadTypes: []models.ThreadType{models.ThreadTypeProjectBlogPost}, - Limit: postsPerPage, - Offset: (page - 1) * postsPerPage, + ProjectIDs: []int{c.CurrentProject.ID}, + ThreadTypes: []models.ThreadType{models.ThreadTypeProjectBlogPost}, + Limit: postsPerPage, + Offset: (page - 1) * postsPerPage, + OrderByCreated: true, }) if err != nil { return c.ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to fetch blog posts for index"))