256 lines
9.5 KiB
Go
256 lines
9.5 KiB
Go
package api
|
|
|
|
import (
|
|
"arimelody-web/controller"
|
|
"arimelody-web/log"
|
|
"arimelody-web/model"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func ServeAllBlogs(app *model.AppState) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
session := r.Context().Value("session").(*model.Session)
|
|
|
|
onlyVisible := true
|
|
if session != nil && session.Account != nil {
|
|
onlyVisible = false
|
|
}
|
|
|
|
posts, err := controller.GetBlogPosts(app.DB, onlyVisible, -1, 0)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch blog posts: %v\n", err)
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
type (
|
|
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 := []*ShortBlogPost{}
|
|
|
|
for _, post := range posts {
|
|
resPosts = append(resPosts, &ShortBlogPost{
|
|
ID: post.ID,
|
|
Title: post.Title,
|
|
Description: post.Description,
|
|
Author: &post.Author,
|
|
PublishDate: post.PublishDate,
|
|
})
|
|
}
|
|
|
|
err = json.NewEncoder(w).Encode(resPosts)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "WARN: Failed to serve blog posts: %v\n", err)
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
}
|
|
|
|
func ServeBlog(app *model.AppState) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
session := r.Context().Value("session").(*model.Session)
|
|
privileged := session != nil && session.Account != nil
|
|
blogID := r.PathValue("id")
|
|
|
|
blog, err := controller.GetBlogPost(app.DB, blogID)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch blog post %s: %v\n", blogID, err)
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
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)
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
}
|
|
|
|
func CreateBlog(app *model.AppState) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
session := r.Context().Value("session").(*model.Session)
|
|
|
|
var blog model.BlogPost
|
|
err := json.NewDecoder(r.Body).Decode(&blog)
|
|
if err != nil {
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if blog.ID == "" {
|
|
http.Error(w, "Post ID cannot be empty", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if blog.Title == "" { blog.Title = blog.ID }
|
|
|
|
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.Author.ID = session.Account.ID
|
|
|
|
err = controller.CreateBlogPost(app.DB, &blog)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "duplicate key") {
|
|
http.Error(w, fmt.Sprintf("Post %s already exists", blog.ID), http.StatusBadRequest)
|
|
return
|
|
}
|
|
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: %v\n", blog.ID, err)
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
}
|
|
})
|
|
}
|
|
|
|
func UpdateBlog(app *model.AppState) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
session := r.Context().Value("session").(*model.Session)
|
|
blogID := r.PathValue("id")
|
|
|
|
blog, err := controller.GetBlogPost(app.DB, blogID)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch blog post %s: %v\n", blogID, err)
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
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: %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 {
|
|
fmt.Printf("WARN: Failed to update release %s: %v\n", blogID, err)
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
}
|
|
|
|
app.Log.Info(log.TYPE_BLOG, "Blog post \"%s\" updated by \"%s\".", blog.ID, 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 updated, but failed to send JSON response: %v\n", blog.ID, err)
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
}
|
|
})
|
|
}
|
|
|
|
func DeleteBlog(app *model.AppState) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
session := r.Context().Value("session").(*model.Session)
|
|
blogID := r.PathValue("id")
|
|
|
|
blog, err := controller.GetBlogPost(app.DB, blogID)
|
|
if err != nil {
|
|
fmt.Printf("WARN: Failed to fetch blog post %s: %v\n", blogID, err)
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
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)
|
|
}
|
|
})
|
|
}
|