Skip to content

Commit

Permalink
Deprecate UpdateUser (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferhatelmas authored Jan 22, 2021
1 parent 9a58bc3 commit d80c989
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 21 deletions.
9 changes: 5 additions & 4 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (ch *Channel) query(options, data map[string]interface{}) (err error) {
return nil
}

// Update edits the channel's custom properties
// Update edits the channel's custom properties.
//
// options: the object to update the custom properties of this channel with
// message: optional update message
Expand Down Expand Up @@ -377,7 +377,8 @@ func (ch *Channel) demoteModerators(userIDs []string, msg *Message) error {
return ch.client.makeRequest(http.MethodPost, p, nil, data, nil)
}

// MarkRead send the mark read event for user with given ID, only works if the `read_events` setting is enabled
// MarkRead send the mark read event for user with given ID,
// only works if the `read_events` setting is enabled.
// options: additional data, ie {"messageID": last_messageID}
func (ch *Channel) MarkRead(userID string, options map[string]interface{}) error {
switch {
Expand All @@ -395,8 +396,8 @@ func (ch *Channel) MarkRead(userID string, options map[string]interface{}) error
}

// BanUser bans target user ID from this channel
// userID: user who bans target
// options: additional ban options, ie {"timeout": 3600, "reason": "offensive language is not allowed here"}
// userID: user who bans target.
// options: additional ban options, ie {"timeout": 3600, "reason": "offensive language is not allowed here"}.
func (ch *Channel) BanUser(targetID, userID string, options map[string]interface{}) error {
switch {
case targetID == "":
Expand Down
10 changes: 5 additions & 5 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestChannel_QueryMembers(t *testing.T) {

for _, name := range names {
id := prefix + name
_, err := c.UpdateUser(&User{ID: id, Name: id})
_, err := c.UpsertUser(&User{ID: id, Name: id})
require.NoError(t, err)
require.NoError(t, ch.AddMembers([]string{id}, nil))
}
Expand Down Expand Up @@ -391,7 +391,7 @@ func TestChannel_Update(t *testing.T) {

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

members := make([]string, 0, len(testUsers))
Expand Down Expand Up @@ -500,7 +500,7 @@ func TestChannel_SendImage(t *testing.T) {
func TestChannel_AcceptInvite(t *testing.T) {
c := initClient(t)

_, err := c.UpdateUsers(testUsers...)
_, err := c.UpsertUsers(testUsers...)
require.NoError(t, err, "update users")

members := make([]string, 0, len(testUsers))
Expand All @@ -521,7 +521,7 @@ func TestChannel_AcceptInvite(t *testing.T) {
func TestChannel_RejectInvite(t *testing.T) {
c := initClient(t)

_, err := c.UpdateUsers(testUsers...)
_, err := c.UpsertUsers(testUsers...)
require.NoError(t, err, "update users")

members := make([]string, 0, len(testUsers))
Expand All @@ -542,7 +542,7 @@ func TestChannel_RejectInvite(t *testing.T) {
func TestChannel_Mute_Unmute(t *testing.T) {
c := initClient(t)

_, err := c.UpdateUsers(testUsers...)
_, err := c.UpsertUsers(testUsers...)
require.NoError(t, err, "update users")

members := make([]string, 0, len(testUsers))
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func initClient(t *testing.T) *Client {
}

func initChannel(t *testing.T, c *Client) *Channel {
_, err := c.UpdateUsers(testUsers...)
_, err := c.UpsertUsers(testUsers...)
require.NoError(t, err, "update users")

members := make([]string, 0, len(testUsers))
Expand Down
2 changes: 1 addition & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ type repliesResponse struct {
Messages []*Message `json:"messages"`
}

// GetReplies returns list of the message replies for a parent message
// GetReplies returns list of the message replies for a parent message.
// options: Pagination params, ie {limit:10, idlte: 10}
func (ch *Channel) GetReplies(parentID string, options map[string][]string) ([]*Message, error) {
if parentID == "" {
Expand Down
2 changes: 1 addition & 1 deletion query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestClient_QueryUsers(t *testing.T) {

for i := n - 1; i > -1; i-- {
u := &User{ID: randomString(30), ExtraData: map[string]interface{}{"order": n - i - 1}}
_, err := c.UpdateUser(u)
_, err := c.UpsertUser(u)
require.NoError(t, err)
ids[i] = u.ID
time.Sleep(200 * time.Millisecond)
Expand Down
2 changes: 1 addition & 1 deletion run-lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ gopath="$(go env GOPATH)"
if ! [[ -x "$gopath/bin/golangci-lint" ]]; then
echo >&2 'Installing golangci-lint'
curl --silent --fail --location \
https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "$gopath/bin" v1.31.0
https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "$gopath/bin" v1.33.0
fi

# configured by .golangci.yml
Expand Down
23 changes: 19 additions & 4 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,22 @@ type userRequest struct {
LastActive time.Time `json:"-"`
}

// UpsertUser is a single user version of UpsertUsers for convenience.
func (c *Client) UpsertUser(user *User) (*User, error) {
users, err := c.UpsertUsers(user)
return users[user.ID], err
}

// UpdateUser sending update users request, returns updated user info.
//
// Deprecated: Use UpsertUser. Renamed for clarification, functionality remains the same.
func (c *Client) UpdateUser(user *User) (*User, error) {
users, err := c.UpdateUsers(user)
return users[user.ID], err
return c.UpsertUser(user)
}

// UpdateUsers send update users request, returns updated user info.
func (c *Client) UpdateUsers(users ...*User) (map[string]*User, error) {
// UpsertUsers creates the given users. If a user doesn't exist, it will be created.
// Otherwise, custom data will be extended or updated. Missing data is never removed.
func (c *Client) UpsertUsers(users ...*User) (map[string]*User, error) {
if len(users) == 0 {
return nil, errors.New("users are not set")
}
Expand All @@ -297,6 +305,13 @@ func (c *Client) UpdateUsers(users ...*User) (map[string]*User, error) {
return resp.Users, err
}

// UpdateUsers sends update user request, returns updated user info.
//
// Deprecated: Use UpsertUsers. Renamed for clarification, functionality remains the same.
func (c *Client) UpdateUsers(users ...*User) (map[string]*User, error) {
return c.UpsertUsers(users...)
}

// PartialUserUpdate request; Set and Unset fields can be set at same time, but should not be same field,
// for example you cannot set 'field.path.name' and unset 'field.path' at the same time.
// Field path should not contain spaces or dots (dot is path separator).
Expand Down
8 changes: 4 additions & 4 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ func TestClient_UnmuteUsers(t *testing.T) {
assert.NoError(t, err, "unmute users")
}

func TestClient_UpdateUsers(t *testing.T) {
func TestClient_UpsertUsers(t *testing.T) {
c := initClient(t)

user := randomUser()

resp, err := c.UpdateUsers(user)
resp, err := c.UpsertUsers(user)
require.NoError(t, err, "update users")

assert.Contains(t, resp, user.ID)
Expand Down Expand Up @@ -160,10 +160,10 @@ func TestClient_PartialUpdateUsers(t *testing.T) {
assert.Empty(t, got[user.ID].ExtraData["test"], "extra data field removed")
}

func ExampleClient_UpdateUser() {
func ExampleClient_UpsertUser() {
client, _ := NewClient("XXXX", "XXXX")

_, err := client.UpdateUser(&User{
_, err := client.UpsertUser(&User{
ID: "tommaso",
Name: "Tommaso",
Role: "Admin",
Expand Down

0 comments on commit d80c989

Please sign in to comment.