From 4407f0aae168d787014aa43f903f90d94f14109e Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Tue, 6 Dec 2022 20:02:03 +0100 Subject: [PATCH] Fix comment routing --- comments.go | 5 +++-- commentsAdmin.go | 2 ++ httpRouters.go | 4 ++-- ui.go | 4 ++-- uiComponents.go | 2 +- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/comments.go b/comments.go index 32534b4..42a213a 100644 --- a/comments.go +++ b/comments.go @@ -3,6 +3,7 @@ package main import ( "database/sql" "errors" + "fmt" "net/http" "net/url" "path" @@ -101,7 +102,7 @@ func (a *goBlog) createComment(bc *configBlog, target, comment, name, website, o if commentID, err := result.LastInsertId(); err != nil { return "", http.StatusInternalServerError, errors.New("failed to save comment to database") } else { - commentAddress := bc.getRelativePath(path.Join(commentPath, strconv.Itoa(int(commentID)))) + commentAddress := bc.getRelativePath(fmt.Sprintf("%s/%d", commentPath, commentID)) // Send webmention _ = a.createWebmention(a.getFullAddress(commentAddress), a.getFullAddress(target)) // Return comment path @@ -111,7 +112,7 @@ func (a *goBlog) createComment(bc *configBlog, target, comment, name, website, o if err := a.db.updateComment(updateId, comment, name, website); err != nil { return "", http.StatusInternalServerError, errors.New("failed to update comment in database") } - commentAddress := bc.getRelativePath(path.Join(commentPath, strconv.Itoa(updateId))) + commentAddress := bc.getRelativePath(fmt.Sprintf("%s/%d", commentPath, updateId)) // Send webmention _ = a.createWebmention(a.getFullAddress(commentAddress), a.getFullAddress(target)) // Return comment path diff --git a/commentsAdmin.go b/commentsAdmin.go index 36033e1..7806e73 100644 --- a/commentsAdmin.go +++ b/commentsAdmin.go @@ -81,6 +81,8 @@ func (a *goBlog) commentsAdmin(w http.ResponseWriter, r *http.Request) { }) } +const commentDeleteSubPath = "/delete" + func (a *goBlog) commentsAdminDelete(w http.ResponseWriter, r *http.Request) { id, err := strconv.Atoi(r.FormValue("commentid")) if err != nil { diff --git a/httpRouters.go b/httpRouters.go index d36b226..fd6245f 100644 --- a/httpRouters.go +++ b/httpRouters.go @@ -383,9 +383,9 @@ func (a *goBlog) blogCommentsRouter(conf *configBlog) func(r chi.Router) { r.Use(a.authMiddleware) r.Get("/", a.commentsAdmin) r.Get(paginationPath, a.commentsAdmin) - r.Post("/delete", a.commentsAdminDelete) + r.Post(commentDeleteSubPath, a.commentsAdminDelete) r.Get(commentEditSubPath, a.serveCommentsEditor) - r.Post("/edit", a.serveCommentsEditor) + r.Post(commentEditSubPath, a.serveCommentsEditor) }) }) } diff --git a/ui.go b/ui.go index 2e43a4f..0964ecd 100644 --- a/ui.go +++ b/ui.go @@ -147,7 +147,7 @@ func (a *goBlog) renderBase(hb *htmlbuilder.HtmlBuilder, rd *renderData, title, } if a.commentsEnabledForBlog(rd.Blog) { hb.WriteUnescaped(" • ") - hb.WriteElementOpen("a", "href", "/comment") + hb.WriteElementOpen("a", "href", rd.Blog.getRelativePath(commentPath)) hb.WriteEscaped(a.ts.GetTemplateStringVariant(rd.Blog.Lang, "comments")) hb.WriteElementClose("a") } @@ -1271,7 +1271,7 @@ func (a *goBlog) renderCommentsAdmin(hb *htmlbuilder.HtmlBuilder, rd *renderData hb.WriteUnescaped(c.Comment) hb.WriteElementClose("p") // Delete form - hb.WriteElementOpen("form", "class", "actions", "method", "post", "action", rd.Blog.getRelativePath("/comment/delete")) + hb.WriteElementOpen("form", "class", "actions", "method", "post", "action", rd.Blog.getRelativePath(commentPath+commentDeleteSubPath)) hb.WriteElementOpen("input", "type", "hidden", "name", "commentid", "value", c.ID) hb.WriteElementOpen("input", "type", "submit", "value", a.ts.GetTemplateStringVariant(rd.Blog.Lang, "delete")) hb.WriteElementClose("form") diff --git a/uiComponents.go b/uiComponents.go index 764af1e..18d11f2 100644 --- a/uiComponents.go +++ b/uiComponents.go @@ -359,7 +359,7 @@ func (a *goBlog) renderInteractions(hb *htmlbuilder.HtmlBuilder, rd *renderData) hb.WriteElementOpen("input", "type", "submit", "value", a.ts.GetTemplateStringVariant(rd.Blog.Lang, "send")) hb.WriteElementClose("form") // Show form to create a new comment - hb.WriteElementOpen("form", "class", "fw p", "method", "post", "action", "/comment") + hb.WriteElementOpen("form", "class", "fw p", "method", "post", "action", rd.Blog.getRelativePath(commentPath)) hb.WriteElementOpen("input", "type", "hidden", "name", "target", "value", rd.Canonical) hb.WriteElementOpen("input", "type", "text", "name", "name", "placeholder", a.ts.GetTemplateStringVariant(rd.Blog.Lang, "nameopt")) hb.WriteElementOpen("input", "type", "url", "name", "website", "placeholder", a.ts.GetTemplateStringVariant(rd.Blog.Lang, "websiteopt"))