diff libervia/backend/memory/sqla.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 0d7bb4df2343
children b93f95efe329
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],