diff --git a/api4/post.go b/api4/post.go index eee642b99e..2ce216c099 100644 --- a/api4/post.go +++ b/api4/post.go @@ -809,6 +809,11 @@ func updatePost(c *Context, w http.ResponseWriter, r *http.Request) { post.Id = c.Params.PostId + if *c.App.Config().ServiceSettings.PostEditTimeLimit != -1 && model.GetMillis() > originalPost.CreateAt+int64(*c.App.Config().ServiceSettings.PostEditTimeLimit*1000) && post.Message != originalPost.Message { + c.Err = model.NewAppError("UpdatePost", "api.post.update_post.permissions_time_limit.app_error", map[string]any{"timeLimit": *c.App.Config().ServiceSettings.PostEditTimeLimit}, "", http.StatusBadRequest) + return + } + rpost, err := c.App.UpdatePost(c.AppContext, c.App.PostWithProxyRemovedFromImageURLs(&post), false) if err != nil { c.Err = err @@ -863,6 +868,11 @@ func patchPost(c *Context, w http.ResponseWriter, r *http.Request) { return } + if *c.App.Config().ServiceSettings.PostEditTimeLimit != -1 && model.GetMillis() > originalPost.CreateAt+int64(*c.App.Config().ServiceSettings.PostEditTimeLimit*1000) && post.Message != nil { + c.Err = model.NewAppError("patchPost", "api.post.update_post.permissions_time_limit.app_error", map[string]any{"timeLimit": *c.App.Config().ServiceSettings.PostEditTimeLimit}, "", http.StatusBadRequest) + return + } + patchedPost, err := c.App.PatchPost(c.AppContext, c.Params.PostId, c.App.PostPatchWithProxyRemovedFromImageURLs(&post)) if err != nil { c.Err = err diff --git a/api4/post_test.go b/api4/post_test.go index a73a486a50..db979d569f 100644 --- a/api4/post_test.go +++ b/api4/post_test.go @@ -850,6 +850,32 @@ func TestUpdatePost(t *testing.T) { assert.NotEqual(t, rpost3.Attachments(), rrupost3.Attachments()) }) + t.Run("change message, but post too old", func(t *testing.T) { + th.App.UpdateConfig(func(cfg *model.Config) { + *cfg.ServiceSettings.PostEditTimeLimit = 1 + }) + defer th.App.UpdateConfig(func(cfg *model.Config) { + *cfg.ServiceSettings.PostEditTimeLimit = -1 + }) + + rpost4, appErr := th.App.CreatePost(th.Context, &model.Post{ + ChannelId: channel.Id, + Message: "zz" + model.NewId() + "a", + UserId: th.BasicUser.Id, + CreateAt: model.GetMillis() - 2000, + }, channel, false, true) + require.Nil(t, appErr) + + up4 := &model.Post{ + Id: rpost4.Id, + ChannelId: channel.Id, + Message: "zz" + model.NewId() + " update post 4", + } + _, resp, err := client.UpdatePost(rpost4.Id, up4) + require.Error(t, err, "should fail on update old post") + CheckBadRequestStatus(t, resp) + }) + t.Run("logged out", func(t *testing.T) { client.Logout() _, resp, err := client.UpdatePost(rpost.Id, rpost) @@ -1035,6 +1061,31 @@ func TestPatchPost(t *testing.T) { _, _, err = client.PatchPost(post.Id, patch) require.NoError(t, err) }) + + t.Run("time limit expired", func(t *testing.T) { + th.App.UpdateConfig(func(cfg *model.Config) { + *cfg.ServiceSettings.PostEditTimeLimit = 1 + }) + defer th.App.UpdateConfig(func(cfg *model.Config) { + *cfg.ServiceSettings.PostEditTimeLimit = -1 + }) + + post2 := &model.Post{ + ChannelId: channel.Id, + Message: "#hashtag a message", + CreateAt: model.GetMillis() - 2000, + } + post2, _, err := th.SystemAdminClient.CreatePost(post2) + require.NoError(t, err) + + patch2 := &model.PostPatch{ + Message: model.NewString("new message"), + } + _, resp, err := th.SystemAdminClient.PatchPost(post2.Id, patch2) + require.Error(t, err) + CheckBadRequestStatus(t, resp) + require.Equal(t, "api.post.update_post.permissions_time_limit.app_error", err.(*model.AppError).Id, "should be time limit error") + }) } func TestPinPost(t *testing.T) { diff --git a/app/post.go b/app/post.go index f9018bf18a..f05b5e80e0 100644 --- a/app/post.go +++ b/app/post.go @@ -631,11 +631,6 @@ func (a *App) UpdatePost(c *request.Context, post *model.Post, safeUpdate bool) return nil, err } - if *a.Config().ServiceSettings.PostEditTimeLimit != -1 && model.GetMillis() > oldPost.CreateAt+int64(*a.Config().ServiceSettings.PostEditTimeLimit*1000) && post.Message != oldPost.Message { - err = model.NewAppError("UpdatePost", "api.post.update_post.permissions_time_limit.app_error", map[string]any{"timeLimit": *a.Config().ServiceSettings.PostEditTimeLimit}, "", http.StatusBadRequest) - return nil, err - } - channel, err := a.GetChannel(c, oldPost.ChannelId) if err != nil { return nil, err diff --git a/app/post_test.go b/app/post_test.go index 2fedbf8a75..110bdf1779 100644 --- a/app/post_test.go +++ b/app/post_test.go @@ -309,7 +309,7 @@ func TestUpdatePostTimeLimit(t *testing.T) { }) post.Message = model.NewId() _, err = th.App.UpdatePost(th.Context, post, true) - require.NotNil(t, err, "should fail on update old post") + require.Nil(t, err, "should allow you to edit an old post because the time check is applied above in the call hierarchy") th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.PostEditTimeLimit = -1