From 86d5367d8ec3f69867955bda5101a4e4122a85df Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Fri, 15 Mar 2019 14:12:38 -0700 Subject: [PATCH] Fix sticky blog posts not ordering correctly --- models/posts/index.go | 18 ++++++++++-------- models/posts/posts.go | 2 +- root/blog/entry.partial.gohtml | 5 +++++ root/css/blog-core.css | 3 +++ src/controllers/posts/posts.go | 17 ++++++++++++++--- 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/models/posts/index.go b/models/posts/index.go index 6b5ff02..33d5c10 100644 --- a/models/posts/index.go +++ b/models/posts/index.go @@ -53,14 +53,16 @@ func RebuildIndex() (*Index, error) { // Update a blog's entry in the index. func (idx *Index) Update(p *Post) error { idx.Posts[p.ID] = Post{ - ID: p.ID, - Title: p.Title, - Fragment: p.Fragment, - AuthorID: p.AuthorID, - Privacy: p.Privacy, - Tags: p.Tags, - Created: p.Created, - Updated: p.Updated, + ID: p.ID, + Title: p.Title, + Fragment: p.Fragment, + AuthorID: p.AuthorID, + Privacy: p.Privacy, + Sticky: p.Sticky, + EnableComments: p.EnableComments, + Tags: p.Tags, + Created: p.Created, + Updated: p.Updated, } idx.Fragments[p.Fragment] = p.ID err := DB.Commit("blog/index", idx) diff --git a/models/posts/posts.go b/models/posts/posts.go index 2346a16..625242e 100644 --- a/models/posts/posts.go +++ b/models/posts/posts.go @@ -27,7 +27,7 @@ type Post struct { ID int `json:"id"` Title string `json:"title"` Fragment string `json:"fragment"` - ContentType string `json:"contentType"` + ContentType string `json:"contentType,omitempty"` AuthorID int `json:"author"` Body string `json:"body,omitempty"` Privacy string `json:"privacy"` diff --git a/root/blog/entry.partial.gohtml b/root/blog/entry.partial.gohtml index 8aadab7..708f366 100644 --- a/root/blog/entry.partial.gohtml +++ b/root/blog/entry.partial.gohtml @@ -16,6 +16,11 @@ {{ else if eq $p.Privacy "unlisted" }} [unlisted] {{ end }} + + {{ if $p.Sticky }} + [pinned] + {{ end }} + {{ $p.Created.Format "January 2, 2006" }} diff --git a/root/css/blog-core.css b/root/css/blog-core.css index c88c2ee..2cf6be5 100644 --- a/root/css/blog-core.css +++ b/root/css/blog-core.css @@ -22,6 +22,9 @@ a.blog-title { .blog-meta .blog-draft { color: #909; } +.blog-meta .blog-sticky { + color: #F0F; +} /* Comment metadata line */ .comment-meta { diff --git a/src/controllers/posts/posts.go b/src/controllers/posts/posts.go index c7f7c62..2d18dd8 100644 --- a/src/controllers/posts/posts.go +++ b/src/controllers/posts/posts.go @@ -10,11 +10,11 @@ import ( "time" "github.com/gorilla/mux" + "github.com/kirsle/blog/models/posts" + "github.com/kirsle/blog/models/users" "github.com/kirsle/blog/src/log" "github.com/kirsle/blog/src/markdown" "github.com/kirsle/blog/src/middleware/auth" - "github.com/kirsle/blog/models/posts" - "github.com/kirsle/blog/models/users" "github.com/kirsle/blog/src/render" "github.com/kirsle/blog/src/responses" "github.com/kirsle/blog/src/types" @@ -91,6 +91,7 @@ func RecentPosts(r *http.Request, tag, privacy string) []posts.Post { // The set of blog posts to show. var pool []posts.Post + var sticky []posts.Post // sticky pinned posts on top for _, post := range idx.Posts { // Limiting by a specific privacy setting? (drafts or private only) if privacy != "" { @@ -130,10 +131,20 @@ func RecentPosts(r *http.Request, tag, privacy string) []posts.Post { } } - pool = append(pool, post) + // Group them by sticky vs. not + if post.Sticky { + sticky = append(sticky, post) + } else { + pool = append(pool, post) + } } sort.Sort(sort.Reverse(posts.ByUpdated(pool))) + if len(sticky) > 0 { + sort.Sort(sort.Reverse(posts.ByUpdated(sticky))) + pool = append(sticky, pool...) + } + return pool }