Open platform to collect and prioritize feedback
Adds attachments provided via the POST /api/v1/posts endpoint to the end of the description
Anders Larsson8db03edd0646
diff --git a/app/handlers/apiv1/post.go b/app/handlers/apiv1/post.goindex 058be7ffa..6465fc547 100644--- a/app/handlers/apiv1/post.go+++ b/app/handlers/apiv1/post.go@@ -1,9 +1,13 @@package apiv1import (+ "fmt"+ "strings"+"github.com/getfider/fider/app/actions""github.com/getfider/fider/app/metrics""github.com/getfider/fider/app/models/cmd"+ "github.com/getfider/fider/app/models/dto""github.com/getfider/fider/app/models/entity""github.com/getfider/fider/app/models/enum""github.com/getfider/fider/app/models/query"@@ -13,6 +17,35 @@ import ("github.com/getfider/fider/app/tasks")+// appendUnreferencedAttachments appends a fider-image markdown reference to the end of the+// description for every attachment being added that isn't already referenced in it. This+// keeps API-created posts consistent with the editor, which embeds images inline as+// "". Must be called after UploadImages so blob keys are finalized.+func appendUnreferencedAttachments(description string, attachments []*dto.ImageUpload) string {+ seen := map[string]bool{}+ refs := make([]string, 0, len(attachments))+ for _, a := range attachments {+ if a == nil || a.Remove || a.BlobKey == "" || seen[a.BlobKey] {+ continue+ }+ seen[a.BlobKey] = true+ if strings.Contains(description, "fider-image:"+a.BlobKey) {+ continue+ }+ refs = append(refs, fmt.Sprintf("", a.BlobKey))+ }++ if len(refs) == 0 {+ return description+ }++ appended := strings.Join(refs, "\n\n")+ if strings.TrimSpace(description) == "" {+ return appended+ }+ return description + "\n\n" + appended+}+// SearchPosts return existing posts based on search criteriafunc SearchPosts() web.HandlerFunc {return func(c *web.Context) error {@@ -73,7 +106,7 @@ func CreatePost() web.HandlerFunc {newPost := &cmd.AddNewPost{Title: action.Title,- Description: action.Description,+ Description: appendUnreferencedAttachments(action.Description, action.Attachments),}err := bus.Dispatch(c, newPost)if err != nil {@@ -133,17 +166,18 @@ func UpdatePost() web.HandlerFunc {return c.HandleValidation(result)}+ // Upload first so blob keys are finalized before building the description.+ if err := bus.Dispatch(c, &cmd.UploadImages{Images: action.Attachments, Folder: "attachments"}); err != nil {+ return c.Failure(err)+ }+updatePost := &cmd.UpdatePost{Post: action.Post,Title: action.Title,- Description: action.Description,+ Description: appendUnreferencedAttachments(action.Description, action.Attachments),}err := bus.Dispatch(c,- &cmd.UploadImages{- Images: action.Attachments,- Folder: "attachments",- },updatePost,&cmd.SetAttachments{Post: action.Post,diff --git a/app/handlers/apiv1/post_test.go b/app/handlers/apiv1/post_test.goindex 0e042c2c5..96671b103 100644--- a/app/handlers/apiv1/post_test.go+++ b/app/handlers/apiv1/post_test.go@@ -53,6 +53,38 @@ func TestCreatePostHandler(t *testing.T) {Expect(newPost.Description).Equals("")}+func TestCreatePostHandler_AppendsUnreferencedAttachments(t *testing.T) {+ RegisterT(t)++ var newPost *cmd.AddNewPost+ bus.AddHandler(func(ctx context.Context, c *cmd.AddNewPost) error {+ newPost = c+ c.Result = &entity.Post{ID: 1, Title: c.Title, Description: c.Description}+ return nil+ })+ bus.AddHandler(func(ctx context.Context, q *query.GetPostBySlug) error { return app.ErrNotFound })+ bus.AddHandler(func(ctx context.Context, c *cmd.SetAttachments) error { return nil })+ bus.AddHandler(func(ctx context.Context, c *cmd.AddVote) error { return nil })+ bus.AddHandler(func(ctx context.Context, c *cmd.UploadImages) error { return nil })++ code, _ := mock.NewServer().+ OnTenant(mock.DemoTenant).+ AsUser(mock.JonSnow).+ ExecutePost(apiv1.CreatePost(), `{+ "title": "Post with attachments",+ "description": "Already referenced: ",+ "attachments": [+ { "bkey": "attachments/referenced.png" },+ { "bkey": "attachments/standalone.png" }+ ]+ }`)++ Expect(code).Equals(http.StatusOK)+ // The already-referenced attachment is left as-is (not duplicated), and the+ // unreferenced one is appended at the end as a fider-image markdown reference.+ Expect(newPost.Description).Equals("Already referenced: \n\n")+}+func TestCreatePostHandler_WithoutTitle(t *testing.T) {RegisterT(t)