From e434103fc130419a0571ca79ff90e7e7374845f6 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Sun, 14 Feb 2021 14:15:01 +0100 Subject: [PATCH] Allow comments on comments --- comments.go | 21 +++++++++------------ go.mod | 3 ++- go.sum | 6 ++++-- render.go | 16 ++++++++-------- templates/comment.gohtml | 5 ++++- templates/interactions.gohtml | 10 ++++------ 6 files changed, 31 insertions(+), 30 deletions(-) diff --git a/comments.go b/comments.go index d26e58e..bb09dd6 100644 --- a/comments.go +++ b/comments.go @@ -4,7 +4,9 @@ import ( "database/sql" "fmt" "net/http" + "net/url" "strconv" + "strings" "github.com/go-chi/chi" "github.com/microcosm-cc/bluemonday" @@ -41,6 +43,7 @@ func serveComment(blog string) func(http.ResponseWriter, *http.Request) { w.Header().Set("X-Robots-Tag", "noindex") render(w, templateComment, &renderData{ BlogString: blog, + Canonical: appConfig.Server.PublicAddress + appConfig.Blogs[blog].getRelativePath(fmt.Sprintf("/comment/%d", id)), Data: comment, }) } @@ -93,22 +96,16 @@ func checkCommentTarget(w http.ResponseWriter, r *http.Request) string { if target == "" { serveError(w, r, "No target specified", http.StatusBadRequest) return "" + } else if !strings.HasPrefix(target, appConfig.Server.PublicAddress) { + serveError(w, r, "Bad target", http.StatusBadRequest) + return "" } - postExists := 0 - row, err := appDbQueryRow("select exists(select 1 from posts where path = @path)", sql.Named("path", target)) + targetURL, err := url.Parse(target) if err != nil { - serveError(w, r, err.Error(), http.StatusInternalServerError) + serveError(w, r, err.Error(), http.StatusBadRequest) return "" } - if err = row.Scan(&postExists); err != nil { - serveError(w, r, err.Error(), http.StatusInternalServerError) - return "" - } - if postExists != 1 { - serveError(w, r, "Post does not exist", http.StatusBadRequest) - return "" - } - return target + return targetURL.Path } func commentsAdmin(w http.ResponseWriter, r *http.Request) { diff --git a/go.mod b/go.mod index cac5c8d..8fa0e33 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/caddyserver/certmagic v0.12.0 github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f github.com/dgrijalva/jwt-go v3.2.0+incompatible - github.com/go-chi/chi v1.5.1 + github.com/go-chi/chi v1.5.2 github.com/go-fed/httpsig v1.1.0 github.com/go-sql-driver/mysql v1.5.0 // indirect github.com/gofrs/flock v0.8.0 // indirect @@ -44,6 +44,7 @@ require ( github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/viper v1.7.1 github.com/tdewolff/minify/v2 v2.9.12 + github.com/tdewolff/parse/v2 v2.5.10 // indirect github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 github.com/vcraescu/go-paginator v1.0.1-0.20201114172518-2cfc59fe05c2 github.com/yuin/goldmark v1.3.2 diff --git a/go.sum b/go.sum index 9749fef..c5f80a9 100644 --- a/go.sum +++ b/go.sum @@ -66,8 +66,8 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= -github.com/go-chi/chi v1.5.1 h1:kfTK3Cxd/dkMu/rKs5ZceWYp+t5CtiE7vmaTv3LjC6w= -github.com/go-chi/chi v1.5.1/go.mod h1:REp24E+25iKvxgeTfHmdUoL5x15kBiDBlnIl5bCwe2k= +github.com/go-chi/chi v1.5.2 h1:YcLIBANL4OTaAOcTdp//sskGa0yGACQMCtbnr7YEn0Q= +github.com/go-chi/chi v1.5.2/go.mod h1:REp24E+25iKvxgeTfHmdUoL5x15kBiDBlnIl5bCwe2k= github.com/go-fed/httpsig v1.1.0 h1:9M+hb0jkEICD8/cAiNqEB66R87tTINszBRTjwjQzWcI= github.com/go-fed/httpsig v1.1.0/go.mod h1:RCMrTZvN1bJYtofsG4rd5NaO5obxQ5xBkdiS7xsT7bM= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= @@ -288,6 +288,8 @@ github.com/tdewolff/minify/v2 v2.9.12 h1:BKnbg3kcAuTLAhDeF3vSUxjvyj5lBRxaBvgxGCV github.com/tdewolff/minify/v2 v2.9.12/go.mod h1:yuntVVAFuGyi9VmiRoUqAYEQnFCGO929ytj2ITMZuB8= github.com/tdewolff/parse/v2 v2.5.9 h1:9wCXRT3OcgYDNatgU+HUTOoGhE9WcnY5UxLNoJUe1yw= github.com/tdewolff/parse/v2 v2.5.9/go.mod h1:WzaJpRSbwq++EIQHYIRTpbYKNA3gn9it1Ik++q4zyho= +github.com/tdewolff/parse/v2 v2.5.10 h1:vj35n+ljq8LuYUx436s4qB18wuwP7thrLv+t1syE39M= +github.com/tdewolff/parse/v2 v2.5.10/go.mod h1:WzaJpRSbwq++EIQHYIRTpbYKNA3gn9it1Ik++q4zyho= github.com/tdewolff/test v1.0.6 h1:76mzYJQ83Op284kMT+63iCNCI7NEERsIN8dLM+RiKr4= github.com/tdewolff/test v1.0.6/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= diff --git a/render.go b/render.go index 2c92f87..59f9844 100644 --- a/render.go +++ b/render.go @@ -86,14 +86,6 @@ func initRendering() error { "translations": func(p *post) []*post { return p.translations() }, - "postmentions": func(p *post) []*mention { - mentions, _ := getWebmentions(&webmentionsRequestConfig{ - target: p.fullURL(), - status: webmentionStatusApproved, - asc: true, - }) - return mentions - }, "shorturl": func(p *post) string { return p.shortURL() }, @@ -208,6 +200,14 @@ func initRendering() error { "commentsenabled": func(blog *configBlog) bool { return blog.Comments != nil && blog.Comments.Enabled }, + "mentions": func(absolute string) []*mention { + mentions, _ := getWebmentions(&webmentionsRequestConfig{ + target: absolute, + status: webmentionStatusApproved, + asc: true, + }) + return mentions + }, } templates = map[string]*template.Template{} diff --git a/templates/comment.gohtml b/templates/comment.gohtml index d2951a3..92fe807 100644 --- a/templates/comment.gohtml +++ b/templates/comment.gohtml @@ -1,5 +1,5 @@ {{ define "title" }} - {{ string .Blog.Lang "acommentby" }} {{ .Data.Name }} - {{ .Blog.Title }} + {{ string .Blog.Lang "acommentby" }} {{ .Data.Name }} {{ end }} {{ define "main" }} @@ -13,6 +13,9 @@ {{ html .Data.Comment }} + {{ if commentsenabled .Blog }} + {{ include "interactions" . }} + {{ end }} {{ end }} {{ define "comment" }} diff --git a/templates/interactions.gohtml b/templates/interactions.gohtml index dfba404..b57e954 100644 --- a/templates/interactions.gohtml +++ b/templates/interactions.gohtml @@ -1,11 +1,9 @@ {{ define "interactions" }}
{{ string .Blog.Lang "interactions" }} - {{ $postperma := ( absolute .Data.Path ) }} - {{ $mentions := ( postmentions .Data ) }} - {{ if $mentions }} + {{ with ( mentions .Canonical ) }}