From 0a835b5f6117c3430cbfe8f9ca0803275529005f Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Sun, 14 Aug 2022 11:05:23 +0200 Subject: [PATCH] Add and document post parameter to disable interactions (#21) --- comments.go | 10 ++++++++++ comments_test.go | 33 +++++++++++++++++++++++++++++++++ docs/usage.md | 10 +++++++++- go.mod | 2 +- go.sum | 4 ++-- render.go | 3 --- ui.go | 12 ++++++------ 7 files changed, 61 insertions(+), 13 deletions(-) diff --git a/comments.go b/comments.go index 5c7d838..7964bb1 100644 --- a/comments.go +++ b/comments.go @@ -146,3 +146,13 @@ func (db *database) deleteComment(id int) error { _, err := db.Exec("delete from comments where id = @id", sql.Named("id", id)) return err } + +func (a *goBlog) commentsEnabledForBlog(blog *configBlog) bool { + return blog.Comments != nil && blog.Comments.Enabled +} + +const commentsPostParam = "comments" + +func (a *goBlog) commentsEnabledForPost(post *post) bool { + return post != nil && a.commentsEnabledForBlog(a.getBlogFromPost(post)) && post.firstParameter(commentsPostParam) != "false" +} diff --git a/comments_test.go b/comments_test.go index 0459d81..816ac73 100644 --- a/comments_test.go +++ b/comments_test.go @@ -164,3 +164,36 @@ func Test_comments(t *testing.T) { }) } + +func Test_commentsEnabled(t *testing.T) { + app := &goBlog{ + cfg: createDefaultTestConfig(t), + } + err := app.initConfig(false) + require.NoError(t, err) + + assert.False(t, app.commentsEnabledForPost(&post{ + Blog: app.cfg.DefaultBlog, + })) + + app.cfg.Blogs[app.cfg.DefaultBlog].Comments = &configComments{ + Enabled: true, + } + + assert.True(t, app.commentsEnabledForPost(&post{ + Blog: app.cfg.DefaultBlog, + })) + assert.True(t, app.commentsEnabledForPost(&post{ + Blog: app.cfg.DefaultBlog, + Parameters: map[string][]string{ + "comments": {"true"}, + }, + })) + assert.False(t, app.commentsEnabledForPost(&post{ + Blog: app.cfg.DefaultBlog, + Parameters: map[string][]string{ + "comments": {"false"}, + }, + })) + +} diff --git a/docs/usage.md b/docs/usage.md index 062470f..5554835 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -47,4 +47,12 @@ GoBlog can be configured to provide a Tor Hidden Service. This is useful if you ## Reactions -It's possible to enable post reactions. GoBlog currently has a hardcoded list of reactions: "❤️", "👍", "👎", "😂" and "😱". If enabled, users can react to a post by clicking on the reaction button below the post. If you want to disable reactions for a single post, you can set the `reactions` parameter to `false` in the post's metadata. \ No newline at end of file +It's possible to enable post reactions. GoBlog currently has a hardcoded list of reactions: "❤️", "👍", "👎", "😂" and "😱". If enabled, users can react to a post by clicking on the reaction button below the post. If you want to disable reactions for a single post, you can set the `reactions` parameter to `false` in the post's metadata. + +## Comments and interactions + +GoBlog has a comment system. That can be enable using the configuration. See the `example-config.yml` file for how to configure it. + +All comments and interactions (Webmentions) have to be approved manually using the UI at `/webmention`. To completely delete a comment, delete the entry from the Webmention UI and also delete the comment from `/comment`. + +To disable showing comments and interactions on a single post, add the parameter `comments` with the value `false` to the post's metadata. \ No newline at end of file diff --git a/go.mod b/go.mod index f100d0f..d9dcfa2 100644 --- a/go.mod +++ b/go.mod @@ -59,7 +59,7 @@ require ( // master github.com/yuin/goldmark-emoji v1.0.2-0.20210607094911-0487583eca38 golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa - golang.org/x/net v0.0.0-20220811182439-13a9a731de15 + golang.org/x/net v0.0.0-20220812174116-3211cb980234 golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 golang.org/x/text v0.3.7 gopkg.in/yaml.v3 v3.0.1 diff --git a/go.sum b/go.sum index 9c81656..234e04d 100644 --- a/go.sum +++ b/go.sum @@ -619,8 +619,8 @@ golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qx golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211020060615-d418f374d309/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= -golang.org/x/net v0.0.0-20220811182439-13a9a731de15 h1:cik0bxZUSJVDyaHf1hZPSDsU8SZHGQZQMeueXCE7yBQ= -golang.org/x/net v0.0.0-20220811182439-13a9a731de15/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= +golang.org/x/net v0.0.0-20220812174116-3211cb980234 h1:RDqmgfe7SvlMWoqC3xwQ2blLO3fcWcxMa3eBLRdRW7E= +golang.org/x/net v0.0.0-20220812174116-3211cb980234/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= diff --git a/render.go b/render.go index 7aae7f4..0a812b2 100644 --- a/render.go +++ b/render.go @@ -15,7 +15,6 @@ type renderData struct { Blog *configBlog User *configUser Data any - CommentsEnabled bool WebmentionReceivingEnabled bool TorUsed bool EasterEgg bool @@ -79,8 +78,6 @@ func (a *goBlog) checkRenderData(r *http.Request, data *renderData) { if torUsed, ok := r.Context().Value(torUsedKey).(bool); ok && torUsed { data.TorUsed = true } - // Check if comments enabled - data.CommentsEnabled = data.Blog.Comments != nil && data.Blog.Comments.Enabled // Check if able to receive webmentions data.WebmentionReceivingEnabled = a.cfg.Webmention == nil || !a.cfg.Webmention.DisableReceiving // Easter egg diff --git a/ui.go b/ui.go index 0a663ae..0308393 100644 --- a/ui.go +++ b/ui.go @@ -139,7 +139,7 @@ func (a *goBlog) renderBase(hb *htmlbuilder.HtmlBuilder, rd *renderData, title, hb.WriteEscaped(a.ts.GetTemplateStringVariant(rd.Blog.Lang, "webmentions")) hb.WriteElementClose("a") } - if rd.CommentsEnabled { + if a.commentsEnabledForBlog(rd.Blog) { hb.WriteUnescaped(" • ") hb.WriteElementOpen("a", "href", "/comment") hb.WriteEscaped(a.ts.GetTemplateStringVariant(rd.Blog.Lang, "comments")) @@ -358,7 +358,7 @@ func (a *goBlog) renderComment(h *htmlbuilder.HtmlBuilder, rd *renderData) { hb.WriteElementClose("p") hb.WriteElementClose("main") // Interactions - if rd.CommentsEnabled { + if a.commentsEnabledForBlog(rd.Blog) { a.renderInteractions(hb, rd) } }, @@ -467,7 +467,7 @@ func (a *goBlog) renderBlogStats(hb *htmlbuilder.HtmlBuilder, rd *renderData) { hb.WriteElementClose("script") hb.WriteElementClose("main") // Interactions - if rd.CommentsEnabled { + if a.commentsEnabledForBlog(rd.Blog) { a.renderInteractions(hb, rd) } }, @@ -626,7 +626,7 @@ func (a *goBlog) renderGeoMap(hb *htmlbuilder.HtmlBuilder, rd *renderData) { hb.WriteElementClose("script") } hb.WriteElementClose("main") - if rd.CommentsEnabled { + if a.commentsEnabledForBlog(rd.Blog) { a.renderInteractions(hb, rd) } }, @@ -701,7 +701,7 @@ func (a *goBlog) renderBlogroll(hb *htmlbuilder.HtmlBuilder, rd *renderData) { } hb.WriteElementClose("main") // Interactions - if rd.CommentsEnabled { + if a.commentsEnabledForBlog(rd.Blog) { a.renderInteractions(hb, rd) } }, @@ -980,7 +980,7 @@ func (a *goBlog) renderPost(hb *htmlbuilder.HtmlBuilder, rd *renderData) { hb.WriteElementClose("div") } // Comments - if rd.CommentsEnabled { + if a.commentsEnabledForPost(p) { a.renderInteractions(hb, rd) } },