Skip to content

Commit

Permalink
[CHAT-1477] partial channel update (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
thesyncim authored Dec 19, 2020
1 parent a119859 commit 349ea74
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ type Channel struct {
client *Client
}

type PartialUpdate struct {
Set map[string]interface{} `json:"set"`
Unset []string `json:"unset"`
}

type channelForJSON Channel

// UnmarshalJSON implements json.Unmarshaler.
Expand Down Expand Up @@ -155,6 +160,13 @@ func (ch *Channel) Update(options map[string]interface{}, message *Message) erro
return ch.client.makeRequest(http.MethodPost, p, nil, payload, nil)
}

// PartialUpdate set and unset specific fields when it is necessary to retain additional custom data fields on the object. AKA a patch style update.
// options: the object to update the custom properties of the channel
func (ch *Channel) PartialUpdate(update PartialUpdate) error {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
return ch.client.makeRequest(http.MethodPatch, p, nil, update, nil)
}

// Delete removes the channel. Messages are permanently removed.
func (ch *Channel) Delete() error {
p := path.Join("channels", url.PathEscape(ch.Type), url.PathEscape(ch.ID))
Expand Down
30 changes: 30 additions & 0 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,36 @@ func TestChannel_Update(t *testing.T) {
require.NoError(t, err)
}

func TestChannel_PartialUpdate(t *testing.T) {
c := initClient(t)
_, err := c.UpdateUsers(testUsers...)
require.NoError(t, err, "update users")

members := make([]string, 0, len(testUsers))
for i := range testUsers {
members = append(members, testUsers[i].ID)
}

var ch *Channel
ch, err = c.CreateChannel("team", randomString(12), serverUser.ID, map[string]interface{}{
"members": members,
"color": "blue",
"age": 30,
})
require.NoError(t, err)
err = ch.PartialUpdate(PartialUpdate{
Set: map[string]interface{}{
"color": "red",
},
Unset: []string{"age"},
})
require.NoError(t, err)
err = ch.refresh()
require.NoError(t, err)
require.Equal(t, "red", ch.ExtraData["color"])
require.Equal(t, nil, ch.ExtraData["age"])
}

func TestChannel_AddModerators(t *testing.T) {
}

Expand Down

0 comments on commit 349ea74

Please sign in to comment.