Coverage for slidge/slixfix/delivery_receipt.py: 100%
20 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-08-18 04:30 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-08-18 04:30 +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 TYPE_CHECKING
10from slixmpp import JID, Message
11from slixmpp.types import MessageTypes
13if TYPE_CHECKING:
14 from slidge.core.gateway import BaseGateway
17class DeliveryReceipt:
18 xmpp: "BaseGateway"
20 def __init__(self, xmpp: "BaseGateway") -> None:
21 self.xmpp = xmpp
23 def ack(self, msg: Message) -> None:
24 """
25 Send a XEP-0184 (delivery receipt) in response to a message,
26 if appropriate.
28 :param msg:
29 """
30 if not self.requires_receipt(msg):
31 return
32 ack = self.make_ack(msg["id"], msg["to"], msg["from"].bare, msg["type"])
33 ack.send()
35 def make_ack(
36 self, msg_id: str, mfrom: JID, mto: JID, mtype: MessageTypes = "chat"
37 ) -> Message:
38 ack = self.xmpp.Message()
39 ack["type"] = mtype
40 ack["to"] = mto
41 ack["from"] = mfrom
42 ack["receipt"] = msg_id
43 return ack
45 def requires_receipt(self, msg: Message) -> bool:
46 """
47 Check if a message is eligible for a delivery receipt.
49 :param msg:
50 :return:
51 """
52 return (
53 msg["request_receipt"]
54 and msg["type"] in self.xmpp.plugin["xep_0184"].ack_types
55 and not msg["receipt"]
56 )