Skip to content

Commit 1981ab7

Browse files
authored
feat: Add support for mark_unread msg or thread (#172)
Signed-off-by: Federico Guerinoni <[email protected]>
1 parent 03bae72 commit 1981ab7

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

stream_chat/async_chat/channel.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ async def mark_read(self, user_id: str, **data: Any) -> StreamResponse:
134134
payload = add_user_id(data, user_id)
135135
return await self.client.post(f"{self.url}/read", data=payload)
136136

137+
async def mark_unread(self, user_id: str, **data: Any) -> StreamResponse:
138+
payload = add_user_id(data, user_id)
139+
return await self.client.post(f"{self.url}/unread", data=payload)
140+
137141
async def get_replies(self, parent_id: str, **options: Any) -> StreamResponse:
138142
return await self.client.get(f"messages/{parent_id}/replies", params=options)
139143

stream_chat/base/channel.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,20 @@ def mark_read(
277277
"""
278278
pass
279279

280+
@abc.abstractmethod
281+
def mark_unread(
282+
self, user_id: str, **data: Any
283+
) -> Union[StreamResponse, Awaitable[StreamResponse]]:
284+
"""
285+
Marks channel as unread from a specific message or thread, if thread_id is provided in data
286+
a thread will be searched, otherwise a message.
287+
288+
:param user_id: the user ID for the event
289+
:param data: additional data, ie {"message_id": last_message_id} or {"thread_id": thread_id}
290+
:return: The server response
291+
"""
292+
pass
293+
280294
@abc.abstractmethod
281295
def get_replies(
282296
self, parent_id: str, **options: Any

stream_chat/channel.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ def mark_read(self, user_id: str, **data: Any) -> StreamResponse:
135135
payload = add_user_id(data, user_id)
136136
return self.client.post(f"{self.url}/read", data=payload)
137137

138+
def mark_unread(self, user_id: str, **data: Any) -> StreamResponse:
139+
payload = add_user_id(data, user_id)
140+
return self.client.post(f"{self.url}/unread", data=payload)
141+
138142
def get_replies(self, parent_id: str, **options: Any) -> StreamResponse:
139143
return self.client.get(f"messages/{parent_id}/replies", params=options)
140144

stream_chat/tests/async_chat/test_channel.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ async def test_mark_read(self, channel: Channel, random_user: Dict):
169169
assert "event" in response
170170
assert response["event"]["type"] == "message.read"
171171

172+
async def test_mark_unread(self, channel: Channel, random_user: Dict):
173+
msg = await channel.send_message({"text": "hi"}, random_user["id"])
174+
response = await channel.mark_unread(
175+
random_user["id"], message_id=msg["message"]["id"]
176+
)
177+
assert "duration" in response
178+
172179
async def test_get_replies(self, channel: Channel, random_user: Dict):
173180
msg = await channel.send_message({"text": "hi"}, random_user["id"])
174181
response = await channel.get_replies(msg["message"]["id"])

stream_chat/tests/test_channel.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ def test_mark_read(self, channel: Channel, random_user: Dict):
169169
assert "event" in response
170170
assert response["event"]["type"] == "message.read"
171171

172+
def test_mark_unread(self, channel: Channel, random_user: Dict):
173+
msg_id = str(uuid.uuid4())
174+
channel.send_message({"id": msg_id, "text": "helloworld"}, random_user["id"])
175+
response = channel.mark_unread(random_user["id"], message_id=msg_id)
176+
assert "duration" in response
177+
172178
def test_get_messages(self, channel: Channel, random_user: Dict):
173179
msg_id = str(uuid.uuid4())
174180
channel.send_message({"id": msg_id, "text": "helloworld"}, random_user["id"])

0 commit comments

Comments
 (0)