Skip to content

Commit

Permalink
Add message/member limits to query channels (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferhatelmas authored Nov 10, 2020
1 parent 6e4e2ba commit f01ae82
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
18 changes: 12 additions & 6 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ type QueryOption struct {
Filter map[string]interface{} `json:"filter_conditions,omitempty"`
Sort []*SortOption `json:"sort,omitempty"`

UserID string `json:"user_id,omitempty"`
Limit int `json:"limit,omitempty"` // pagination option: limit number of results
Offset int `json:"offset,omitempty"` // pagination option: offset to return items from
UserID string `json:"user_id,omitempty"`
Limit int `json:"limit,omitempty"` // pagination option: limit number of results
Offset int `json:"offset,omitempty"` // pagination option: offset to return items from
MessageLimit int `json:"message_limit,omitempty"`
MemberLimit int `json:"member_limit,omitempty"`
}

type SortOption struct {
Expand All @@ -27,9 +29,11 @@ type queryRequest struct {
State bool `json:"state"`
Presence bool `json:"presence"`

UserID string `json:"user_id,omitempty"`
Limit int `json:"limit,omitempty"`
Offset int `json:"offset,omitempty"`
UserID string `json:"user_id,omitempty"`
Limit int `json:"limit,omitempty"`
Offset int `json:"offset,omitempty"`
MemberLimit int `json:"member_limit,omitempty"`
MessageLimit int `json:"message_limit,omitempty"`

FilterConditions map[string]interface{} `json:"filter_conditions,omitempty"`
Sort []*SortOption `json:"sort,omitempty"`
Expand Down Expand Up @@ -84,6 +88,8 @@ func (c *Client) QueryChannels(q *QueryOption, sort ...*SortOption) ([]*Channel,
UserID: q.UserID,
Limit: q.Limit,
Offset: q.Offset,
MemberLimit: q.MemberLimit,
MessageLimit: q.MessageLimit,
}

data, err := json.Marshal(&qp)
Expand Down
23 changes: 15 additions & 8 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,24 @@ func TestClient_QueryChannels(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)

got, err := c.QueryChannels(&QueryOption{Filter: map[string]interface{}{
"id": map[string]interface{}{
"$eq": ch.ID,
_, err := ch.SendMessage(&Message{Text: "abc"}, "some")
require.NoError(t, err)
_, err = ch.SendMessage(&Message{Text: "abc"}, "some")
require.NoError(t, err)

messageLimit := 1
got, err := c.QueryChannels(&QueryOption{
Filter: map[string]interface{}{
"id": map[string]interface{}{
"$eq": ch.ID,
},
},
}})
MessageLimit: messageLimit,
})

require.NoError(t, err, "query channels error")

if assert.NotEmpty(t, got, "query channels exists") {
assert.Equal(t, ch.ID, got[0].ID, "received channel ID")
}
require.Equal(t, ch.ID, got[0].ID, "received channel ID")
require.Len(t, got[0].Messages, messageLimit)
}

func TestClient_Search(t *testing.T) {
Expand Down

0 comments on commit f01ae82

Please sign in to comment.