MM-13253/MM-13254 Call PreparePostForClient in app.UpdatePost (#9983)

This commit is contained in:
Harrison Healey
2018-12-13 09:47:30 -05:00
committed by GitHub
parent 487f82f30e
commit ea8e9497a8
3 changed files with 99 additions and 5 deletions

View File

@@ -435,8 +435,6 @@ func updatePost(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
rpost = c.App.PreparePostForClient(rpost)
w.Write([]byte(rpost.ToJson()))
}
@@ -477,8 +475,6 @@ func patchPost(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
patchedPost = c.App.PreparePostForClient(patchedPost)
w.Write([]byte(patchedPost.ToJson()))
}

View File

@@ -409,6 +409,8 @@ func (a *App) UpdatePost(post *model.Post, safeUpdate bool) (*model.Post, *model
})
}
rpost = a.PreparePostForClient(rpost)
a.sendUpdatedPostEvent(rpost)
a.InvalidateCacheForChannelPosts(rpost.ChannelId)
@@ -434,7 +436,7 @@ func (a *App) PatchPost(postId string, patch *model.PostPatch) (*model.Post, *mo
func (a *App) sendUpdatedPostEvent(post *model.Post) {
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_POST_EDITED, "", post.ChannelId, "", nil)
message.Add("post", a.PreparePostForClient(post).ToJson())
message.Add("post", post.ToJson())
a.Publish(message)
}

View File

@@ -340,3 +340,99 @@ func TestDeletePostWithFileAttachments(t *testing.T) {
_, err = th.App.GetFileInfo(info1.Id)
assert.NotNil(t, err)
}
func TestCreatePost(t *testing.T) {
t.Run("call PreparePostForClient before returning", func(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ExperimentalSettings.EnablePostMetadata = false
*cfg.ServiceSettings.ImageProxyType = "atmos/camo"
*cfg.ServiceSettings.ImageProxyURL = "https://127.0.0.1"
*cfg.ServiceSettings.ImageProxyOptions = "foo"
})
imageURL := "http://mydomain.com/myimage"
proxiedImageURL := "https://127.0.0.1/f8dace906d23689e8d5b12c3cefbedbf7b9b72f5/687474703a2f2f6d79646f6d61696e2e636f6d2f6d79696d616765"
post := &model.Post{
ChannelId: th.BasicChannel.Id,
Message: "![image](" + imageURL + ")",
UserId: th.BasicUser.Id,
}
rpost, err := th.App.CreatePost(post, th.BasicChannel, false)
require.Nil(t, err)
assert.Equal(t, "![image]("+proxiedImageURL+")", rpost.Message)
})
}
func TestPatchPost(t *testing.T) {
t.Run("call PreparePostForClient before returning", func(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ExperimentalSettings.EnablePostMetadata = false
*cfg.ServiceSettings.ImageProxyType = "atmos/camo"
*cfg.ServiceSettings.ImageProxyURL = "https://127.0.0.1"
*cfg.ServiceSettings.ImageProxyOptions = "foo"
})
imageURL := "http://mydomain.com/myimage"
proxiedImageURL := "https://127.0.0.1/f8dace906d23689e8d5b12c3cefbedbf7b9b72f5/687474703a2f2f6d79646f6d61696e2e636f6d2f6d79696d616765"
post := &model.Post{
ChannelId: th.BasicChannel.Id,
Message: "![image](http://mydomain/anotherimage)",
UserId: th.BasicUser.Id,
}
rpost, err := th.App.CreatePost(post, th.BasicChannel, false)
require.Nil(t, err)
assert.NotEqual(t, "![image]("+proxiedImageURL+")", rpost.Message)
patch := &model.PostPatch{
Message: model.NewString("![image](" + imageURL + ")"),
}
rpost, err = th.App.PatchPost(rpost.Id, patch)
require.Nil(t, err)
assert.Equal(t, "![image]("+proxiedImageURL+")", rpost.Message)
})
}
func TestUpdatePost(t *testing.T) {
t.Run("call PreparePostForClient before returning", func(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ExperimentalSettings.EnablePostMetadata = false
*cfg.ServiceSettings.ImageProxyType = "atmos/camo"
*cfg.ServiceSettings.ImageProxyURL = "https://127.0.0.1"
*cfg.ServiceSettings.ImageProxyOptions = "foo"
})
imageURL := "http://mydomain.com/myimage"
proxiedImageURL := "https://127.0.0.1/f8dace906d23689e8d5b12c3cefbedbf7b9b72f5/687474703a2f2f6d79646f6d61696e2e636f6d2f6d79696d616765"
post := &model.Post{
ChannelId: th.BasicChannel.Id,
Message: "![image](http://mydomain/anotherimage)",
UserId: th.BasicUser.Id,
}
rpost, err := th.App.CreatePost(post, th.BasicChannel, false)
require.Nil(t, err)
assert.NotEqual(t, "![image]("+proxiedImageURL+")", rpost.Message)
post.Id = rpost.Id
post.Message = "![image](" + imageURL + ")"
rpost, err = th.App.UpdatePost(post, false)
require.Nil(t, err)
assert.Equal(t, "![image]("+proxiedImageURL+")", rpost.Message)
})
}