changeset 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 f6672bc80897
children b93f95efe329
files libervia/backend/memory/sqla.py libervia/backend/plugins/plugin_xep_0444.py
diffstat 2 files changed, 46 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/backend/memory/sqla.py	Tue May 06 00:21:24 2025 +0200
+++ b/libervia/backend/memory/sqla.py	Tue May 06 00:33:57 2025 +0200
@@ -495,6 +495,49 @@
                 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,
         from_jid: Optional[jid.JID],
--- a/libervia/backend/plugins/plugin_xep_0444.py	Tue May 06 00:21:24 2025 +0200
+++ b/libervia/backend/plugins/plugin_xep_0444.py	Tue May 06 00:33:57 2025 +0200
@@ -75,33 +75,6 @@
     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 @@
         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(