changeset 1222:e6e0ea4dc835

memory: add Parameter "Chat history limit"
author souliane <souliane@mailoo.org>
date Wed, 24 Sep 2014 13:49:43 +0200
parents 5e5661ab5c81
children 802b7e6bf098
files frontends/src/quick_frontend/quick_chat.py src/core/constants.py src/memory/memory.py src/memory/params.py src/memory/sqlite.py
diffstat 5 files changed, 50 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/quick_frontend/quick_chat.py	Fri Oct 03 12:43:59 2014 +0200
+++ b/frontends/src/quick_frontend/quick_chat.py	Wed Sep 24 13:49:43 2014 +0200
@@ -22,7 +22,7 @@
 log = getLogger(__name__)
 from sat_frontends.tools.jid  import JID
 from sat_frontends.quick_frontend.quick_utils import unescapePrivate
-from sat_frontends.quick_frontend.constants import Const
+from sat_frontends.quick_frontend.constants import Const as C
 
 
 class QuickChat(object):
@@ -100,7 +100,7 @@
         """Refresh or scroll down the focus after the history is printed"""
         pass
 
-    def historyPrint(self, size=20, profile='@NONE@'):
+    def historyPrint(self, size=C.HISTORY_LIMIT_DEFAULT, profile='@NONE@'):
         """Print the current history
         @param size (int): number of messages
         @param profile (str): %(doc_profile)s
@@ -119,7 +119,7 @@
         def onHistoryError(err):
             log.error(_("Can't get history"))
 
-        if self.target.startswith(Const.PRIVATE_PREFIX):
+        if self.target.startswith(C.PRIVATE_PREFIX):
             target = unescapePrivate(self.target)
         else:
             target = self.target.bare
@@ -128,9 +128,9 @@
 
     def _get_nick(self, jid):
         """Return nick of this jid when possible"""
-        if self.target.startswith(Const.PRIVATE_PREFIX):
+        if self.target.startswith(C.PRIVATE_PREFIX):
             unescaped = unescapePrivate(self.target)
-            if jid.startswith(Const.PRIVATE_PREFIX) or unescaped.bare == jid.bare:
+            if jid.startswith(C.PRIVATE_PREFIX) or unescaped.bare == jid.bare:
                 return unescaped.resource
         return jid.resource if self.type == "group" else (self.host.contact_list.getCache(jid,'nick') or self.host.contact_list.getCache(jid,'name') or jid.node)
 
@@ -166,7 +166,7 @@
     def updateChatState(self, state, nick=None):
         """Set the chat state (XEP-0085) of the contact. Leave nick to None
         to set the state for a one2one conversation, or give a nickname or
-        Const.ALL_OCCUPANTS to set the state of a participant within a MUC.
+        C.ALL_OCCUPANTS to set the state of a participant within a MUC.
         @param state: the new chat state
         @param nick: None for one2one, the MUC user nick or ALL_OCCUPANTS
         """
--- a/src/core/constants.py	Fri Oct 03 12:43:59 2014 +0200
+++ b/src/core/constants.py	Wed Sep 24 13:49:43 2014 +0200
@@ -44,6 +44,8 @@
     NO_SECURITY_LIMIT = -1
     INDIVIDUAL = "individual"
     GENERAL = "general"
+    # General parameters
+    HISTORY_LIMIT = "History"
     # Parameters related to connection
     FORCE_SERVER_PARAM = "Force server"
     FORCE_PORT_PARAM = "Force port"
@@ -169,6 +171,11 @@
     BOOL_FALSE = "false"
 
 
+    ## Special values used in bridge methods calls ##
+    HISTORY_LIMIT_DEFAULT = -1
+    HISTORY_LIMIT_NONE = -2
+
+
     ## Misc ##
     SAVEFILE_DATABASE = APP_NAME_FILE + ".db"
     IQ_SET = '/iq[@type="set"]'
--- a/src/memory/memory.py	Fri Oct 03 12:43:59 2014 +0200
+++ b/src/memory/memory.py	Wed Sep 24 13:49:43 2014 +0200
@@ -350,8 +350,25 @@
             extra = {}
         return self.storage.addToHistory(from_jid, to_jid, message, type_, extra, timestamp, profile)
 
-    def getHistory(self, from_jid, to_jid, limit=None, between=True, profile=C.PROF_KEY_NONE):
+    def getHistory(self, from_jid, to_jid, limit=C.HISTORY_LIMIT_NONE, between=True, 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)
+        @param limit (int): maximum number of messages to get:
+            - 0 for no message (returns the empty list)
+            - 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 profile (str): %(doc_profile)s
+        @return: list of tuple as in http://wiki.goffi.org/wiki/Bridge_API#getHistory
+        """
         assert profile != C.PROF_KEY_NONE
+        if limit == C.HISTORY_LIMIT_DEFAULT:
+            limit = int(self.getParamA(C.HISTORY_LIMIT, 'General', profile_key=profile))
+        elif limit == C.HISTORY_LIMIT_NONE:
+            limit = None
+        if limit == 0:
+            return defer.succeed([])
         return self.storage.getHistory(jid.JID(from_jid), jid.JID(to_jid), limit, between, profile)
 
     def _getLastResource(self, jid_s, profile_key):
--- a/src/memory/params.py	Fri Oct 03 12:43:59 2014 +0200
+++ b/src/memory/params.py	Wed Sep 24 13:49:43 2014 +0200
@@ -17,7 +17,7 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from sat.core.i18n import _
+from sat.core.i18n import _, D_
 
 from sat.core import exceptions
 from sat.core.constants import Const as C
@@ -44,6 +44,7 @@
     <individual>
         <category name="General" label="%(category_general)s">
             <param name="Password" value="" type="password" />
+            <param name="%(history_param)s" label="%(label_history)s" value="20" min="0" max="100" type="int" security="0" />
         </category>
         <category name="Connection" label="%(category_connection)s">
             <param name="JabberID" value="name@example.org/SàT" type="string" />
@@ -61,12 +62,14 @@
     </individual>
     </params>
     """ % {
-        'category_general': _("General"),
-        'category_connection': _("Connection"),
-        'label_NewAccount': _("Register new account"),
-        'label_autoconnect': _('Connect on frontend startup'),
-        'label_autodisconnect': _('Disconnect on frontend closure'),
-        'category_misc': _("Misc"),
+        'category_general': D_("General"),
+        'category_connection': D_("Connection"),
+        'history_param': C.HISTORY_LIMIT,
+        'label_history': D_('Chat history limit'),
+        'label_NewAccount': D_("Register new account"),
+        'label_autoconnect': D_('Connect on frontend startup'),
+        'label_autodisconnect': D_('Disconnect on frontend closure'),
+        'category_misc': D_("Misc"),
         'force_server_param': C.FORCE_SERVER_PARAM,
         'force_port_param': C.FORCE_PORT_PARAM,
     }
--- a/src/memory/sqlite.py	Fri Oct 03 12:43:59 2014 +0200
+++ b/src/memory/sqlite.py	Wed Sep 24 13:49:43 2014 +0200
@@ -265,10 +265,15 @@
         return d
 
     def getHistory(self, from_jid, to_jid, limit=None, between=True, profile=None):
-        """Store a new message in history
-        @param from_jid: source JID (full, or bare for catchall
-        @param to_jid: dest JID (full, or bare for catchall
-        @param size: maximum number of messages to get, or None for unlimited
+        """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)
+        @param limit (int): maximum number of messages to get:
+            - 0 for no message (returns the empty list)
+            - None for unlimited
+        @param between (bool): confound source and dest (ignore the direction)
+        @param profile (str): %(doc_profile)s
+        @return: list of tuple as in http://wiki.goffi.org/wiki/Bridge_API#getHistory
         """
         assert(profile)
         if limit == 0: