Fix sticky blog posts not ordering correctly

bindata
Noah 2019-03-15 14:12:38 -07:00
parent dee7c8eb98
commit 86d5367d8e
5 changed files with 33 additions and 12 deletions

View File

@ -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)

View File

@ -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"`

View File

@ -16,6 +16,11 @@
{{ else if eq $p.Privacy "unlisted" }}
<span class="blog-unlisted">[unlisted]</span>
{{ end }}
{{ if $p.Sticky }}
<span class="blog-sticky">[pinned]</span>
{{ end }}
<span title="{{ $p.Created.Format "Jan 2 2006 @ 15:04:05 MST" }}">
{{ $p.Created.Format "January 2, 2006" }}
</span>

View File

@ -22,6 +22,9 @@ a.blog-title {
.blog-meta .blog-draft {
color: #909;
}
.blog-meta .blog-sticky {
color: #F0F;
}
/* Comment metadata line */
.comment-meta {

View File

@ -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
}