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

42 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-11-07 05:11 +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, _get_entity, exceptions_to_xmpp_errors 

7 

8 

9class MarkerMixin(DispatcherMixin): 

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

11 super().__init__(xmpp) 

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

13 xmpp.add_event_handler( 

14 "message_displayed_synchronization_publish", 

15 self.on_message_displayed_synchronization_publish, 

16 ) 

17 

18 @exceptions_to_xmpp_errors 

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

20 assert isinstance(msg, Message) 

21 session = await self._get_session(msg) 

22 

23 e: Recipient = await _get_entity(session, msg) 

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

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

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

27 to_mark = e.get_msg_xmpp_id_up_to(displayed_msg_id) # type: ignore 

28 if to_mark is None: 

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

30 to_mark = [displayed_msg_id] 

31 else: 

32 to_mark = [displayed_msg_id] 

33 for xmpp_id in to_mark: 

34 await session.on_displayed( 

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

36 ) 

37 if isinstance(e, LegacyMUC): 

38 await e.echo(msg, None) 

39 

40 @exceptions_to_xmpp_errors 

41 async def on_message_displayed_synchronization_publish( 

42 self, msg: StanzaBase 

43 ) -> None: 

44 assert isinstance(msg, Message) 

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

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

47 return 

48 

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

50 

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

52 return 

53 

54 chat = await session.get_contact_or_group_or_participant(chat_jid) 

55 if not isinstance(chat, LegacyMUC): 

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

57 return 

58 

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

60 await session.on_displayed( 

61 chat, self._xmpp_msg_id_to_legacy(session, stanza_id) 

62 )