Philipp Hörist pushed to branch master at gajim / gajim


Commits:
fa3ba1a2 by Philipp Hörist at 2024-09-10T17:09:09+02:00
imprv: Control: Improve query to load messages around a timestamp

- - - - -


2 changed files:

- gajim/common/storage/archive/storage.py
- gajim/gtk/control.py


Changes:

=====================================
gajim/common/storage/archive/storage.py
=====================================
@@ -23,6 +23,7 @@
 from nbxmpp import JID
 from sqlalchemy import delete
 from sqlalchemy import select
+from sqlalchemy import union_all
 from sqlalchemy import update
 from sqlalchemy.dialects.sqlite import insert
 from sqlalchemy.engine import Engine
@@ -556,6 +557,53 @@ def get_conversation_before_after(
         self._explain(session, stmt)
         return session.scalars(stmt).all()
 
+    @with_session
+    @timeit
+    def get_conversation_around_timestamp(
+        self,
+        session: Session,
+        account: str,
+        jid: JID,
+        timestamp: datetime
+    ) -> list[Message]:
+        '''
+        Loads messages around a primary key
+
+        :param account:
+            The account
+        :param jid:
+            The jid for which we request the conversation
+        :param timestamp:
+            The timestamp in the conversation
+        '''
+
+        fk_account_pk = self._get_account_pk(session, account)
+        fk_remote_pk = self._get_jid_pk(session, jid)
+
+        base_stmt = select(Message.pk).where(
+            Message.fk_remote_pk == fk_remote_pk,
+            Message.fk_account_pk == fk_account_pk,
+            Message.correction_id.is_(None),
+        )
+
+        preceding_query = base_stmt.where(
+            Message.timestamp <= timestamp,
+        ).order_by(
+            sa.desc(Message.timestamp), sa.desc(Message.pk)
+        ).limit(50).subquery()
+
+        following_query = base_stmt.where(
+            Message.timestamp > timestamp,
+        ).order_by(
+            sa.asc(Message.timestamp), sa.asc(Message.pk)
+        ).limit(50).subquery()
+
+        u_stmt = union_all(select(preceding_query), select(following_query))
+        stmt = select(Message).where(Message.pk.in_(u_stmt.scalar_subquery()))
+
+        self._explain(session, stmt)
+        return list(session.scalars(stmt).all())
+
     @with_session
     @timeit
     def get_last_conversation_row(


=====================================
gajim/gtk/control.py
=====================================
@@ -192,20 +192,12 @@ def scroll_to_message(
     ) -> None:
         row = self._scrolled_view.get_row_by_pk(pk)
         if row is None:
-            m = app.storage.archive.get_message_with_pk(pk)
-            if m is None:
-                log.info('Message with pk %s was not found in DB', pk)
-                return
-
             # Clear view and reload conversation around timestamp
             self._scrolled_view.reset()
             self._scrolled_view.block_signals(True)
-            messages: list[Message] = []
-            messages.append(m)
-            messages.extend(app.storage.archive.get_conversation_before_after(
-                self.contact.account, self.contact.jid, True, timestamp, 50))
-            messages.extend(app.storage.archive.get_conversation_before_after(
-                self.contact.account, self.contact.jid, False, timestamp, 50))
+            messages = app.storage.archive.get_conversation_around_timestamp(
+                self.contact.account, self.contact.jid, timestamp
+            )
             self._add_messages(messages)
             self._scrolled_view.set_history_complete(False, False)
 



View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/fa3ba1a2e92b44e703c978df0c4a8bdf3b5574de

-- 
View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/fa3ba1a2e92b44e703c978df0c4a8bdf3b5574de
You're receiving this email because of your account on dev.gajim.org.


_______________________________________________
Commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to