huge blog refactor
tidying up data structures; improvements to blog admin UI/UX, etc.
This commit is contained in:
parent
eaa2f6587d
commit
0c2aaa0b38
18 changed files with 432 additions and 239 deletions
95
view/blog.go
95
view/blog.go
|
|
@ -6,7 +6,6 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"arimelody-web/controller"
|
||||
|
|
@ -19,18 +18,18 @@ import (
|
|||
)
|
||||
|
||||
type (
|
||||
BlogView struct {
|
||||
Collections []*BlogViewPostCollection
|
||||
blogView struct {
|
||||
Collections []*blogPostCollection
|
||||
}
|
||||
|
||||
BlogViewPostCollection struct {
|
||||
Name string
|
||||
Posts []*BlogPostView
|
||||
blogPostCollection struct {
|
||||
Year int
|
||||
Posts []*model.BlogPost
|
||||
}
|
||||
|
||||
BlogPostView struct {
|
||||
blogPostView struct {
|
||||
*model.BlogPost
|
||||
Author *model.Account
|
||||
BlogHTML template.HTML
|
||||
Comments []*model.ThreadViewPost
|
||||
Likes int
|
||||
Boosts int
|
||||
|
|
@ -39,6 +38,13 @@ type (
|
|||
}
|
||||
)
|
||||
|
||||
func (c *blogPostCollection) Clone() blogPostCollection {
|
||||
return blogPostCollection{
|
||||
Year: c.Year,
|
||||
Posts: slices.Clone(c.Posts),
|
||||
}
|
||||
}
|
||||
|
||||
var mdRenderer = html.NewRenderer(html.RendererOptions{
|
||||
Flags: html.CommonFlags | html.HrefTargetBlank,
|
||||
})
|
||||
|
|
@ -51,7 +57,7 @@ func BlogHandler(app *model.AppState) http.Handler {
|
|||
})
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
dbPosts, err := controller.GetBlogPosts(app.DB, true, -1, 0)
|
||||
posts, err := controller.GetBlogPosts(app.DB, true, -1, 0)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
|
|
@ -62,43 +68,33 @@ func BlogHandler(app *model.AppState) http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
collections := []*BlogViewPostCollection{}
|
||||
posts := []*BlogPostView{}
|
||||
collectionYear := 0
|
||||
for i, post := range dbPosts {
|
||||
author, err := controller.GetAccountByID(app.DB, post.AuthorID)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to retrieve author of blog %s: %v\n", post.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
collections := []*blogPostCollection{}
|
||||
collection := blogPostCollection{
|
||||
Posts: []*model.BlogPost{},
|
||||
Year: -1,
|
||||
}
|
||||
for i, post := range posts {
|
||||
if i == 0 {
|
||||
collectionYear = post.CreatedAt.Year()
|
||||
collection.Year = post.PublishDate.Year()
|
||||
}
|
||||
|
||||
if post.CreatedAt.Year() != collectionYear || i == len(dbPosts) - 1 {
|
||||
if i == len(dbPosts) - 1 {
|
||||
posts = append(posts, &BlogPostView{
|
||||
BlogPost: post,
|
||||
Author: author,
|
||||
})
|
||||
if post.PublishDate.Year() != collection.Year {
|
||||
clone := collection.Clone()
|
||||
collections = append(collections, &clone)
|
||||
collection = blogPostCollection{
|
||||
Year: post.PublishDate.Year(),
|
||||
Posts: []*model.BlogPost{},
|
||||
}
|
||||
postsCopy := slices.Clone(posts)
|
||||
collections = append(collections, &BlogViewPostCollection{
|
||||
Name: strconv.Itoa(collectionYear),
|
||||
Posts: postsCopy,
|
||||
})
|
||||
posts = []*BlogPostView{}
|
||||
collectionYear = post.CreatedAt.Year()
|
||||
}
|
||||
|
||||
posts = append(posts, &BlogPostView{
|
||||
BlogPost: post,
|
||||
Author: author,
|
||||
})
|
||||
collection.Posts = append(collection.Posts, post)
|
||||
|
||||
if i == len(posts) - 1 {
|
||||
collections = append(collections, &collection)
|
||||
}
|
||||
}
|
||||
|
||||
err = templates.BlogTemplate.Execute(w, BlogView{
|
||||
err = templates.BlogTemplate.Execute(w, blogView{
|
||||
Collections: collections,
|
||||
})
|
||||
if err != nil {
|
||||
|
|
@ -119,10 +115,14 @@ func ServeBlogPost(app *model.AppState, blogPostID string) http.Handler {
|
|||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch blog post Bluesky thread: %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch blog post %s: %v\n", blogPostID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if blog == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if !blog.Visible {
|
||||
session, err := controller.GetSessionFromRequest(app, r)
|
||||
|
|
@ -138,37 +138,32 @@ func ServeBlogPost(app *model.AppState, blogPostID string) http.Handler {
|
|||
}
|
||||
}
|
||||
|
||||
author, err := controller.GetAccountByID(app.DB, blog.AuthorID)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to retrieve author of blog %s: %v\n", blog.ID, err)
|
||||
}
|
||||
|
||||
// blog.Markdown += " <i class=\"end-mark\"></i>"
|
||||
|
||||
mdParser := parser.NewWithExtensions(parser.CommonExtensions | parser.AutoHeadingIDs)
|
||||
md := mdParser.Parse([]byte(blog.Markdown))
|
||||
blog.HTML = template.HTML(markdown.Render(md, mdRenderer))
|
||||
blogHTML := template.HTML(markdown.Render(md, mdRenderer))
|
||||
|
||||
comments := []*model.ThreadViewPost{}
|
||||
likeCount := 0
|
||||
boostCount := 0
|
||||
var blueskyURL string
|
||||
var blueskyPost *model.ThreadViewPost
|
||||
if blog.BlueskyActorID != nil && blog.BlueskyPostID != nil {
|
||||
blueskyPost, err = controller.FetchThreadViewPost(*blog.BlueskyActorID, *blog.BlueskyPostID)
|
||||
if blog.Bluesky != nil {
|
||||
blueskyPost, err = controller.FetchThreadViewPost(blog.Bluesky.ActorDID, blog.Bluesky.RecordID)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch blog post Bluesky thread: %v\n", err)
|
||||
} else {
|
||||
comments = append(comments, blueskyPost.Replies...)
|
||||
likeCount += blueskyPost.Post.LikeCount
|
||||
boostCount += blueskyPost.Post.RepostCount
|
||||
blueskyURL = fmt.Sprintf("https://bsky.app/profile/%s/post/%s", blueskyPost.Post.Author.Handle, *blog.BlueskyPostID)
|
||||
blueskyURL = fmt.Sprintf("https://bsky.app/profile/%s/post/%s", blueskyPost.Post.Author.Handle, blog.Bluesky.RecordID)
|
||||
}
|
||||
}
|
||||
|
||||
err = templates.BlogPostTemplate.Execute(w, BlogPostView{
|
||||
err = templates.BlogPostTemplate.Execute(w, blogPostView{
|
||||
BlogPost: blog,
|
||||
Author: author,
|
||||
BlogHTML: blogHTML,
|
||||
Comments: comments,
|
||||
Likes: likeCount,
|
||||
Boosts: boostCount,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue