Skip to content

Commit

Permalink
Add query members support (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferhatelmas authored Sep 18, 2020
1 parent b19b1b9 commit be666d8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
37 changes: 37 additions & 0 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stream_chat //nolint: golint
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -211,6 +212,42 @@ func (ch *Channel) RemoveMembers(userIDs []string, message *Message) error {
return nil
}

type queryMembersResponse struct {
Members []*ChannelMember `json:"members"`
}

// QueryMembers queries members of a channel.
func (ch *Channel) QueryMembers(q *QueryOption, sorters ...*SortOption) ([]*ChannelMember, error) {
qp := map[string]interface{}{
"id": ch.ID,
"type": ch.Type,
"filter_conditions": q.Filter,
"limit": q.Limit,
"offset": q.Offset,
"sort": sorters,
}

if len(ch.Members) > 0 {
qp["members"] = ch.Members
}

data, err := json.Marshal(&qp)
if err != nil {
return nil, err
}

fmt.Println(string(data))

values := url.Values{}
values.Set("payload", string(data))

var resp queryMembersResponse
if err := ch.client.makeRequest(http.MethodGet, "members", values, nil, &resp); err != nil {
return nil, err
}
return resp.Members, nil
}

// AddModerators adds moderators with given IDs to the channel.
func (ch *Channel) AddModerators(userIDs ...string) error {
return ch.addModerators(userIDs, nil)
Expand Down
37 changes: 37 additions & 0 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,43 @@ func TestChannel_AddMembers(t *testing.T) {
assert.Equal(t, user.ID, ch.Members[0].User.ID, "members contain user id")
}

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

chanID := randomString(12)

ch, err := c.CreateChannel("messaging", chanID, serverUser.ID, nil)
require.NoError(t, err, "create channel")
defer func() {
_ = ch.Delete()
}()

assert.Empty(t, ch.Members, "members are empty")

prefix := randomString(12)
names := []string{"paul", "george", "john", "jessica", "john2"}

for _, name := range names {
id := prefix + name
_, err := c.UpdateUser(&User{ID: id, Name: id})
require.NoError(t, err)
require.NoError(t, ch.AddMembers([]string{id}, nil))
}

members, err := ch.QueryMembers(&QueryOption{
Filter: map[string]interface{}{
"name": map[string]interface{}{"$autocomplete": prefix + "j"},
},
Offset: 1,
Limit: 10,
}, &SortOption{Field: "created_at", Direction: 1})

require.NoError(t, err)
require.Len(t, members, 2)
require.Equal(t, prefix+"jessica", members[0].UserID)
require.Equal(t, prefix+"john2", members[1].UserID)
}

// See https://getstream.io/chat/docs/channel_members/ for more details.
func ExampleChannel_AddModerators() {
channel := &Channel{}
Expand Down

0 comments on commit be666d8

Please sign in to comment.