Open platform to collect and prioritize feedback
Moving tests and code around
Matt Roberts33fbd8e3475d
diff --git a/app/handlers/apiv1/post.go b/app/handlers/apiv1/post.goindex fffecc946..5bbf8cb1e 100644--- a/app/handlers/apiv1/post.go+++ b/app/handlers/apiv1/post.go@@ -1,6 +1,8 @@package apiv1import (+ "fmt"+"github.com/getfider/fider/app/actions""github.com/getfider/fider/app/metrics""github.com/getfider/fider/app/models/cmd"@@ -272,13 +274,18 @@ func PostComment() web.HandlerFunc {}addNewComment := &cmd.AddNewComment{- Post: getPost.Result,- Content: action.Content,+ Post: getPost.Result,+ Content: entity.CommentString(action.Content).FormatMentionJson(func(mention entity.Mention) string {+ return fmt.Sprintf(`{"id":%d,"name":"%s"}`, mention.ID, mention.Name)+ }),}if err := bus.Dispatch(c, addNewComment); err != nil {return c.Failure(err)}+ // For processing, restore the original content+ addNewComment.Result.Content = action.Content+if err := bus.Dispatch(c, &cmd.SetAttachments{Post: getPost.Result,Comment: addNewComment.Result,@@ -287,7 +294,7 @@ func PostComment() web.HandlerFunc {return c.Failure(err)}- c.Enqueue(tasks.NotifyAboutNewComment(getPost.Result, addNewComment.Result))+ c.Enqueue(tasks.NotifyAboutNewComment(addNewComment.Result, getPost.Result))metrics.TotalComments.Inc()return c.Ok(web.Map{@@ -304,6 +311,15 @@ func UpdateComment() web.HandlerFunc {return c.HandleValidation(result)}+ getPost := &query.GetPostByID{PostID: action.Post.ID}+ if err := bus.Dispatch(c, getPost); err != nil {+ return c.Failure(err)+ }++ contentToSave := entity.CommentString(action.Content).FormatMentionJson(func(mention entity.Mention) string {+ return fmt.Sprintf(`{"id":%d,"name":"%s"}`, mention.ID, mention.Name)+ })+err := bus.Dispatch(c,&cmd.UploadImages{Images: action.Attachments,@@ -311,7 +327,7 @@ func UpdateComment() web.HandlerFunc {},&cmd.UpdateComment{CommentID: action.ID,- Content: action.Content,+ Content: contentToSave,},&cmd.SetAttachments{Post: action.Post,@@ -323,6 +339,10 @@ func UpdateComment() web.HandlerFunc {return c.Failure(err)}+ // Update the content++ c.Enqueue(tasks.NotifyAboutUpdatedComment(action.Content, getPost.Result))+return c.Ok(web.Map{})}}diff --git a/app/handlers/apiv1/post_test.go b/app/handlers/apiv1/post_test.goindex 1bb77621e..101662503 100644--- a/app/handlers/apiv1/post_test.go+++ b/app/handlers/apiv1/post_test.go@@ -543,6 +543,36 @@ func TestPostCommentHandler(t *testing.T) {Expect(newComment.Content).Equals("This is a comment!")}+func TestPostCommentHandlerMentions(t *testing.T) {+ RegisterT(t)++ post := &entity.Post{ID: 1, Number: 1, Title: "The Post #1", Description: "The Description #1"}+ bus.AddHandler(func(ctx context.Context, q *query.GetPostByNumber) error {+ q.Result = post+ return nil+ })++ var newComment *cmd.AddNewComment+ bus.AddHandler(func(ctx context.Context, c *cmd.AddNewComment) error {+ newComment = c+ c.Result = &entity.Comment{ID: 1, Content: c.Content}+ return nil+ })++ bus.AddHandler(func(ctx context.Context, c *cmd.SetAttachments) error { return nil })+ bus.AddHandler(func(ctx context.Context, c *cmd.UploadImages) error { return nil })++ code, _ := mock.NewServer().+ OnTenant(mock.DemoTenant).+ AsUser(mock.JonSnow).+ AddParam("number", post.Number).+ ExecutePost(apiv1.PostComment(), `{ "content": "Hello @{\"id\":1,\"name\":\"Jon Snow\",\"isNew\":true}!" }`)++ Expect(code).Equals(http.StatusOK)+ Expect(newComment.Post).Equals(post)+ Expect(newComment.Content).Equals("Hello @{\"id\":1,\"name\":\"Jon Snow\"}!")+}+func TestPostCommentHandler_WithoutContent(t *testing.T) {RegisterT(t)diff --git a/app/models/entity/comment.go b/app/models/entity/comment.goindex dcf63b6af..ddac8b4a8 100644--- a/app/models/entity/comment.go+++ b/app/models/entity/comment.go@@ -1,9 +1,6 @@package entityimport (- "encoding/json"- "regexp"- "strings""time")@@ -13,12 +10,6 @@ type ReactionCounts struct {IncludesMe bool `json:"includesMe"`}-type Mention struct {- ID int `json:"id"`- Name string `json:"name"`- IsNew bool `json:"isNew"`-}-// Comment represents an user comment on an posttype Comment struct {ID int `json:"id"`@@ -33,38 +24,6 @@ type Comment struct {}func (c *Comment) ParseMentions() {- r, _ := regexp.Compile("@{([^}]+)}")-- // Remove escaped quotes from the input string- input := strings.ReplaceAll(c.Content, `\"`, `"`)-- matches := r.FindAllString(input, -1)-- c.Mentions = []Mention{}-- for _, match := range matches {-- jsonMention := match[1:]-- var dat map[string]interface{}-- err := json.Unmarshal([]byte(jsonMention), &dat)- if err == nil {- name, nameExists := dat["name"]- id, idExists := dat["id"]- isNew, isNewExists := dat["isNew"]- if !isNewExists {- isNew = false- }-- if nameExists && idExists {- mention := Mention{- ID: int(id.(float64)),- Name: name.(string),- IsNew: isNew.(bool),- }- c.Mentions = append(c.Mentions, mention)- }- }- }+ mentionString := CommentString(c.Content)+ c.Mentions = mentionString.ParseMentions()}diff --git a/app/models/entity/comment_test.go b/app/models/entity/comment_test.godeleted file mode 100644index f59270981..000000000--- a/app/models/entity/comment_test.go+++ /dev/null@@ -1,52 +0,0 @@-package entity_test--import (- "testing"-- "github.com/getfider/fider/app/models/entity"-- . "github.com/getfider/fider/app/pkg/assert"-)--func TestComment_ParseMentions(t *testing.T) {- RegisterT(t)- tests := []struct {- name string- content string- expected []entity.Mention- }{- {- name: "no mentions",- content: "This is a regular comment \\\" just here",- expected: []entity.Mention{},- },- {- name: "Simple mention",- content: `Hello there @{"id":1,"name":"John Doe","isNew":false} how are you`,- expected: []entity.Mention{{ID: 1, Name: "John Doe", IsNew: false}},- },- {- name: "Simple mention 2",- content: `Hello there @{"id":2,"name":"John Doe Smith","isNew":true} how are you`,- expected: []entity.Mention{{ID: 2, Name: "John Doe Smith", IsNew: true}},- },- {- name: "Multiple mentions",- content: `Hello there @{"id":2,"name":"John Doe Smith","isNew":true} and @{"id":1,"name":"John Doe","isNew":false} how are you`,- expected: []entity.Mention{{ID: 2, Name: "John Doe Smith", IsNew: true}, {ID: 1, Name: "John Doe", IsNew: false}},- },- {- name: "Some odd JSON",- content: `Hello there @{"id":2,name:"John Doe Smith","isNew":true} how are you`,- expected: []entity.Mention{},- },- }-- for _, tt := range tests {- t.Run(tt.name, func(t *testing.T) {- comment := &entity.Comment{Content: tt.content}- comment.ParseMentions()- Expect(comment.Mentions).Equals(tt.expected)- })- }-}diff --git a/app/models/entity/mention.go b/app/models/entity/mention.gonew file mode 100644index 000000000..085b4356c--- /dev/null+++ b/app/models/entity/mention.go@@ -0,0 +1,69 @@+package entity++import (+ "encoding/json"+ "regexp"+ "strings"+)++type Mention struct {+ ID int `json:"id"`+ Name string `json:"name"`+ IsNew bool `json:"isNew"`+}++type CommentString string++const mentionRegex = `@{([^{}]+)}`++func (commentString CommentString) ParseMentions() []Mention {+ r, _ := regexp.Compile(mentionRegex)++ // Remove escaped quotes from the input string+ input := strings.ReplaceAll(string(commentString), `\"`, `"`)++ matches := r.FindAllString(input, -1)++ mentions := []Mention{}++ for _, match := range matches {++ jsonMention := match[1:]++ var mention Mention+ err := json.Unmarshal([]byte(jsonMention), &mention)+ if err == nil {+ if mention.ID > 0 && mention.Name != "" {+ mentions = append(mentions, mention)+ }+ }+ }++ return mentions+}++func (mentionString CommentString) FormatMentionJson(jsonOperator func(Mention) string) string {++ r, _ := regexp.Compile(mentionRegex)++ // Remove escaped quotes from the input string+ input := strings.ReplaceAll(string(mentionString), `\"`, `"`)++ return r.ReplaceAllStringFunc(input, func(match string) string {+ jsonMention := match[1:]++ var mention Mention++ err := json.Unmarshal([]byte(jsonMention), &mention)+ if err != nil {+ return match+ }++ if mention.ID == 0 || mention.Name == "" {+ return match+ }++ return "@" + jsonOperator(mention)+ })++}diff --git a/app/models/entity/mention_test.go b/app/models/entity/mention_test.gonew file mode 100644index 000000000..e519928ce--- /dev/null+++ b/app/models/entity/mention_test.go@@ -0,0 +1,92 @@+package entity_test++import (+ "testing"++ "github.com/getfider/fider/app/models/entity"++ . "github.com/getfider/fider/app/pkg/assert"+)++func TestComment_ParseMentions(t *testing.T) {+ RegisterT(t)+ tests := []struct {+ name string+ content string+ expected []entity.Mention+ }{+ {+ name: "no mentions",+ content: "This is a regular comment \\\" just here",+ expected: []entity.Mention{},+ },+ {+ name: "Simple mention",+ content: `Hello there @{"id":1,"name":"John Doe","isNew":false} how are you`,+ expected: []entity.Mention{{ID: 1, Name: "John Doe", IsNew: false}},+ },+ {+ name: "Simple mention 2",+ content: `Hello there @{"id":2,"name":"John Doe Smith","isNew":true} how are you`,+ expected: []entity.Mention{{ID: 2, Name: "John Doe Smith", IsNew: true}},+ },+ {+ name: "Multiple mentions",+ content: `Hello there @{"id":2,"name":"John Doe Smith","isNew":true} and @{"id":1,"name":"John Doe","isNew":false} how are you`,+ expected: []entity.Mention{{ID: 2, Name: "John Doe Smith", IsNew: true}, {ID: 1, Name: "John Doe", IsNew: false}},+ },+ {+ name: "Some odd JSON",+ content: `Hello there @{"id":2,name:"John Doe Smith","isNew":true} how are you`,+ expected: []entity.Mention{},+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ comment := &entity.Comment{Content: tt.content}+ comment.ParseMentions()+ Expect(comment.Mentions).Equals(tt.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 := entity.CommentString(input).FormatMentionJson(func(mention entity.Mention) string {+ return mention.Name+ })+ Expect(output).Equals(expected)+ }+}++func TestStripMentionMetaDataDoesntBreakUserInput(t *testing.T) {+ RegisterT(t)++ for input, expected := range map[string]string{+ `There is nothing here`: "There is nothing here",+ `There is nothing here {ok}`: "There is nothing here {ok}",+ `This is a message for {{matt}}`: "This is a message for {{matt}}",+ `This is a message for {{id:1,wiggles:true}}`: "This is a message for {{id:1,wiggles:true}}",+ `Although uncommon, someone could enter @{something} like this`: "Although uncommon, someone could enter @{something} like this",+ `Or @{"id":100,"wiggles":"yes"} something like this`: `Or @{"id":100,"wiggles":"yes"} something like this`,+ } {+ output := entity.CommentString(input).FormatMentionJson(func(mention entity.Mention) string {+ return mention.Name+ })+ Expect(output).Equals(expected)+ }+}diff --git a/app/pkg/markdown/markdown.go b/app/pkg/markdown/markdown.goindex 3fb55e5df..758c6b88a 100644--- a/app/pkg/markdown/markdown.go+++ b/app/pkg/markdown/markdown.go@@ -1,12 +1,11 @@package markdownimport (- "encoding/json""html/template""io"- "regexp""strings"+ "github.com/getfider/fider/app/models/entity""github.com/gomarkdown/markdown""github.com/gomarkdown/markdown/ast"@@ -65,29 +64,9 @@ func Full(input string) template.HTML {// Example output: Hello there @John Doefunc 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- }-- name, nameExists := dat["name"]- _, idExists := dat["id"]-- if !nameExists || !idExists {- return match- }-- return "@" + name.(string)+ mentionString := entity.CommentString(input)+ return mentionString.FormatMentionJson(func(mention entity.Mention) string {+ return mention.Name})}diff --git a/app/pkg/markdown/markdown_test.go b/app/pkg/markdown/markdown_test.goindex 0b472f28a..33525ae7c 100644--- a/app/pkg/markdown/markdown_test.go+++ b/app/pkg/markdown/markdown_test.go@@ -80,39 +80,3 @@ 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)- }-}--func TestStripMentionMetaDataDoesntBreakUserInput(t *testing.T) {- RegisterT(t)-- for input, expected := range map[string]string{- `There is nothing here`: "There is nothing here",- `There is nothing here {ok}`: "There is nothing here {ok}",- `This is a message for {{matt}}`: "This is a message for {{matt}}",- `This is a message for {{id:1,wiggles:true}}`: "This is a message for {{id:1,wiggles:true}}",- `Although uncommon, someone could enter @{something} like this`: "Although uncommon, someone could enter @{something} like this",- `Or @{"id":100,"wiggles":"yes"} something like this`: `Or @{"id":100,"wiggles":"yes"} something like this`,- } {- output := markdown.StripMentionMetaData(input)- Expect(output).Equals(expected)- }-}diff --git a/app/tasks/new_comment.go b/app/tasks/new_comment.goindex 7dd6ace55..6a6f058fa 100644--- a/app/tasks/new_comment.go+++ b/app/tasks/new_comment.go@@ -9,6 +9,7 @@ import ("github.com/getfider/fider/app/models/enum""github.com/getfider/fider/app/pkg/bus""github.com/getfider/fider/app/pkg/i18n"+ "github.com/getfider/fider/app/pkg/log""github.com/getfider/fider/app/pkg/markdown""github.com/getfider/fider/app/pkg/web""github.com/getfider/fider/app/pkg/webhook"@@ -16,7 +17,7 @@ import ()// NotifyAboutNewComment sends a notification (web and email) to subscribers-func NotifyAboutNewComment(post *entity.Post, comment *entity.Comment) worker.Task {+func NotifyAboutNewComment(comment *entity.Comment, post *entity.Post) worker.Task {return describe("Notify about new comment", func(c *worker.Context) error {comment.ParseMentions()@@ -136,6 +137,75 @@ func NotifyAboutNewComment(post *entity.Post, comment *entity.Comment) worker.Ta})}+func NotifyAboutUpdatedComment(content string, post *entity.Post) worker.Task {+ return describe("Notify about updated comment", func(c *worker.Context) error {++ contentString := entity.CommentString(content)+ mentions := contentString.ParseMentions()++ log.Infof(c, "Comment updated: @{Comment:Yellow}. Mentions @{MentionsCount}", dto.Props{+ "Comment": contentString,+ "MentionsCount": len(mentions),+ })++ author := c.User()+ title := fmt.Sprintf("**%s** mentioned you in **%s**", author.Name, post.Title)+ link := fmt.Sprintf("/posts/%d/%s", post.Number, post.Slug)+ if mentions != nil {++ users, err := getActiveSubscribers(c, post, enum.NotificationChannelWeb, enum.NotificationEventMention)+ if err != nil {+ return c.Failure(err)+ }++ // Iterate the mentions+ for _, mention := range mentions {+ // Check if the user is in the list of mention subscribers (users)+ for _, u := range users {+ if u.ID == mention.ID && mention.IsNew {+ err = bus.Dispatch(c, &cmd.AddNewNotification{+ User: u,+ Title: title,+ Link: link,+ PostID: post.ID,+ })+ if err != nil {+ return c.Failure(err)+ }+ }+ }+ }++ }++ strippedContent := markdown.StripMentionMetaData(content)++ to := make([]dto.Recipient, 0)+ if mentions != nil {++ users, err := getActiveSubscribers(c, post, enum.NotificationChannelEmail, enum.NotificationEventMention)+ if err != nil {+ return c.Failure(err)+ }++ for _, mention := range mentions {+ // Check if the user is in the list of mention subscribers (users)+ for _, u := range users {+ if u.ID == mention.ID && mention.IsNew {+ to = append(to, dto.NewRecipient(u.Name, u.Email, dto.Props{}))+ break+ }+ }+ }++ }++ sendEmailNotifications(c, post, to, strippedContent, enum.NotificationEventMention)++ return nil+ })+}+func sendEmailNotifications(c *worker.Context, post *entity.Post, to []dto.Recipient, comment string, event enum.NotificationEvent) error {// Short circuit if there is no one to notifydiff --git a/app/tasks/new_comment_test.go b/app/tasks/new_comment_test.goindex 6cc7ea6e2..a609c7125 100644--- a/app/tasks/new_comment_test.go+++ b/app/tasks/new_comment_test.go@@ -56,7 +56,7 @@ func TestNotifyAboutNewCommentTask(t *testing.T) {Description: "TypeScript is great, please add support for it",User: mock.JonSnow,}- task := tasks.NotifyAboutNewComment(post, &entity.Comment{Content: "I agree"})+ task := tasks.NotifyAboutNewComment(&entity.Comment{Content: "I agree"}, post)err := worker.OnTenant(mock.DemoTenant).@@ -158,7 +158,7 @@ func TestNotifyAboutNewCommentTask_WithMention(t *testing.T) {Description: "TypeScript is great, please add support for it",User: mock.JonSnow,}- task := tasks.NotifyAboutNewComment(post, &entity.Comment{Content: "I agree with @{\"id\":1,\"name\":\"Jon Snow\",\"isNew\":true}"})+ task := tasks.NotifyAboutNewComment(&entity.Comment{Content: "I agree with @{\"id\":1,\"name\":\"Jon Snow\",\"isNew\":true}"}, post)err := worker.OnTenant(mock.DemoTenant).@@ -223,3 +223,72 @@ func TestNotifyAboutNewCommentTask_WithMention(t *testing.T) {"tenant_url": "http://domain.com",})}++func TestNotifyAboutUpdatedComment(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+ })++ 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,+ }++ commentString := "I agree with @{\"id\":1,\"name\":\"Jon Snow\",\"isNew\":true} but not @{\"id\":2,\"name\":\"Arya Stark\",\"isNew\":false}"+ task := tasks.NotifyAboutUpdatedComment(commentString, post)++ err := worker.+ OnTenant(mock.DemoTenant).+ AsUser(mock.AryaStark).+ WithBaseURL("http://domain.com").+ Execute(task)++ Expect(err).IsNil()+ Expect(emailmock.MessageHistory).HasLen(1)+ Expect(emailmock.MessageHistory[0].TemplateName).Equals("new_comment")+ Expect(emailmock.MessageHistory[0].Tenant).Equals(mock.DemoTenant)+ Expect(emailmock.MessageHistory[0].Props).Equals(dto.Props{+ "title": "Add support for TypeScript",+ "messageLocaleString": "email.new_mention.text",+ "postLink": "<a href='http://domain.com/posts/1/add-support-for-typescript'>#1</a>",+ "siteName": "Demonstration",+ "userName": "Arya Stark",+ "content": template.HTML("<p>I agree with @Jon Snow but not @Arya Stark</p>"),+ "view": "<a href='http://domain.com/posts/1/add-support-for-typescript'>view it on your browser</a>",+ "change": "<a href='http://domain.com/settings'>change your notification preferences</a>",+ "unsubscribe": "<a href='http://domain.com/posts/1/add-support-for-typescript'>unsubscribe from it</a>",+ "logo": "https://fider.io/images/logo-100x100.png",+ })+ Expect(emailmock.MessageHistory[0].From).Equals(dto.Recipient{+ Name: "Arya Stark",+ })+ Expect(emailmock.MessageHistory[0].To).HasLen(1)+ Expect(emailmock.MessageHistory[0].To[0]).Equals(dto.Recipient{+ Name: "Jon Snow",+ Address: "[email protected]",+ Props: dto.Props{},+ })++ 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** mentioned you in **Add support for TypeScript**")+ Expect(addNewNotification.User).Equals(mock.JonSnow)+}