changeset 1224:f0c9b149ed99

bridge, memory: add "search" attribute to getHistory (filter the results with a unix globbing pattern)
author souliane <souliane@mailoo.org>
date Sat, 04 Oct 2014 10:25:32 +0200
parents 802b7e6bf098
children 82dabb442e2e
files frontends/src/bridge/DBus.py frontends/src/quick_frontend/quick_chat.py src/bridge/DBus.py src/bridge/bridge_constructor/bridge_template.ini src/memory/memory.py src/memory/sqlite.py
diffstat 6 files changed, 22 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/bridge/DBus.py	Sat Oct 04 10:23:13 2014 +0200
+++ b/frontends/src/bridge/DBus.py	Sat Oct 04 10:25:32 2014 +0200
@@ -164,8 +164,8 @@
     def getEntityData(self, jid, keys, profile):
         return self.db_core_iface.getEntityData(jid, keys, profile)
 
-    def getHistory(self, from_jid, to_jid, limit, between=True, profile="@NONE@", callback=None, errback=None):
-        return self.db_core_iface.getHistory(from_jid, to_jid, limit, between, profile, timeout=const_TIMEOUT, reply_handler=callback, error_handler=lambda err:errback(dbus_to_bridge_exception(err)))
+    def getHistory(self, from_jid, to_jid, limit, between=True, search='', profile="@NONE@", callback=None, errback=None):
+        return self.db_core_iface.getHistory(from_jid, to_jid, limit, between, search, profile, timeout=const_TIMEOUT, reply_handler=callback, error_handler=lambda err:errback(dbus_to_bridge_exception(err)))
 
     def getLastResource(self, contact_jid, profile_key="@DEFAULT@"):
         return unicode(self.db_core_iface.getLastResource(contact_jid, profile_key))
--- a/frontends/src/quick_frontend/quick_chat.py	Sat Oct 04 10:23:13 2014 +0200
+++ b/frontends/src/quick_frontend/quick_chat.py	Sat Oct 04 10:25:32 2014 +0200
@@ -100,9 +100,10 @@
         """Refresh or scroll down the focus after the history is printed"""
         pass
 
-    def historyPrint(self, size=C.HISTORY_LIMIT_DEFAULT, profile='@NONE@'):
+    def historyPrint(self, size=C.HISTORY_LIMIT_DEFAULT, search='', profile='@NONE@'):
         """Print the current history
         @param size (int): number of messages
+        @param search (str): pattern to filter the history results
         @param profile (str): %(doc_profile)s
         """
         log.debug(_("now we print the history (%d messages)") % size)
@@ -124,7 +125,7 @@
         else:
             target = self.target.bare
 
-        return self.host.bridge.getHistory(self.host.profiles[profile]['whoami'].bare, target, size, profile=profile, callback=onHistory, errback=onHistoryError)
+        return self.host.bridge.getHistory(self.host.profiles[profile]['whoami'].bare, target, size, search=search, profile=profile, callback=onHistory, errback=onHistoryError)
 
     def _get_nick(self, jid):
         """Return nick of this jid when possible"""
--- a/src/bridge/DBus.py	Sat Oct 04 10:23:13 2014 +0200
+++ b/src/bridge/DBus.py	Sat Oct 04 10:25:32 2014 +0200
@@ -277,10 +277,10 @@
         return self._callback("getEntityData", unicode(jid), keys, unicode(profile))
 
     @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
-                         in_signature='ssibs', out_signature='a(dssssa{ss})',
+                         in_signature='ssibss', out_signature='a(dssssa{ss})',
                          async_callbacks=('callback', 'errback'))
-    def getHistory(self, from_jid, to_jid, limit, between=True, profile="@NONE@", callback=None, errback=None):
-        return self._callback("getHistory", unicode(from_jid), unicode(to_jid), limit, between, unicode(profile), callback=callback, errback=errback)
+    def getHistory(self, from_jid, to_jid, limit, between=True, search='', profile="@NONE@", callback=None, errback=None):
+        return self._callback("getHistory", unicode(from_jid), unicode(to_jid), limit, between, unicode(search), unicode(profile), callback=callback, errback=errback)
 
     @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
                          in_signature='ss', out_signature='s',
--- a/src/bridge/bridge_constructor/bridge_template.ini	Sat Oct 04 10:23:13 2014 +0200
+++ b/src/bridge/bridge_constructor/bridge_template.ini	Sat Oct 04 10:25:32 2014 +0200
@@ -483,16 +483,18 @@
 async=
 type=method
 category=core
-sig_in=ssibs
+sig_in=ssibss
 sig_out=a(dssssa{ss})
 param_3_default=True
-param_4_default="@NONE@"
+param_4_default=''
+param_5_default="@NONE@"
 doc=Get history of a communication between two entities
 doc_param_0=from_jid: source JID (bare jid for catch all, full jid else)
 doc_param_1=to_jid: dest JID (bare jid for catch all, full jid else)
 doc_param_2=limit: max number of history elements to get (0 for the whole history)
 doc_param_3=between: True if we want history between the two jids (in both direction), False if we only want messages from from_jid to to_jid
-doc_param_4=%(doc_profile)s
+doc_param_4=search: pattern to filter the history results
+doc_param_5=%(doc_profile)s
 doc_return=Ordered list (by timestamp) of tuples (timestamp, full from_jid, full to_jid, message, type, extra)
 
 [addContact]
--- a/src/memory/memory.py	Sat Oct 04 10:23:13 2014 +0200
+++ b/src/memory/memory.py	Sat Oct 04 10:25:32 2014 +0200
@@ -350,7 +350,7 @@
             extra = {}
         return self.storage.addToHistory(from_jid, to_jid, message, type_, extra, timestamp, profile)
 
-    def getHistory(self, from_jid, to_jid, limit=C.HISTORY_LIMIT_NONE, between=True, profile=C.PROF_KEY_NONE):
+    def getHistory(self, from_jid, to_jid, limit=C.HISTORY_LIMIT_NONE, between=True, search=None, profile=C.PROF_KEY_NONE):
         """Retrieve messages in history
         @param from_jid (JID): source JID (full, or bare for catchall)
         @param to_jid (JID): dest JID (full, or bare for catchall)
@@ -359,6 +359,7 @@
             - C.HISTORY_LIMIT_NONE or None for unlimited
             - C.HISTORY_LIMIT_DEFAULT to use the HISTORY_LIMIT parameter value
         @param between (bool): confound source and dest (ignore the direction)
+        @param search (str): pattern to filter the history results
         @param profile (str): %(doc_profile)s
         @return: list of tuple as in http://wiki.goffi.org/wiki/Bridge_API#getHistory
         """
@@ -369,7 +370,7 @@
             limit = None
         if limit == 0:
             return defer.succeed([])
-        return self.storage.getHistory(jid.JID(from_jid), jid.JID(to_jid), limit, between, profile)
+        return self.storage.getHistory(jid.JID(from_jid), jid.JID(to_jid), limit, between, search, profile)
 
     def _getLastResource(self, jid_s, profile_key):
         jid_ = jid.JID(jid_s)
--- a/src/memory/sqlite.py	Sat Oct 04 10:23:13 2014 +0200
+++ b/src/memory/sqlite.py	Sat Oct 04 10:25:32 2014 +0200
@@ -264,7 +264,7 @@
                                           {"from_jid": from_jid.full(), "to_jid": to_jid.full(), "message": message})))
         return d
 
-    def getHistory(self, from_jid, to_jid, limit=None, between=True, profile=None):
+    def getHistory(self, from_jid, to_jid, limit=None, between=True, search=None, profile=None):
         """Retrieve messages in history
         @param from_jid (JID): source JID (full, or bare for catchall)
         @param to_jid (JID): dest JID (full, or bare for catchall)
@@ -272,6 +272,7 @@
             - 0 for no message (returns the empty list)
             - None for unlimited
         @param between (bool): confound source and dest (ignore the direction)
+        @param search (str): pattern to filter the history results
         @param profile (str): %(doc_profile)s
         @return: list of tuple as in http://wiki.goffi.org/wiki/Bridge_API#getHistory
         """
@@ -311,6 +312,10 @@
         else:
             query_parts.append("%s AND %s" % (test_jid('source', from_jid),
                                               test_jid('dest', to_jid)))
+        if search:
+            # TODO: use REGEXP (function to be defined) instead of GLOB: https://www.sqlite.org/lang_expr.html
+            query_parts.append("AND message GLOB ?")
+            values.append("*%s*" % search)
 
         query_parts.append("ORDER BY timestamp DESC")