diff --git a/api4/post.go b/api4/post.go index 02ee847bad..b6d2483d5f 100644 --- a/api4/post.go +++ b/api4/post.go @@ -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())) } diff --git a/app/post.go b/app/post.go index 778b733ccd..403873feba 100644 --- a/app/post.go +++ b/app/post.go @@ -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) } diff --git a/app/post_test.go b/app/post_test.go index 56b8318506..5d605bc238 100644 --- a/app/post_test.go +++ b/app/post_test.go @@ -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) + }) +}