Coverage for slidge/core/dispatcher/message/marker.py: 81%

43 statements  

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

1from slixmpp import JID, Message 

2from slixmpp.xmlstream import StanzaBase 

3 

4from ....group.room import LegacyMUC 

5from ....util.types import Recipient 

6from ..util import DispatcherMixin, exceptions_to_xmpp_errors, get_recipient 

7 

8 

9class MarkerMixin(DispatcherMixin): 

10 __slots__: list[str] = [] 

11 

12 def __init__(self, xmpp) -> None: 

13 super().__init__(xmpp) 

14 xmpp.add_event_handler("marker_displayed", self.on_marker_displayed) 

15 xmpp.add_event_handler( 

16 "message_displayed_synchronization_publish", 

17 self.on_message_displayed_synchronization_publish, 

18 ) 

19 

20 @exceptions_to_xmpp_errors 

21 async def on_marker_displayed(self, msg: StanzaBase) -> None: 

22 assert isinstance(msg, Message) 

23 session = await self._get_session(msg) 

24 

25 e: Recipient = await get_recipient(session, msg) 

26 legacy_thread = await self._xmpp_to_legacy_thread(session, msg, e) 

27 displayed_msg_id = msg["displayed"]["id"] 

28 if not isinstance(e, LegacyMUC) and self.xmpp.MARK_ALL_MESSAGES: 

29 to_mark = e.get_msg_xmpp_id_up_to(displayed_msg_id) 

30 if to_mark is None: 

31 session.log.debug("Can't mark all messages up to %s", displayed_msg_id) 

32 to_mark = [displayed_msg_id] 

33 else: 

34 to_mark = [displayed_msg_id] 

35 for xmpp_id in to_mark: 

36 await session.on_displayed( 

37 e, self._xmpp_msg_id_to_legacy(session, xmpp_id, e), legacy_thread 

38 ) 

39 if isinstance(e, LegacyMUC): 

40 await e.echo(msg, None) 

41 

42 @exceptions_to_xmpp_errors 

43 async def on_message_displayed_synchronization_publish( 

44 self, msg: StanzaBase 

45 ) -> None: 

46 assert isinstance(msg, Message) 

47 chat_jid = JID(msg["pubsub_event"]["items"]["item"]["id"]) 

48 if chat_jid.server != self.xmpp.boundjid.bare: 

49 return 

50 

51 session = await self._get_session(msg, timeout=None) 

52 

53 if chat_jid == self.xmpp.boundjid.bare: 

54 return 

55 

56 chat = await session.get_contact_or_group_or_participant(chat_jid) 

57 if not isinstance(chat, LegacyMUC): 

58 session.log.debug("Ignoring non-groupchat MDS event") 

59 return 

60 

61 stanza_id = msg["pubsub_event"]["items"]["item"]["displayed"]["stanza_id"]["id"] 

62 await session.on_displayed( 

63 chat, self._xmpp_msg_id_to_legacy(session, stanza_id, chat) 

64 )