From a0e870bed228c43a44ddbf42f5ca2502c89532e3 Mon Sep 17 00:00:00 2001 From: Lev <1187448+levb@users.noreply.github.com> Date: Fri, 21 Jan 2022 08:25:33 -0800 Subject: [PATCH] Restored `ToJSON` encoding (sans Integration) for Post APIs (#19279) * Restored ToJSON encoding sans Integration for Post APIs * PR feedback + GetPost test Co-authored-by: Mattermod --- api4/channel.go | 2 +- api4/post.go | 22 +++++++-------- api4/post_test.go | 52 ++++++++++++++++++++++++++++++++++++ model/post.go | 6 +++++ model/post_list.go | 6 +++++ model/post_search_results.go | 6 +++++ 6 files changed, 82 insertions(+), 12 deletions(-) diff --git a/api4/channel.go b/api4/channel.go index 3c3b25aee3..3aaa96aafe 100644 --- a/api4/channel.go +++ b/api4/channel.go @@ -682,7 +682,7 @@ func getPinnedPosts(c *Context, w http.ResponseWriter, r *http.Request) { } w.Header().Set(model.HeaderEtagServer, clientPostList.Etag()) - if err := json.NewEncoder(w).Encode(clientPostList); err != nil { + if err := clientPostList.EncodeJSON(w); err != nil { mlog.Warn("Error while writing response", mlog.Err(err)) } } diff --git a/api4/post.go b/api4/post.go index 3a6cb7b981..3b356a49a3 100644 --- a/api4/post.go +++ b/api4/post.go @@ -101,7 +101,7 @@ func createPost(c *Context, w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) // Note that rp has already had PreparePostForClient called on it by App.CreatePost - if err := json.NewEncoder(w).Encode(rp); err != nil { + if err := rp.EncodeJSON(w); err != nil { mlog.Warn("Error while writing response", mlog.Err(err)) } } @@ -138,7 +138,7 @@ func createEphemeralPost(c *Context, w http.ResponseWriter, r *http.Request) { c.Err = err return } - if err := json.NewEncoder(w).Encode(rp); err != nil { + if err := rp.EncodeJSON(w); err != nil { mlog.Warn("Error while writing response", mlog.Err(err)) } } @@ -244,7 +244,7 @@ func getPostsForChannel(c *Context, w http.ResponseWriter, r *http.Request) { return } - if err := json.NewEncoder(w).Encode(clientPostList); err != nil { + if err := clientPostList.EncodeJSON(w); err != nil { mlog.Warn("Error while writing response", mlog.Err(err)) } } @@ -310,7 +310,7 @@ func getPostsForChannelAroundLastUnread(c *Context, w http.ResponseWriter, r *ht if etag != "" { w.Header().Set(model.HeaderEtagServer, etag) } - if err := json.NewEncoder(w).Encode(clientPostList); err != nil { + if err := clientPostList.EncodeJSON(w); err != nil { mlog.Warn("Error while writing response", mlog.Err(err)) } } @@ -375,7 +375,7 @@ func getFlaggedPostsForUser(c *Context, w http.ResponseWriter, r *http.Request) c.Err = err return } - if err := json.NewEncoder(w).Encode(clientPostList); err != nil { + if err := clientPostList.EncodeJSON(w); err != nil { mlog.Warn("Error while writing response", mlog.Err(err)) } } @@ -404,7 +404,7 @@ func getPost(c *Context, w http.ResponseWriter, r *http.Request) { } w.Header().Set(model.HeaderEtagServer, post.Etag()) - if err := json.NewEncoder(w).Encode(post); err != nil { + if err := post.EncodeJSON(w); err != nil { mlog.Warn("Error while writing response", mlog.Err(err)) } } @@ -451,7 +451,7 @@ func getPostsByIds(c *Context, w http.ResponseWriter, r *http.Request) { } post = c.App.PreparePostForClient(post, false, false) - + post.StripActionIntegrations() posts = append(posts, post) } @@ -536,7 +536,7 @@ func getPostThread(c *Context, w http.ResponseWriter, r *http.Request) { w.Header().Set(model.HeaderEtagServer, clientPostList.Etag()) - if err := json.NewEncoder(w).Encode(clientPostList); err != nil { + if err := clientPostList.EncodeJSON(w); err != nil { mlog.Warn("Error while writing response", mlog.Err(err)) } } @@ -623,7 +623,7 @@ func searchPosts(c *Context, w http.ResponseWriter, r *http.Request, teamId stri results = model.MakePostSearchResults(clientPostList, results.Matches) w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") - if err := json.NewEncoder(w).Encode(results); err != nil { + if err := results.EncodeJSON(w); err != nil { mlog.Warn("Error while writing response", mlog.Err(err)) } } @@ -682,7 +682,7 @@ func updatePost(c *Context, w http.ResponseWriter, r *http.Request) { auditRec.Success() auditRec.AddMeta("update", rpost) - if err := json.NewEncoder(w).Encode(rpost); err != nil { + if err := rpost.EncodeJSON(w); err != nil { mlog.Warn("Error while writing response", mlog.Err(err)) } } @@ -733,7 +733,7 @@ func patchPost(c *Context, w http.ResponseWriter, r *http.Request) { auditRec.Success() auditRec.AddMeta("patch", patchedPost) - if err := json.NewEncoder(w).Encode(patchedPost); err != nil { + if err := patchedPost.EncodeJSON(w); err != nil { mlog.Warn("Error while writing response", mlog.Err(err)) } } diff --git a/api4/post_test.go b/api4/post_test.go index f6fb01ce96..68551608e7 100644 --- a/api4/post_test.go +++ b/api4/post_test.go @@ -2758,3 +2758,55 @@ func TestGetPostsByIds(t *testing.T) { require.Error(t, err) CheckNotFoundStatus(t, response) } + +func TestGetPostStripActionIntegrations(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + client := th.Client + + post := &model.Post{ + ChannelId: th.BasicChannel.Id, + Message: "with slack attachment action", + } + post.AddProp("attachments", []*model.SlackAttachment{ + { + Text: "Slack Attachment Text", + Fields: []*model.SlackAttachmentField{ + { + Title: "Test Field", + Value: "test value", + Short: true, + }, + }, + Actions: []*model.PostAction{ + { + Type: "button", + Name: "test-name", + Integration: &model.PostActionIntegration{ + URL: "https://test.test/action", + Context: map[string]interface{}{ + "test-ctx": "some-value", + }, + }, + }, + }, + }, + }) + + rpost, resp, err2 := client.CreatePost(post) + require.NoError(t, err2) + CheckCreatedStatus(t, resp) + + actualPost, _, err := client.GetPost(rpost.Id, "") + require.NoError(t, err) + attachments, _ := actualPost.Props["attachments"].([]interface{}) + require.Equal(t, 1, len(attachments)) + att, _ := attachments[0].(map[string]interface{}) + require.NotNil(t, att) + actions, _ := att["actions"].([]interface{}) + require.Equal(t, 1, len(actions)) + action, _ := actions[0].(map[string]interface{}) + require.NotNil(t, action) + // integration must be omitted + require.Nil(t, action["integration"]) +} diff --git a/model/post.go b/model/post.go index d13fef0a67..87c1f33836 100644 --- a/model/post.go +++ b/model/post.go @@ -6,6 +6,7 @@ package model import ( "encoding/json" "errors" + "io" "net/http" "regexp" "sort" @@ -227,6 +228,11 @@ func (o *Post) ToJSON() (string, error) { return string(b), err } +func (o *Post) EncodeJSON(w io.Writer) error { + o.StripActionIntegrations() + return json.NewEncoder(w).Encode(o) +} + type GetPostsSinceOptions struct { UserId string ChannelId string diff --git a/model/post_list.go b/model/post_list.go index 933d1f8fdb..bb28063a93 100644 --- a/model/post_list.go +++ b/model/post_list.go @@ -5,6 +5,7 @@ package model import ( "encoding/json" + "io" "sort" ) @@ -80,6 +81,11 @@ func (o *PostList) ToJSON() (string, error) { return string(b), err } +func (o *PostList) EncodeJSON(w io.Writer) error { + o.StripActionIntegrations() + return json.NewEncoder(w).Encode(o) +} + func (o *PostList) MakeNonNil() { if o.Order == nil { o.Order = make([]string, 0) diff --git a/model/post_search_results.go b/model/post_search_results.go index 92e044a7f8..a3afc7231a 100644 --- a/model/post_search_results.go +++ b/model/post_search_results.go @@ -5,6 +5,7 @@ package model import ( "encoding/json" + "io" ) type PostSearchMatches map[string][]string @@ -27,3 +28,8 @@ func (o *PostSearchResults) ToJSON() (string, error) { b, err := json.Marshal(©) return string(b), err } + +func (o *PostSearchResults) EncodeJSON(w io.Writer) error { + o.PostList.StripActionIntegrations() + return json.NewEncoder(w).Encode(o) +}