Mercurial > libervia-backend
comparison libervia/backend/plugins/plugin_xep_0444.py @ 4366:1ef32316a55e
plugin XEP-0444, storage: move `get_history_from_xmpp_id` to storage as it is generally useful.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 06 May 2025 00:33:57 +0200 |
parents | 5d2de6c1156d |
children |
comparison
equal
deleted
inserted
replaced
4365:f6672bc80897 | 4366:1ef32316a55e |
---|---|
73 host.trigger.add("message_received", self._message_received_trigger) | 73 host.trigger.add("message_received", self._message_received_trigger) |
74 | 74 |
75 def get_handler(self, client): | 75 def get_handler(self, client): |
76 return XEP_0444_Handler() | 76 return XEP_0444_Handler() |
77 | 77 |
78 @aio | |
79 async def get_history_from_reaction_id( | |
80 self, client: SatXMPPEntity, message_elt: domish.Element, reaction_id: str | |
81 ) -> History | None: | |
82 """Retrieves history rows that match a specific reaction_id. | |
83 | |
84 The retrieval criteria vary based on the message type, according to XEP-0444. | |
85 | |
86 @param message_elt: The message element, used to determine the type of message. | |
87 @param reaction_id: The reaction ID to match in the history. | |
88 """ | |
89 profile_id = self.host.memory.storage.profiles[client.profile] | |
90 async with self.host.memory.storage.session() as session: | |
91 if message_elt.getAttribute("type") == C.MESS_TYPE_GROUPCHAT: | |
92 query = select(History).where( | |
93 History.profile_id == profile_id, History.stanza_id == reaction_id | |
94 ) | |
95 else: | |
96 query = select(History).where( | |
97 History.profile_id == profile_id, History.origin_id == reaction_id | |
98 ) | |
99 | |
100 result = await session.execute(query) | |
101 history = result.scalars().first() | |
102 | |
103 return history | |
104 | |
105 async def _message_received_trigger( | 78 async def _message_received_trigger( |
106 self, | 79 self, |
107 client: SatXMPPEntity, | 80 client: SatXMPPEntity, |
108 message_elt: domish.Element, | 81 message_elt: domish.Element, |
109 post_treat: defer.Deferred, | 82 post_treat: defer.Deferred, |
110 ) -> bool: | 83 ) -> bool: |
111 reactions_elt = next(message_elt.elements(NS_REACTIONS, "reactions"), None) | 84 reactions_elt = next(message_elt.elements(NS_REACTIONS, "reactions"), None) |
112 if reactions_elt is not None: | 85 if reactions_elt is not None: |
113 reaction_id = reactions_elt.getAttribute("id") | 86 reaction_id = reactions_elt.getAttribute("id") |
114 history = await self.get_history_from_reaction_id( | 87 message_type = message_elt.getAttribute("type") |
115 client, message_elt, reaction_id | 88 history = await self.host.memory.storage.get_history_from_xmpp_id( |
89 client, reaction_id, message_type | |
116 ) | 90 ) |
117 if history is None: | 91 if history is None: |
118 log.warning( | 92 log.warning( |
119 f"Can't find matching message for reaction: {reactions_elt.toXml()}" | 93 f"Can't find matching message for reaction: {reactions_elt.toXml()}" |
120 ) | 94 ) |