From 931052f40c4d8a1b1189890edc077fa0152443b5 Mon Sep 17 00:00:00 2001 From: Goffi Date: Tue, 6 May 2025 00:33:57 +0200 Subject: [PATCH] plugin XEP-0444, storage: move `get_history_from_xmpp_id` to storage as it is generally useful. --- libervia/backend/memory/sqla.py | 43 +++++++++++++++++++++ libervia/backend/plugins/plugin_xep_0444.py | 32 ++------------- 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/libervia/backend/memory/sqla.py b/libervia/backend/memory/sqla.py index 3a731fe7..bbcaee6e 100644 --- a/libervia/backend/memory/sqla.py +++ b/libervia/backend/memory/sqla.py @@ -494,6 +494,49 @@ class Storage: else: return History.source == jid_.userhost() + @aio + async def get_history_from_xmpp_id( + self, client: SatXMPPEntity, message_id: str, message_type: str|None + ) -> History | None: + """Retrieves history row that match a specific message ID. + + The retrieval criteria vary based on the message type. + + @param message_type: The type of the message (one of C.MESS_TYPE_*) + @param message_id: the ID of the message, XEP-0359's stanza-id is used for + groupchat, origin-id from the same XEP otherwise. + @return: History instance with messages, subjects and threads, if any found, None + otherwise. + """ + profile_id = self.profiles[client.profile] + async with self.session() as session: + if message_type == C.MESS_TYPE_GROUPCHAT: + query = select(History).where( + History.profile_id == profile_id, History.stanza_id == message_id + ) + else: + query = select(History).where( + History.profile_id == profile_id, History.origin_id == message_id + ) + + query = ( + query + .outerjoin(History.messages) + .outerjoin(History.subjects) + .outerjoin(History.thread) + .options( + contains_eager(History.messages), + contains_eager(History.subjects), + contains_eager(History.thread), + ) + ) + + result = await session.execute(query) + history = result.scalars().first() + + return history + + @aio async def history_get( self, diff --git a/libervia/backend/plugins/plugin_xep_0444.py b/libervia/backend/plugins/plugin_xep_0444.py index 511ec7e8..964dda1f 100644 --- a/libervia/backend/plugins/plugin_xep_0444.py +++ b/libervia/backend/plugins/plugin_xep_0444.py @@ -75,33 +75,6 @@ class XEP_0444: def get_handler(self, client): return XEP_0444_Handler() - @aio - async def get_history_from_reaction_id( - self, client: SatXMPPEntity, message_elt: domish.Element, reaction_id: str - ) -> History | None: - """Retrieves history rows that match a specific reaction_id. - - The retrieval criteria vary based on the message type, according to XEP-0444. - - @param message_elt: The message element, used to determine the type of message. - @param reaction_id: The reaction ID to match in the history. - """ - profile_id = self.host.memory.storage.profiles[client.profile] - async with self.host.memory.storage.session() as session: - if message_elt.getAttribute("type") == C.MESS_TYPE_GROUPCHAT: - query = select(History).where( - History.profile_id == profile_id, History.stanza_id == reaction_id - ) - else: - query = select(History).where( - History.profile_id == profile_id, History.origin_id == reaction_id - ) - - result = await session.execute(query) - history = result.scalars().first() - - return history - async def _message_received_trigger( self, client: SatXMPPEntity, @@ -111,8 +84,9 @@ class XEP_0444: reactions_elt = next(message_elt.elements(NS_REACTIONS, "reactions"), None) if reactions_elt is not None: reaction_id = reactions_elt.getAttribute("id") - history = await self.get_history_from_reaction_id( - client, message_elt, reaction_id + message_type = message_elt.getAttribute("type") + history = await self.host.memory.storage.get_history_from_xmpp_id( + client, reaction_id, message_type ) if history is None: log.warning(