Skip to content

Commit 7d833ea

Browse files
authored
Add query members support (#37)
1 parent 956e02b commit 7d833ea

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

stream_chat/channel.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
13
from stream_chat.exceptions import StreamChannelException
24

35

@@ -94,6 +96,32 @@ def query(self, **options):
9496

9597
return state
9698

99+
def query_members(self, filter_conditions, sort=None, **options):
100+
"""
101+
Query the API for this channel to filter, sort and paginate its members efficiently.
102+
103+
:param filter_conditions: filters, checks docs on https://getstream.io/chat/docs/
104+
:param sort: sorting field and direction slice, check docs on https://getstream.io/chat/docs/
105+
:param options: pagination or members based channel searching details
106+
:return: Returns members response
107+
108+
eg.
109+
channel.query_members(filter_conditions={"name": "tommaso"},
110+
sort=[{"field": "created_at", "direction": -1}],
111+
offset=0,
112+
limit=10)
113+
"""
114+
115+
payload = {
116+
"id": self.id,
117+
"type": self.channel_type,
118+
"filter_conditions": filter_conditions,
119+
"sort": sort or [],
120+
**options,
121+
}
122+
response = self.client.get("members", params={"payload": json.dumps(payload)})
123+
return response["members"]
124+
97125
def update(self, channel_data, update_message=None):
98126
"""
99127
Edit the channel's custom properties

stream_chat/tests/test_channel.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,20 @@ def test_invites(self, client, channel):
224224
# cannot reject again
225225
with pytest.raises(StreamAPIException):
226226
reject = channel.reject_invite("eric")
227+
228+
def test_query_members(self, client, channel):
229+
members = ["paul", "george", "john", "jessica", "john2"]
230+
client.update_users([{"id": m, "name": m} for m in members])
231+
for member in members:
232+
channel.add_members([member])
233+
234+
response = channel.query_members(
235+
filter_conditions={"name": {"$autocomplete": "j"}},
236+
sort=[{"field": "created_at", "direction": 1}],
237+
offset=1,
238+
limit=10,
239+
)
240+
241+
assert len(response) == 2
242+
assert response[0]["user_id"] == "jessica"
243+
assert response[1]["user_id"] == "john2"

0 commit comments

Comments
 (0)