From b916bae0f7569c224ea74cb39ee011093ab0f364 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Thu, 16 Jul 2020 10:25:22 +0200 Subject: [PATCH] [MM-24602] Adds local patch channel endpoint (#15032) --- api4/channel_local.go | 44 +++++++++++++++++++++++++++++++++++++++++++ api4/channel_test.go | 10 ++++++---- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/api4/channel_local.go b/api4/channel_local.go index d454527603..d35b796b97 100644 --- a/api4/channel_local.go +++ b/api4/channel_local.go @@ -16,6 +16,7 @@ func (api *API) InitChannelLocal() { api.BaseRoutes.Channel.Handle("", api.ApiLocal(getChannel)).Methods("GET") api.BaseRoutes.ChannelByName.Handle("", api.ApiLocal(getChannelByName)).Methods("GET") api.BaseRoutes.Channel.Handle("", api.ApiLocal(deleteChannel)).Methods("DELETE") + api.BaseRoutes.Channel.Handle("/patch", api.ApiLocal(localPatchChannel)).Methods("PUT") api.BaseRoutes.ChannelMember.Handle("", api.ApiLocal(localRemoveChannelMember)).Methods("DELETE") api.BaseRoutes.ChannelMember.Handle("", api.ApiLocal(getChannelMember)).Methods("GET") @@ -162,3 +163,46 @@ func localRemoveChannelMember(c *Context, w http.ResponseWriter, r *http.Request ReturnStatusOK(w) } + +func localPatchChannel(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequireChannelId() + if c.Err != nil { + return + } + + patch := model.ChannelPatchFromJson(r.Body) + if patch == nil { + c.SetInvalidParam("channel") + return + } + + originalOldChannel, err := c.App.GetChannel(c.Params.ChannelId) + if err != nil { + c.Err = err + return + } + channel := originalOldChannel.DeepCopy() + + auditRec := c.MakeAuditRecord("localPatchChannel", audit.Fail) + defer c.LogAuditRec(auditRec) + auditRec.AddMeta("channel", channel) + + channel.Patch(patch) + rchannel, err := c.App.UpdateChannel(channel) + if err != nil { + c.Err = err + return + } + + err = c.App.FillInChannelProps(rchannel) + if err != nil { + c.Err = err + return + } + + auditRec.Success() + c.LogAudit("") + auditRec.AddMeta("patch", rchannel) + + w.Write([]byte(rchannel.ToJson())) +} diff --git a/api4/channel_test.go b/api4/channel_test.go index 89bd596d50..131e7953fd 100644 --- a/api4/channel_test.go +++ b/api4/channel_test.go @@ -282,11 +282,13 @@ func TestPatchChannel(t *testing.T) { _, resp = Client.PatchChannel(th.BasicChannel.Id, patch) CheckForbiddenStatus(t, resp) - _, resp = th.SystemAdminClient.PatchChannel(th.BasicChannel.Id, patch) - CheckNoError(t, resp) + th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { + _, resp = client.PatchChannel(th.BasicChannel.Id, patch) + CheckNoError(t, resp) - _, resp = th.SystemAdminClient.PatchChannel(th.BasicPrivateChannel.Id, patch) - CheckNoError(t, resp) + _, resp = client.PatchChannel(th.BasicPrivateChannel.Id, patch) + CheckNoError(t, resp) + }) // Test updating the header of someone else's GM channel. user1 := th.CreateUser()