diff src/browser/libervia_main.py @ 586:3eb3a2c0c011

browser and server side: uses RSM (XEP-0059)
author souliane <souliane@mailoo.org>
date Fri, 28 Nov 2014 00:31:27 +0100
parents 0a06cf833f5a
children a90cc8fc9605
line wrap: on
line diff
--- a/src/browser/libervia_main.py	Thu Oct 23 16:56:36 2014 +0200
+++ b/src/browser/libervia_main.py	Fri Nov 28 00:31:27 2014 +0100
@@ -132,7 +132,7 @@
     def __init__(self):
         LiberviaJsonProxy.__init__(self, "/json_api",
                         ["getContacts", "addContact", "sendMessage", "sendMblog", "sendMblogComment",
-                         "getLastMblogs", "getMassiveLastMblogs", "getMblogComments", "getProfileJid",
+                         "getMblogs", "getMassiveMblogs", "getMblogComments", "getProfileJid",
                          "getHistory", "getPresenceStatuses", "joinMUC", "mucLeave", "getRoomsJoined",
                          "inviteMUC", "launchTarotGame", "getTarotCardsPaths", "tarotGameReady",
                          "tarotGamePlayCards", "launchRadioCollective", "getMblogs", "getMblogsWithComments",
@@ -203,6 +203,7 @@
         self.initialised = False
         self.init_cache = []  # used to cache events until initialisation is done
         self.cached_params = {}
+        self.next_rsm_index = 0
 
     def importPlugins(self):
         self.plugins = {}
@@ -541,10 +542,15 @@
         if not xml_ui:
             self.panel.menu.removeItemParams()
 
-    def _ownBlogsFills(self, mblogs):
-        #put our own microblogs in cache, then fill all panels with them
+    def _ownBlogsFills(self, mblogs, mblog_panel=None):
+        """Put our own microblogs in cache, then fill the panels with them.
+
+        @param mblogs (dict): dictionary mapping a publisher JID to blogs data.
+        @param mblog_panel (MicroblogPanel): the panel to fill, or all if None.
+        """
+        cache = []
         for publisher in mblogs:
-            for mblog in mblogs[publisher]:
+            for mblog in mblogs[publisher][0]:
                 if 'content' not in mblog:
                     log.warning("No content found in microblog [%s]" % mblog)
                     continue
@@ -553,13 +559,19 @@
                 else:
                     _groups = None
                 mblog_entry = panels.MicroblogItem(mblog)
-                self.mblog_cache.append((_groups, mblog_entry))
+                cache.append((_groups, mblog_entry))
 
+        self.mblog_cache.extend(cache)
         if len(self.mblog_cache) > MAX_MBLOG_CACHE:
             del self.mblog_cache[0:len(self.mblog_cache - MAX_MBLOG_CACHE)]
-        for lib_wid in self.libervia_widgets:
+
+        widget_list = [mblog_panel] if mblog_panel else self.libervia_widgets
+        for lib_wid in widget_list:
             if isinstance(lib_wid, panels.MicroblogPanel):
-                self.FillMicroblogPanel(lib_wid)
+                self.fillMicroblogPanel(lib_wid, cache)
+
+        if self.initialised:
+            return
         self.initialised = True  # initialisation phase is finished here
         for event_data in self.init_cache:  # so we have to send all the cached events
             self._personalEventCb(*event_data)
@@ -577,18 +589,38 @@
         for lib_wid in self.libervia_widgets:
             if isinstance(lib_wid, panels.MicroblogPanel):
                 if lib_wid.accept_all():
-                    self.bridge.call('getMassiveLastMblogs', lib_wid.massiveInsert, 'ALL', [], 10)
+                    self.bridge.call('getMassiveMblogs', lib_wid.massiveInsert, 'ALL', [])
                 else:
-                    self.bridge.call('getMassiveLastMblogs', lib_wid.massiveInsert, 'GROUP', lib_wid.accepted_groups, 10)
+                    self.bridge.call('getMassiveMblogs', lib_wid.massiveInsert, 'GROUP', lib_wid.accepted_groups)
 
         #we ask for our own microblogs:
-        self.bridge.call('getMassiveLastMblogs', self._ownBlogsFills, 'JID', [self.whoami.bare], 10)
+        self.loadOurMainEntries()
 
         # initialize plugins which waited for the connection to be done
         for plugin in self.plugins.values():
             if hasattr(plugin, 'profileConnected'):
                 plugin.profileConnected()
 
+    def loadOurMainEntries(self, index=0, mblog_panel=None):
+        """Load a page of our own blogs from the cache or ask them to the
+        backend. Then fill the panels with them.
+
+        @param index (int): starting index of the blog page to retrieve.
+        @param mblog_panel (MicroblogPanel): the panel to fill, or all if None.
+        """
+        delta = index - self.next_rsm_index
+        if delta < 0:
+            assert(mblog_panel is not None)
+            self.fillMicroblogPanel(mblog_panel, self.mblog_cache[index:index + C.RSM_MAX_ITEMS])
+            return
+
+        def cb(result):
+            self._ownBlogsFills(result, mblog_panel)
+
+        rsm = {'max': str(delta + C.RSM_MAX_ITEMS), 'index': str(self.next_rsm_index)}
+        self.bridge.call('getMassiveMblogs', cb, 'JID', [self.whoami.bare], rsm)
+        self.next_rsm_index = index + C.RSM_MAX_ITEMS
+
     ## Signals callbacks ##
 
     def _personalEventCb(self, sender, event_type, data):
@@ -647,12 +679,12 @@
            or (_groups and _groups.intersection(mblog_panel.accepted_groups)):
             mblog_panel.addEntry(mblog_entry)
 
-    def FillMicroblogPanel(self, mblog_panel):
+    def fillMicroblogPanel(self, mblog_panel, mblogs):
         """Fill a microblog panel with entries in cache
         @param mblog_panel: MicroblogPanel instance
         """
         #XXX: only our own entries are cached
-        for cache_entry in self.mblog_cache:
+        for cache_entry in mblogs:
             _groups, mblog_entry = cache_entry
             self.addBlogEntry(mblog_panel, self.whoami.bare, *cache_entry)
 
@@ -661,7 +693,7 @@
         for lib_wid in self.libervia_widgets:
             if isinstance(lib_wid, panels.MicroblogPanel):
                 if lib_wid.isJidAccepted(entity):
-                    self.bridge.call('getMassiveLastMblogs', lib_wid.massiveInsert, 'JID', [entity], 10)
+                    self.bridge.call('getMassiveMblogs', lib_wid.massiveInsert, 'JID', [entity])
 
     def getLiberviaWidget(self, class_, entity, ignoreOtherTabs=True):
         """Get the corresponding panel if it exists.