huge blog refactor

tidying up data structures; improvements to blog admin UI/UX, etc.
This commit is contained in:
ari melody 2025-11-08 12:54:31 +00:00
parent eaa2f6587d
commit 0c2aaa0b38
Signed by: ari
GPG key ID: CF99829C92678188
18 changed files with 432 additions and 239 deletions

View file

@ -29,38 +29,23 @@ func ServeAllBlogs(app *model.AppState) http.Handler {
}
type (
BlogAuthor struct {
ID string `json:"id"`
Avatar string `json:"avatar"`
}
BlogPost struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Author BlogAuthor `json:"author"`
CreatedAt time.Time `json:"created_at"`
ShortBlogPost struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Author *model.BlogAuthor `json:"author"`
PublishDate time.Time `json:"publish_date"`
}
)
resPosts := []*BlogPost{}
resPosts := []*ShortBlogPost{}
for _, post := range posts {
author, err := controller.GetAccountByID(app.DB, post.AuthorID)
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch author for blog post %s: %v\n", post.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
resPosts = append(resPosts, &BlogPost{
resPosts = append(resPosts, &ShortBlogPost{
ID: post.ID,
Title: post.Title,
Description: post.Description,
Author: BlogAuthor{
ID: author.Username,
Avatar: author.AvatarURL.String,
},
CreatedAt: post.CreatedAt,
Author: &post.Author,
PublishDate: post.PublishDate,
})
}
@ -90,11 +75,13 @@ func ServeBlog(app *model.AppState) http.Handler {
return
}
if !blog.Visible && !privileged {
if blog == nil || (!blog.Visible && !privileged) {
http.NotFound(w, r)
return
}
blog.Author.ID = blog.Author.DisplayName
err = json.NewEncoder(w).Encode(blog)
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: Failed to serve blog post %s: %v\n", blogID, err)
@ -122,11 +109,13 @@ func CreateBlog(app *model.AppState) http.Handler {
if blog.Title == "" { blog.Title = blog.ID }
if !blog.CreatedAt.Equal(time.Unix(0, 0)) {
blog.CreatedAt = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC)
if blog.PublishDate.Equal(time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)) {
blog.PublishDate = time.Date(
time.Now().Year(), time.Now().Month(), time.Now().Day(),
time.Now().Hour(), time.Now().Minute(), 0, 0, time.UTC)
}
blog.AuthorID = session.Account.ID
blog.Author.ID = session.Account.ID
err = controller.CreateBlogPost(app.DB, &blog)
if err != nil {
@ -134,18 +123,21 @@ func CreateBlog(app *model.AppState) http.Handler {
http.Error(w, fmt.Sprintf("Post %s already exists", blog.ID), http.StatusBadRequest)
return
}
fmt.Printf("WARN: Failed to create blog %s: %s\n", blog.ID, err)
fmt.Printf("WARN: Failed to create blog %s: %v\n", blog.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
app.Log.Info(log.TYPE_BLOG, "Blog post \"%s\" created by \"%s\".", blog.ID, session.Account.Username)
blog.Author.ID = session.Account.Username
blog.Author.DisplayName = session.Account.Username
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
err = json.NewEncoder(w).Encode(blog)
if err != nil {
fmt.Printf("WARN: Blog post %s created, but failed to send JSON response: %s\n", blog.ID, err)
fmt.Printf("WARN: Blog post %s created, but failed to send JSON response: %v\n", blog.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
})
@ -167,20 +159,64 @@ func UpdateBlog(app *model.AppState) http.Handler {
return
}
err = json.NewDecoder(r.Body).Decode(blog)
type (
BlueskyRecord struct {
ActorID string `json:"actor"`
RecordID string `json:"record"`
}
FediverseStatus struct {
AccountID string `json:"account"`
StatusID string `json:"status"`
}
UpdatedBlog struct {
Title string `json:"title"`
PublishDate time.Time `json:"publish_date"`
Description string `json:"description"`
Markdown string `json:"markdown"`
Bluesky BlueskyRecord `json:"bluesky"`
Fediverse FediverseStatus `json:"fediverse"`
Visible bool `json:"visible"`
}
)
var updatedBlog UpdatedBlog
err = json.NewDecoder(r.Body).Decode(&updatedBlog)
if err != nil {
fmt.Printf("WARN: Failed to update blog %s: %s\n", blog.ID, err)
fmt.Printf("WARN: Failed to update blog %s: %v\n", blog.ID, err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
blog.Title = updatedBlog.Title
blog.PublishDate = updatedBlog.PublishDate
blog.Description = updatedBlog.Description
blog.Markdown = updatedBlog.Markdown
if len(updatedBlog.Bluesky.ActorID) > 0 && len(updatedBlog.Bluesky.RecordID) > 0 {
blog.Bluesky = &model.BlueskyRecord{
ActorDID: updatedBlog.Bluesky.ActorID,
RecordID: updatedBlog.Bluesky.RecordID,
}
} else {
blog.Bluesky = nil
}
if len(updatedBlog.Fediverse.AccountID) > 0 && len(updatedBlog.Fediverse.StatusID) > 0 {
blog.Fediverse = &model.FediverseActivity{
AccountID: updatedBlog.Fediverse.AccountID,
StatusID: updatedBlog.Fediverse.StatusID,
}
} else {
blog.Fediverse = nil
}
blog.Visible = updatedBlog.Visible
err = controller.UpdateBlogPost(app.DB, blogID, blog)
if err != nil {
if strings.Contains(err.Error(), "no rows") {
http.NotFound(w, r)
return
}
fmt.Printf("WARN: Failed to update release %s: %s\n", blogID, err)
fmt.Printf("WARN: Failed to update release %s: %v\n", blogID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
@ -190,7 +226,7 @@ func UpdateBlog(app *model.AppState) http.Handler {
w.WriteHeader(http.StatusCreated)
err = json.NewEncoder(w).Encode(blog)
if err != nil {
fmt.Printf("WARN: Blog post %s created, but failed to send JSON response: %s\n", blog.ID, err)
fmt.Printf("WARN: Blog post %s updated, but failed to send JSON response: %v\n", blog.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
})
@ -201,17 +237,32 @@ func DeleteBlog(app *model.AppState) http.Handler {
session := r.Context().Value("session").(*model.Session)
blogID := r.PathValue("id")
rowsAffected, err := controller.DeleteBlogPost(app.DB, blogID)
blog, err := controller.GetBlogPost(app.DB, blogID)
if err != nil {
fmt.Printf("WARN: Failed to delete blog post %s: %s\n", blogID, err)
fmt.Printf("WARN: Failed to fetch blog post %s: %v\n", blogID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if rowsAffected == 0 {
if blog == nil {
http.NotFound(w, r)
return
}
err = controller.DeleteBlogPost(app.DB, blogID)
if err != nil {
fmt.Printf("WARN: Failed to delete blog post %s: %v\n", blogID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
app.Log.Info(log.TYPE_BLOG, "Blog post \"%s\" deleted by \"%s\".", blogID, session.Account.Username)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
err = json.NewEncoder(w).Encode(blog)
if err != nil {
fmt.Printf("WARN: Blog post %s deleted, but failed to send JSON response: %v\n", blog.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
})
}