Skip to content

Commit 8ece3b1

Browse files
author
Rafael Marinho
committed
fix
1 parent 55a5d79 commit 8ece3b1

File tree

9 files changed

+171
-37
lines changed

9 files changed

+171
-37
lines changed

stream_chat/async_chat/channel.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,6 @@ async def mark_unread(self, user_id: str, **data: Any) -> StreamResponse:
139139
payload = add_user_id(data, user_id)
140140
return await self.client.post(f"{self.url}/unread", data=payload)
141141

142-
async def mark_delivered(self, data: Dict[str, Any]) -> Optional[StreamResponse]:
143-
return await self.client.post("channels/delivered", data=data)
144-
145142
async def get_replies(self, parent_id: str, **options: Any) -> StreamResponse:
146143
return await self.client.get(f"messages/{parent_id}/replies", params=options)
147144

stream_chat/async_chat/client.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,51 @@ async def update_user_location(
986986
params = {"user_id": user_id, **options}
987987
return await self.put("users/live_locations", data=data, params=params)
988988

989+
async def mark_delivered(self, data: Dict[str, Any]) -> Optional[StreamResponse]:
990+
"""
991+
Send the mark delivered event for this user, only works if the `delivery_receipts` setting is enabled
992+
993+
:param data: MarkDeliveredOptions containing latest_delivered_messages and other optional fields
994+
:return: The server response or None if delivery receipts are disabled
995+
"""
996+
# Validate required fields
997+
if not data.get("latest_delivered_messages"):
998+
raise ValueError("latest_delivered_messages must not be empty")
999+
1000+
# Ensure either user or user_id is provided
1001+
if not data.get("user") and not data.get("user_id"):
1002+
raise ValueError("either user or user_id must be provided")
1003+
1004+
return await self.post("channels/delivered", data=data)
1005+
1006+
async def mark_delivered_simple(self, user_id: str, message_id: str, channel_cid: str) -> Optional[StreamResponse]:
1007+
"""
1008+
Convenience method to mark a message as delivered for a specific user.
1009+
1010+
:param user_id: The user ID
1011+
:param message_id: The message ID
1012+
:param channel_cid: The channel CID (channel_type:channel_id)
1013+
:return: The server response or None if delivery receipts are disabled
1014+
"""
1015+
if not user_id:
1016+
raise ValueError("user ID must not be empty")
1017+
if not message_id:
1018+
raise ValueError("message ID must not be empty")
1019+
if not channel_cid:
1020+
raise ValueError("channel CID must not be empty")
1021+
1022+
data = {
1023+
"latest_delivered_messages": [
1024+
{
1025+
"cid": channel_cid,
1026+
"id": message_id
1027+
}
1028+
],
1029+
"user_id": user_id
1030+
}
1031+
1032+
return await self.mark_delivered(data)
1033+
9891034
async def close(self) -> None:
9901035
await self.session.close()
9911036

stream_chat/base/channel.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -293,18 +293,6 @@ def mark_unread(
293293
"""
294294
pass
295295

296-
@abc.abstractmethod
297-
def mark_delivered(
298-
self, data: Dict[str, Any]
299-
) -> Union[StreamResponse, Awaitable[StreamResponse], None]:
300-
"""
301-
Send the mark delivered event for this user, only works if the `delivery_receipts` setting is enabled
302-
303-
:param data: MarkDeliveredOptions containing channel_delivered_message and other optional fields
304-
:return: The server response or None if delivery receipts are disabled
305-
"""
306-
pass
307-
308296
@abc.abstractmethod
309297
def get_replies(
310298
self, parent_id: str, **options: Any

stream_chat/base/client.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,32 @@ def update_user_location(
15371537
"""
15381538
pass
15391539

1540+
@abc.abstractmethod
1541+
def mark_delivered(
1542+
self, data: Dict[str, Any]
1543+
) -> Union[StreamResponse, Awaitable[StreamResponse], None]:
1544+
"""
1545+
Send the mark delivered event for this user, only works if the `delivery_receipts` setting is enabled
1546+
1547+
:param data: MarkDeliveredOptions containing latest_delivered_messages and other optional fields
1548+
:return: The server response or None if delivery receipts are disabled
1549+
"""
1550+
pass
1551+
1552+
@abc.abstractmethod
1553+
def mark_delivered_simple(
1554+
self, user_id: str, message_id: str, channel_cid: str
1555+
) -> Union[StreamResponse, Awaitable[StreamResponse], None]:
1556+
"""
1557+
Convenience method to mark a message as delivered for a specific user.
1558+
1559+
:param user_id: The user ID
1560+
:param message_id: The message ID
1561+
:param channel_cid: The channel CID (channel_type:channel_id)
1562+
:return: The server response or None if delivery receipts are disabled
1563+
"""
1564+
pass
1565+
15401566
#####################
15411567
# Private methods #
15421568
#####################

stream_chat/channel.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,6 @@ def mark_unread(self, user_id: str, **data: Any) -> StreamResponse:
140140
payload = add_user_id(data, user_id)
141141
return self.client.post(f"{self.url}/unread", data=payload)
142142

143-
def mark_delivered(self, data: Dict[str, Any]) -> Optional[StreamResponse]:
144-
return self.client.post("channels/delivered", data=data)
145-
146143
def get_replies(self, parent_id: str, **options: Any) -> StreamResponse:
147144
return self.client.get(f"messages/{parent_id}/replies", params=options)
148145

stream_chat/client.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,3 +939,48 @@ def update_user_location(
939939
data.update(cast(dict, options))
940940
params = {"user_id": user_id, **options}
941941
return self.put("users/live_locations", data=data, params=params)
942+
943+
def mark_delivered(self, data: Dict[str, Any]) -> Optional[StreamResponse]:
944+
"""
945+
Send the mark delivered event for this user, only works if the `delivery_receipts` setting is enabled
946+
947+
:param data: MarkDeliveredOptions containing latest_delivered_messages and other optional fields
948+
:return: The server response or None if delivery receipts are disabled
949+
"""
950+
# Validate required fields
951+
if not data.get("latest_delivered_messages"):
952+
raise ValueError("latest_delivered_messages must not be empty")
953+
954+
# Ensure either user or user_id is provided
955+
if not data.get("user") and not data.get("user_id"):
956+
raise ValueError("either user or user_id must be provided")
957+
958+
return self.post("channels/delivered", data=data)
959+
960+
def mark_delivered_simple(self, user_id: str, message_id: str, channel_cid: str) -> Optional[StreamResponse]:
961+
"""
962+
Convenience method to mark a message as delivered for a specific user.
963+
964+
:param user_id: The user ID
965+
:param message_id: The message ID
966+
:param channel_cid: The channel CID (channel_type:channel_id)
967+
:return: The server response or None if delivery receipts are disabled
968+
"""
969+
if not user_id:
970+
raise ValueError("user ID must not be empty")
971+
if not message_id:
972+
raise ValueError("message ID must not be empty")
973+
if not channel_cid:
974+
raise ValueError("channel CID must not be empty")
975+
976+
data = {
977+
"latest_delivered_messages": [
978+
{
979+
"cid": channel_cid,
980+
"id": message_id
981+
}
982+
],
983+
"user_id": user_id
984+
}
985+
986+
return self.mark_delivered(data)

stream_chat/tests/test_channel.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -203,25 +203,6 @@ def test_mark_unread(self, channel: Channel, random_user: Dict):
203203
response = channel.mark_unread(random_user["id"], message_id=msg_id)
204204
assert "duration" in response
205205

206-
def test_mark_delivered(self, channel: Channel, random_user: Dict):
207-
delivery_data = {
208-
"channel_delivered_message": {channel.id: "test-message-id"},
209-
"user_id": random_user["id"]
210-
}
211-
212-
response = channel.mark_delivered(delivery_data)
213-
assert response is not None
214-
215-
delivery_data_with_options = {
216-
"channel_delivered_message": {channel.id: "test-message-id-2"},
217-
"user_id": random_user["id"],
218-
"client_id": "test-client",
219-
"connection_id": "test-connection"
220-
}
221-
222-
response = channel.mark_delivered(delivery_data_with_options)
223-
assert response is not None
224-
225206
def test_get_messages(self, channel: Channel, random_user: Dict):
226207
msg_id = str(uuid.uuid4())
227208
channel.send_message({"id": msg_id, "text": "helloworld"}, random_user["id"])

stream_chat/tests/test_client.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,3 +1016,58 @@ def test_query_message_history(
10161016

10171017
assert len(response_next["message_history"]) == 1
10181018
assert response_next["message_history"][0]["text"] == "helloworld-2"
1019+
1020+
def test_mark_delivered(self, client: StreamChat, channel: Channel, random_user: Dict):
1021+
"""Test marking messages as delivered"""
1022+
# Test with basic delivery data using new format
1023+
delivery_data = {
1024+
"latest_delivered_messages": [
1025+
{
1026+
"cid": channel.cid,
1027+
"id": "test-message-id"
1028+
}
1029+
],
1030+
"user_id": random_user["id"]
1031+
}
1032+
1033+
response = client.mark_delivered(delivery_data)
1034+
assert response is not None
1035+
1036+
# Test with multiple messages
1037+
delivery_data_multiple = {
1038+
"latest_delivered_messages": [
1039+
{
1040+
"cid": channel.cid,
1041+
"id": "test-message-id-1"
1042+
},
1043+
{
1044+
"cid": channel.cid,
1045+
"id": "test-message-id-2"
1046+
}
1047+
],
1048+
"user_id": random_user["id"]
1049+
}
1050+
1051+
response = client.mark_delivered(delivery_data_multiple)
1052+
assert response is not None
1053+
1054+
def test_mark_delivered_simple(self, client: StreamChat, channel: Channel, random_user: Dict):
1055+
"""Test the convenience method for marking messages as delivered"""
1056+
response = client.mark_delivered_simple(
1057+
user_id=random_user["id"],
1058+
message_id="test-message-id",
1059+
channel_cid=channel.cid
1060+
)
1061+
assert response is not None
1062+
1063+
def test_mark_delivered_validation(self, client: StreamChat, random_user: Dict):
1064+
"""Test validation of mark_delivered method"""
1065+
# Test empty latest_delivered_messages
1066+
with pytest.raises(ValueError, match="latest_delivered_messages must not be empty"):
1067+
client.mark_delivered({"user_id": random_user["id"]})
1068+
1069+
# Test missing user and user_id
1070+
with pytest.raises(ValueError, match="either user or user_id must be provided"):
1071+
client.mark_delivered({
1072+
"latest_delivered_messages": [{"cid": "test:channel", "id": "test"}]
1073+
})

stream_chat/types/delivery_receipts.py

Whitespace-only changes.

0 commit comments

Comments
 (0)