From 4b13f99df82378b629aa50f82aa80fd99d8ceba4 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Thu, 23 Sep 2021 01:09:18 -0500 Subject: [PATCH] Add thread title editing --- src/website/blogs.go | 15 ++++++++++++++- src/website/forums.go | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/website/blogs.go b/src/website/blogs.go index 2ff59ad8..e535fd7f 100644 --- a/src/website/blogs.go +++ b/src/website/blogs.go @@ -332,7 +332,7 @@ func BlogPostEditSubmit(c *RequestContext) ResponseData { } defer tx.Rollback(c.Context()) - post, err := FetchThreadPost(c.Context(), c.Conn, c.CurrentUser, cd.ThreadID, cd.PostID, PostsQuery{ + post, err := FetchThreadPost(c.Context(), tx, c.CurrentUser, cd.ThreadID, cd.PostID, PostsQuery{ ProjectIDs: []int{c.CurrentProject.ID}, ThreadTypes: []models.ThreadType{models.ThreadTypeProjectBlogPost}, }) @@ -355,6 +355,19 @@ func BlogPostEditSubmit(c *RequestContext) ResponseData { CreatePostVersion(c.Context(), tx, post.Post.ID, unparsed, c.Req.Host, editReason, &c.CurrentUser.ID) + if title != "" { + _, err := tx.Exec(c.Context(), + ` + UPDATE handmade_thread SET title = $1 WHERE id = $2 + `, + title, + post.Thread.ID, + ) + if err != nil { + return c.ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to update thread title")) + } + } + err = tx.Commit(c.Context()) if err != nil { return c.ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to edit blog post")) diff --git a/src/website/forums.go b/src/website/forums.go index 35debc89..190d7822 100644 --- a/src/website/forums.go +++ b/src/website/forums.go @@ -686,15 +686,42 @@ func ForumPostEditSubmit(c *RequestContext) ResponseData { } defer tx.Rollback(c.Context()) + post, err := FetchThreadPost(c.Context(), tx, c.CurrentUser, cd.ThreadID, cd.PostID, PostsQuery{ + ProjectIDs: []int{c.CurrentProject.ID}, + ThreadTypes: []models.ThreadType{models.ThreadTypeForumPost}, + }) + if errors.Is(err, db.NotFound) { + return FourOhFour(c) + } else if err != nil { + return c.ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to get forum post to submit edits")) + } + c.Req.ParseForm() + title := c.Req.Form.Get("title") unparsed := c.Req.Form.Get("body") editReason := c.Req.Form.Get("editreason") + if title != "" && post.Thread.FirstID != post.Post.ID { + return RejectRequest(c, "You can only edit the title by editing the first post.") + } if unparsed == "" { return RejectRequest(c, "You must provide a body for your post.") } CreatePostVersion(c.Context(), tx, cd.PostID, unparsed, c.Req.Host, editReason, &c.CurrentUser.ID) + if title != "" { + _, err := tx.Exec(c.Context(), + ` + UPDATE handmade_thread SET title = $1 WHERE id = $2 + `, + title, + post.Thread.ID, + ) + if err != nil { + return c.ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to update thread title")) + } + } + err = tx.Commit(c.Context()) if err != nil { return c.ErrorResponse(http.StatusInternalServerError, oops.New(err, "failed to edit forum post"))