This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"plumber/internal/mail"
|
||||
"plumber/internal/store"
|
||||
@@ -20,6 +21,11 @@ import (
|
||||
// handleCreatePost creates either a root question or a reply. Replies are
|
||||
// limited to the root author and admins, and cannot be added to hidden threads.
|
||||
func (s *Server) handleCreatePost(w http.ResponseWriter, r *http.Request) {
|
||||
cleanup, ok := parsePostMutationForm(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
defer cleanup()
|
||||
if !s.requireCSRF(w, r) {
|
||||
return
|
||||
}
|
||||
@@ -37,6 +43,7 @@ func (s *Server) handleCreatePost(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
post := &store.Post{
|
||||
ID: uuid.NewString(),
|
||||
AuthorID: user.ID,
|
||||
Body: truncateRunes(body, 12000),
|
||||
}
|
||||
@@ -71,7 +78,14 @@ func (s *Server) handleCreatePost(w http.ResponseWriter, r *http.Request) {
|
||||
root = threadRoot
|
||||
}
|
||||
|
||||
images, newKeys, err := s.postImagesFromForm(r.Context(), r, post.ID, nil)
|
||||
if err != nil {
|
||||
writePostImageRequestError(w, err)
|
||||
return
|
||||
}
|
||||
post.Images = images
|
||||
if err := s.store.CreatePost(r.Context(), post); err != nil {
|
||||
s.deletePostImageObjects(newKeys)
|
||||
if errors.Is(err, store.ErrInvalidPost) {
|
||||
http.Error(w, "invalid post", http.StatusBadRequest)
|
||||
return
|
||||
@@ -149,6 +163,11 @@ func (s *Server) notifyPostReply(
|
||||
// handleEditPost updates only a post's body after verifying that the current
|
||||
// homeowner owns it or that an admin is editing an admin-authored post.
|
||||
func (s *Server) handleEditPost(w http.ResponseWriter, r *http.Request) {
|
||||
cleanup, ok := parsePostMutationForm(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
defer cleanup()
|
||||
if !s.requireCSRF(w, r) {
|
||||
return
|
||||
}
|
||||
@@ -177,8 +196,16 @@ func (s *Server) handleEditPost(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "post body required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
previousImages := append([]store.PostImage(nil), post.Images...)
|
||||
images, newKeys, err := s.postImagesFromForm(r.Context(), r, post.ID, previousImages)
|
||||
if err != nil {
|
||||
writePostImageRequestError(w, err)
|
||||
return
|
||||
}
|
||||
post.Body = truncateRunes(body, 12000)
|
||||
post.Images = images
|
||||
if err := s.store.UpdatePost(r.Context(), post); err != nil {
|
||||
s.deletePostImageObjects(newKeys)
|
||||
if errors.Is(err, store.ErrInvalidPost) {
|
||||
http.Error(w, "invalid post", http.StatusBadRequest)
|
||||
return
|
||||
@@ -190,6 +217,7 @@ func (s *Server) handleEditPost(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "could not save post", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
s.deletePostImageObjects(removedPostImageKeys(previousImages, images))
|
||||
http.Redirect(
|
||||
w,
|
||||
r,
|
||||
|
||||
Reference in New Issue
Block a user