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 <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Lev
2022-01-21 08:25:33 -08:00
коммит произвёл GitHub
родитель 956e21cfa2
Коммит a0e870bed2
6 изменённых файлов: 82 добавлений и 12 удалений

Просмотреть файл

@@ -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))
}
}

Просмотреть файл

@@ -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))
}
}

Просмотреть файл

@@ -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"])
}

Просмотреть файл

@@ -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

Просмотреть файл

@@ -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)

Просмотреть файл

@@ -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(&copy)
return string(b), err
}
func (o *PostSearchResults) EncodeJSON(w io.Writer) error {
o.PostList.StripActionIntegrations()
return json.NewEncoder(w).Encode(o)
}