Coverage for slidge / slixfix / delivery_receipt.py: 100%
22 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-20 19:56 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-20 19:56 +0000
1"""
2XEP-0184 Delivery Receipts
4The corresponding slixmpp module is a bit too rigid, this is our implementation
5to selectively choose when we send delivery receipts
6"""
8from typing import Generic, TypeVar
10from slixmpp import JID, Message
11from slixmpp.types import MessageTypes
13GatewayType = TypeVar("GatewayType")
16class DeliveryReceipt(Generic[GatewayType]):
17 xmpp: GatewayType
19 def __init__(self, xmpp: GatewayType) -> None:
20 self.xmpp = xmpp
22 def ack(self, msg: Message) -> None:
23 """
24 Send a XEP-0184 (delivery receipt) in response to a message,
25 if appropriate.
27 :param msg:
28 """
29 if not self.requires_receipt(msg):
30 return
31 ack = self.make_ack(msg["id"], msg["to"], msg["from"].bare, msg["type"])
32 ack.send()
34 def make_ack(
35 self, msg_id: str, mfrom: JID, mto: JID, mtype: MessageTypes = "chat"
36 ) -> Message:
37 ack = self.xmpp.Message()
38 ack["type"] = mtype
39 ack["to"] = mto
40 ack["from"] = mfrom
41 ack["receipt"] = msg_id
42 return ack
44 def requires_receipt(self, msg: Message) -> bool:
45 """
46 Check if a message is eligible for a delivery receipt.
48 :param msg:
49 :return:
50 """
51 return (
52 msg["request_receipt"]
53 and msg["type"] in self.xmpp.plugin["xep_0184"].ack_types
54 and not msg["receipt"]
55 )