Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unread_count and unread_count_batch prep #153

Merged
merged 12 commits into from
Mar 1, 2024
6 changes: 6 additions & 0 deletions stream_chat/async_chat/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,12 @@ async def get_import(self, id: str) -> StreamResponse:
async def list_imports(self, options: Dict = None) -> StreamResponse:
return await self.get("imports", params=options)

async def unread_counts(self, user_id: str) -> StreamResponse:
return await self.get("unread", params={"user_id": user_id})

async def unread_counts_batch(self, user_ids: List[str]) -> StreamResponse:
return await self.post("unread_batch", data={"user_ids": user_ids})

async def close(self) -> None:
await self.session.close()

Expand Down
18 changes: 18 additions & 0 deletions stream_chat/base/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,24 @@ def list_imports(
"""
pass

@abc.abstractmethod
def unread_counts(
self, user_id: str
) -> Union[StreamResponse, Awaitable[StreamResponse]]:
"""
Get unread counts for a single user.
"""
pass

@abc.abstractmethod
def unread_counts_batch(
self, user_ids: List[str]
) -> Union[StreamResponse, Awaitable[StreamResponse]]:
"""
Get unread counts for multiple users at once.
"""
pass

#####################
# Private methods #
#####################
Expand Down
6 changes: 6 additions & 0 deletions stream_chat/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,9 @@ def get_import(self, id: str) -> StreamResponse:

def list_imports(self, options: Dict = None) -> StreamResponse:
return self.get("imports", params=options)

def unread_counts(self, user_id: str) -> StreamResponse:
return self.get("unread", params={"user_id": user_id})

def unread_counts_batch(self, user_ids: List[str]) -> StreamResponse:
return self.post("unread_batch", data={"user_ids": user_ids})
26 changes: 26 additions & 0 deletions stream_chat/tests/async_chat/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,3 +795,29 @@ async def test_imports_end2end(self, client: StreamChatAsync):

list_resp = await client.list_imports({"limit": 1})
assert len(list_resp["import_tasks"]) == 1

async def test_unread_counts(
self, client: StreamChatAsync, channel, random_user: Dict
):
await channel.add_members([random_user["id"]])
msg_id = str(uuid.uuid4())
await channel.send_message({"id": msg_id, "text": "helloworld"}, str(uuid.uuid4()))
JimmyPettersson85 marked this conversation as resolved.
Show resolved Hide resolved
response = await client.unread_counts(random_user["id"])
assert "total_unread_count" in response
assert "channels" in response
assert "channel_type" in response
assert response["total_unread_count"] == 1
assert len(response["channels"]) == 1
assert response["channels"][0]["channel_id"] == channel.cid
assert len(response["channel_type"]) == 1

# PREP for new endpoint
# async def test_unread_counts_batch(self, client: StreamChatAsync, channel, random_users: Dict):
# await channel.add_members([x["id"] for x in random_users])
# msg_id = str(uuid.uuid4())
# await channel.send_message({"id": msg_id, "text": "helloworld"}, str(uuid.uuid4()))
# response = await client.unread_counts_batch([x["id"] for x in random_users])
# assert "counts_by_user" in response
# for user_id in [x["id"] for x in random_users]:
# assert user_id in response["counts_by_user"]
# assert response["counts_by_user"][user_id]["total_unread_count"] == 1
24 changes: 24 additions & 0 deletions stream_chat/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,3 +775,27 @@ def test_imports_end2end(self, client: StreamChat):

list_resp = client.list_imports({"limit": 1})
assert len(list_resp["import_tasks"]) == 1

def test_unread_counts(self, client: StreamChat, channel, random_user: Dict):
channel.add_members([random_user["id"]])
msg_id = str(uuid.uuid4())
channel.send_message({"id": msg_id, "text": "helloworld"}, str(uuid.uuid4()))
response = client.unread_counts(random_user["id"])
assert "total_unread_count" in response
assert "channels" in response
assert "channel_type" in response
assert response["total_unread_count"] == 1
assert len(response["channels"]) == 1
assert response["channels"][0]["channel_id"] == channel.cid
assert len(response["channel_type"]) == 1

# PREP for new endpoint
# def test_unread_counts_batch(self, client: StreamChat, channel, random_users: Dict):
# channel.add_members([x["id"] for x in random_users])
# msg_id = str(uuid.uuid4())
# channel.send_message({"id": msg_id, "text": "helloworld"}, str(uuid.uuid4()))
# response = client.unread_counts_batch([x["id"] for x in random_users])
# assert "counts_by_user" in response
# for user_id in [x["id"] for x in random_users]:
# assert user_id in response["counts_by_user"]
# assert response["counts_by_user"][user_id]["total_unread_count"] == 1
Loading