Coverage for slidge/slixfix/delivery_receipt.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-04 08:17 +0000

1""" 

2XEP-0184 Delivery Receipts 

3 

4The corresponding slixmpp module is a bit too rigid, this is our implementation 

5to selectively choose when we send delivery receipts 

6""" 

7 

8from typing import TYPE_CHECKING 

9 

10from slixmpp import JID, Message 

11from slixmpp.types import MessageTypes 

12 

13if TYPE_CHECKING: 

14 from slidge.core.gateway import BaseGateway 

15 

16 

17class DeliveryReceipt: 

18 def __init__(self, xmpp: "BaseGateway") -> None: 

19 self.xmpp = xmpp 

20 

21 def ack(self, msg: Message) -> None: 

22 """ 

23 Send a XEP-0184 (delivery receipt) in response to a message, 

24 if appropriate. 

25 

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() 

32 

33 def make_ack( 

34 self, msg_id: str, mfrom: JID, mto: JID, mtype: MessageTypes = "chat" 

35 ) -> Message: 

36 ack = self.xmpp.Message() 

37 ack["type"] = mtype 

38 ack["to"] = mto 

39 ack["from"] = mfrom 

40 ack["receipt"] = msg_id 

41 return ack 

42 

43 def requires_receipt(self, msg: Message) -> bool: 

44 """ 

45 Check if a message is eligible for a delivery receipt. 

46 

47 :param msg: 

48 :return: 

49 """ 

50 return ( 

51 msg["request_receipt"] 

52 and msg["type"] in self.xmpp.plugin["xep_0184"].ack_types 

53 and not msg["receipt"] 

54 )