diff libervia.py @ 139:b6658f3ac8a0

browser side: own microblogs print - our microblogs are gotten on startup and put in cache - FillMicroblogPanel add our own microblogs from cache in a new panel
author Goffi <goffi@goffi.org>
date Tue, 30 Oct 2012 00:52:30 +0100
parents 008fa8d36602
children a5e9aa1f9c0c
line wrap: on
line diff
--- a/libervia.py	Sun Oct 28 18:24:18 2012 +0100
+++ b/libervia.py	Tue Oct 30 00:52:30 2012 +0100
@@ -117,7 +117,7 @@
         self.tab_panel = self.panel.tab_panel
         self.libervia_widgets = set() #keep track of all actives LiberviaWidgets
         self.room_list = set() #set of rooms 
-        self.mblog_cache = [] #used to keep blog entries in memory, to show them in new mblog panel
+        self.mblog_cache = [] #used to keep our own blog entries in memory, to show them in new mblog panel
         self.avatars_cache = {} #keep track of jid's avatar hash (key=jid, value=file)
         #self.discuss_panel.addWidget(panels.EmptyPanel(self))
         self.discuss_panel.addWidget(panels.MicroblogPanel(self, []))
@@ -261,6 +261,26 @@
         elif name == 'entityDataUpdated':
             self._entityDataUpdatedCb(*args)
 
+    def _ownBlogsFills(self, mblogs):
+        #put our own microblogs in cache, then fill all panels with them
+        for publisher in mblogs:
+            for mblog in mblogs[publisher]:
+                if not mblog.has_key('content'):
+                    print ("WARNING: No content found in microblog [%s]", mblog)
+                    continue
+                if mblog.has_key('groups'):
+                    _groups = set(mblog['groups'].split() if mblog['groups'] else [])
+                else:
+                    _groups=None
+                mblog_entry = MicroblogItem(mblog)
+                self.mblog_cache.append((_groups, mblog_entry))
+        
+        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:
+            if isinstance(lib_wid, panels.MicroblogPanel):
+                self.FillMicroblogPanel(lib_wid)
+
     def _getProfileJidCB(self, jid):
         self.whoami = JID(jid)
         #we can now ask our status
@@ -276,8 +296,9 @@
                     self.bridge.call('getMassiveLastMblogs', lib_wid.massiveInsert, 'ALL', [], 10)
                 else:
                     self.bridge.call('getMassiveLastMblogs', lib_wid.massiveInsert, 'GROUP', lib_wid.accepted_groups, 10)
-                #FIXME: we currently get all post and filter after for each widget, need to be optimised
-                self.bridge.call('getMassiveLastMblogs', lib_wid.massiveInsert, 'JID', [jid], 10)
+        
+        #we ask for our own microblogs:
+        self.bridge.call('getMassiveLastMblogs', self._ownBlogsFills, 'JID', [self.whoami.bare], 10)
 
     ## Signals callbacks ##
 
@@ -296,9 +317,10 @@
                 if isinstance(lib_wid, panels.MicroblogPanel):
                     self.addBlogEntry(lib_wid, sender, _groups, mblog_entry) 
 
-            self.mblog_cache.append((sender, _groups, mblog_entry))
-            if len(self.mblog_cache) > MAX_MBLOG_CACHE:
-                del self.mblog_cache[0:len(self.mblog_cache-MAX_MBLOG_CACHE)]
+            if sender == self.whoami.bare:
+                self.mblog_cache.append((_groups, mblog_entry))
+                if len(self.mblog_cache) > MAX_MBLOG_CACHE:
+                    del self.mblog_cache[0:len(self.mblog_cache-MAX_MBLOG_CACHE)]
 
     def addBlogEntry(self, mblog_panel, sender, _groups, mblog_entry):
         """Check if an entry can go in MicroblogPanel and add to it
@@ -310,12 +332,14 @@
            or (_groups and _groups.intersection(mblog_panel.accepted_groups)):
             mblog_panel.addEntry(mblog_entry)
 
-    def FillMicroblogPanel(self, mblog_entry):
+    def FillMicroblogPanel(self, mblog_panel):
         """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:
-            self.addBlogEntry(mblog_entry, *cache_entry)
+            _groups, mblog_entry = cache_entry 
+            self.addBlogEntry(mblog_panel, self.whoami.bare, *cache_entry)
 
     def _newMessageCb(self, from_jid, msg, msg_type, to_jid):
         _from = JID(from_jid)