Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

Added a function to remove mention encoding from comments. Plumbed into webhooks stuff.

Matt Robertsbce520a3756f
diff --git a/app/pkg/markdown/markdown.go b/app/pkg/markdown/markdown.go
index 17df3839d..502473b93 100644
--- a/app/pkg/markdown/markdown.go
+++ b/app/pkg/markdown/markdown.go
@@ -1,8 +1,10 @@
package markdown
import (
+ "encoding/json"
"html/template"
"io"
+ "regexp"
"strings"
"github.com/gomarkdown/markdown"
@@ -57,3 +59,27 @@ func Full(input string) template.HTML {
output := markdown.ToHTML([]byte(input), parser, fullRenderer)
return template.HTML(strings.TrimSpace(string(output)))
}
+
+// StripMentionMetaData removes all mention metadata from a markdown string
+// Example input: Hello there @{\"id\":1,\"name\":\"John Doe\"}"
+// Example output: Hello there @John Doe
+func StripMentionMetaData(input string) string {
+
+ r, _ := regexp.Compile("@{([^}]+)}")
+
+ // Remove escaped quotes from the input string
+ input = strings.ReplaceAll(input, `\"`, `"`)
+
+ return r.ReplaceAllStringFunc(input, func(match string) string {
+ jsonMention := match[1:]
+
+ var dat map[string]interface{}
+
+ err := json.Unmarshal([]byte(jsonMention), &dat)
+ if err != nil {
+ return match
+ }
+ return "@" + dat["name"].(string)
+ })
+
+}
diff --git a/app/pkg/markdown/markdown_test.go b/app/pkg/markdown/markdown_test.go
index 33525ae7c..e39e75027 100644
--- a/app/pkg/markdown/markdown_test.go
+++ b/app/pkg/markdown/markdown_test.go
@@ -80,3 +80,23 @@ How are you?`,
Expect(output).Equals(expected)
}
}
+
+func TestStripMentionMetaData(t *testing.T) {
+ RegisterT(t)
+
+ for input, expected := range map[string]string{
+ `@{\"id\":1,\"name\":\"John Doe quoted\"}`: "@John Doe quoted",
+ `@{"id":1,"name":"John Doe"}`: "@John Doe",
+ `@{"id":1,"name":"JohnDoe"}`: "@JohnDoe",
+ `@{\"id\":1,\"name\":\"JohnDoe quoted\"}`: "@JohnDoe quoted",
+ `@{"id":1,"name":"John Smith Doe"}`: "@John Smith Doe",
+ `@{\"id\":1,\"name\":\"John Smith Doe quoted\"}`: "@John Smith Doe quoted",
+ "Hello there how are you": "Hello there how are you",
+ `Hello there @{"id":1,"name":"John Doe"}`: "Hello there @John Doe",
+ `Hello there @{"id":1,"name":"John Doe quoted"}`: "Hello there @John Doe quoted",
+ `Hello both @{"id":1,"name":"John Doe"} and @{"id":2,"name":"John Smith"}`: "Hello both @John Doe and @John Smith",
+ } {
+ output := markdown.StripMentionMetaData(input)
+ Expect(output).Equals(expected)
+ }
+}
diff --git a/app/tasks/new_comment.go b/app/tasks/new_comment.go
index c87478076..9a19cc839 100644
--- a/app/tasks/new_comment.go
+++ b/app/tasks/new_comment.go
@@ -15,7 +15,7 @@ import (
"github.com/getfider/fider/app/pkg/worker"
)
-//NotifyAboutNewComment sends a notification (web and email) to subscribers
+// NotifyAboutNewComment sends a notification (web and email) to subscribers
func NotifyAboutNewComment(post *entity.Post, comment string) worker.Task {
return describe("Notify about new comment", func(c *worker.Context) error {
// Web notification
@@ -76,7 +76,7 @@ func NotifyAboutNewComment(post *entity.Post, comment string) worker.Task {
Props: mailProps,
})
- webhookProps := webhook.Props{"comment": comment}
+ webhookProps := webhook.Props{"comment": markdown.StripMentionMetaData(comment)}
webhookProps.SetPost(post, "post", baseURL, true, true)
webhookProps.SetUser(author, "author")
webhookProps.SetTenant(tenant, "tenant", baseURL, logoURL)
diff --git a/app/tasks/new_comment_test.go b/app/tasks/new_comment_test.go
index 17b7338ae..177e009de 100644
--- a/app/tasks/new_comment_test.go
+++ b/app/tasks/new_comment_test.go
@@ -116,3 +116,57 @@ func TestNotifyAboutNewCommentTask(t *testing.T) {
"tenant_url": "http://domain.com",
})
}
+
+func TestNotifyAboutNewCommentTask_WithMention(t *testing.T) {
+ RegisterT(t)
+ bus.Init(emailmock.Service{})
+
+ var addNewNotification *cmd.AddNewNotification
+ bus.AddHandler(func(ctx context.Context, c *cmd.AddNewNotification) error {
+ addNewNotification = c
+ return nil
+ })
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetActiveSubscribers) error {
+ q.Result = []*entity.User{
+ mock.JonSnow,
+ }
+ return nil
+ })
+
+ var triggerWebhooks *cmd.TriggerWebhooks
+ bus.AddHandler(func(ctx context.Context, c *cmd.TriggerWebhooks) error {
+ triggerWebhooks = c
+ return nil
+ })
+
+ worker := mock.NewWorker()
+ post := &entity.Post{
+ ID: 1,
+ Number: 1,
+ Title: "Add support for TypeScript",
+ Slug: "add-support-for-typescript",
+ Description: "TypeScript is great, please add support for it",
+ User: mock.JonSnow,
+ }
+ task := tasks.NotifyAboutNewComment(post, `I agree, what about you @{"id":1, "name":"Jon Snow"}?`)
+
+ err := worker.
+ OnTenant(mock.DemoTenant).
+ AsUser(mock.AryaStark).
+ WithBaseURL("http://domain.com").
+ Execute(task)
+
+ Expect(err).IsNil()
+ Expect(addNewNotification).IsNotNil()
+ Expect(addNewNotification.PostID).Equals(post.ID)
+ Expect(addNewNotification.Link).Equals("/posts/1/add-support-for-typescript")
+ Expect(addNewNotification.Title).Equals("**Arya Stark** left a comment on **Add support for TypeScript**")
+ Expect(addNewNotification.User).Equals(mock.JonSnow)
+
+ Expect(triggerWebhooks).IsNotNil()
+ Expect(triggerWebhooks.Type).Equals(enum.WebhookNewComment)
+ Expect(triggerWebhooks.Props).ContainsProps(webhook.Props{
+ "comment": "I agree, what about you @Jon Snow?",
+ })
+}