MM-51021 - Remove PostEditTimeLimit check for plugins and a few other code paths (#22422)

* lift PostEditTimeLimit check to API code; tests

* lift PatchPost time limit check into API layer; test

* fix test

* fix i18n; fix test

* fix test
Этот коммит содержится в:
Christopher Poile
2023-03-09 13:37:23 -05:00
коммит произвёл GitHub
родитель 280bc7f97e
Коммит 53f5f2f267
4 изменённых файлов: 62 добавлений и 6 удалений

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

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

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

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

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

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

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

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