Fix sticky blog posts not ordering correctly

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

View File

@ -58,6 +58,8 @@ func (idx *Index) Update(p *Post) error {
Fragment: p.Fragment, Fragment: p.Fragment,
AuthorID: p.AuthorID, AuthorID: p.AuthorID,
Privacy: p.Privacy, Privacy: p.Privacy,
Sticky: p.Sticky,
EnableComments: p.EnableComments,
Tags: p.Tags, Tags: p.Tags,
Created: p.Created, Created: p.Created,
Updated: p.Updated, Updated: p.Updated,

View File

@ -27,7 +27,7 @@ type Post struct {
ID int `json:"id"` ID int `json:"id"`
Title string `json:"title"` Title string `json:"title"`
Fragment string `json:"fragment"` Fragment string `json:"fragment"`
ContentType string `json:"contentType"` ContentType string `json:"contentType,omitempty"`
AuthorID int `json:"author"` AuthorID int `json:"author"`
Body string `json:"body,omitempty"` Body string `json:"body,omitempty"`
Privacy string `json:"privacy"` Privacy string `json:"privacy"`

View File

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

View File

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

View File

@ -10,11 +10,11 @@ import (
"time" "time"
"github.com/gorilla/mux" "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/log"
"github.com/kirsle/blog/src/markdown" "github.com/kirsle/blog/src/markdown"
"github.com/kirsle/blog/src/middleware/auth" "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/render"
"github.com/kirsle/blog/src/responses" "github.com/kirsle/blog/src/responses"
"github.com/kirsle/blog/src/types" "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. // The set of blog posts to show.
var pool []posts.Post var pool []posts.Post
var sticky []posts.Post // sticky pinned posts on top
for _, post := range idx.Posts { for _, post := range idx.Posts {
// Limiting by a specific privacy setting? (drafts or private only) // Limiting by a specific privacy setting? (drafts or private only)
if privacy != "" { if privacy != "" {
@ -130,10 +131,20 @@ func RecentPosts(r *http.Request, tag, privacy string) []posts.Post {
} }
} }
// Group them by sticky vs. not
if post.Sticky {
sticky = append(sticky, post)
} else {
pool = append(pool, post) pool = append(pool, post)
} }
}
sort.Sort(sort.Reverse(posts.ByUpdated(pool))) sort.Sort(sort.Reverse(posts.ByUpdated(pool)))
if len(sticky) > 0 {
sort.Sort(sort.Reverse(posts.ByUpdated(sticky)))
pool = append(sticky, pool...)
}
return pool return pool
} }