Coverage for slidge/slixfix/delivery_receipt.py: 100%
20 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-11-07 05:11 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-11-07 05:11 +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 def __init__(self, xmpp: "BaseGateway"):
19 self.xmpp = xmpp
21 def ack(self, msg: Message):
22 """
23 Send a XEP-0184 (delivery receipt) in response to a message,
24 if appropriate.
26 :param msg:
27 """
28 if not self.requires_receipt(msg):
29 return
30 ack = self.make_ack(msg["id"], msg["to"], msg["from"].bare, msg["type"])
31 ack.send()
33 def make_ack(self, msg_id: str, mfrom: JID, mto: JID, mtype: MessageTypes = "chat"):
34 ack = self.xmpp.Message()
35 ack["type"] = mtype
36 ack["to"] = mto
37 ack["from"] = mfrom
38 ack["receipt"] = msg_id
39 return ack
41 def requires_receipt(self, msg: Message):
42 """
43 Check if a message is eligible for a delivery receipt.
45 :param msg:
46 :return:
47 """
48 return (
49 msg["request_receipt"]
50 and msg["type"] in self.xmpp.plugin["xep_0184"].ack_types
51 and not msg["receipt"]
52 )