diff libervia.py @ 58:4fa3d57f72f8

browser side: microblog entries caching
author Goffi <goffi@goffi.org>
date Sun, 29 May 2011 17:57:02 +0200
parents e552a67b933d
children 12e889a683ce
line wrap: on
line diff
--- a/libervia.py	Sun May 29 16:07:46 2011 +0200
+++ b/libervia.py	Sun May 29 17:57:02 2011 +0200
@@ -31,6 +31,8 @@
 from browser_side.jid import JID
 from browser_side.tools import html_sanitize
 
+MAX_MBLOG_CACHE = 500 #Max microblog entries kept in memories
+
 class LiberviaJsonProxy(JSONProxy):
     def __init__(self, *args, **kwargs):
         JSONProxy.__init__(self, *args, **kwargs)
@@ -75,6 +77,14 @@
         LiberviaJsonProxy.__init__(self, "/json_signal_api",
                         ["getSignals"])
 
+class MicroblogEntry():
+    def __init__(self, data):
+        self.id = data['id']
+        self.content = data['content']
+        self.author = data['author']
+        self.timestamp = float(data.get('timestamp',0)) #XXX: int doesn't work here
+        
+
 class SatWebFrontend:
     def onModuleLoad(self):
         self.whoami = None
@@ -90,6 +100,7 @@
         self.mpanels = [panels.EmptyPanel(self), panels.MicroblogPanel(self, accept_all=True), panels.EmptyPanel(self)]
         self.other_panels = [] #panels not on the main tab #FIXME: temporary, need to be changed
         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.discuss_panel.changePanel(0,self.mpanels[0])
         self.discuss_panel.changePanel(1,self.mpanels[1])
         self.discuss_panel.changePanel(2,self.mpanels[2])
@@ -207,7 +218,7 @@
 
     ## Signals callbacks ##
 
-    def _personalEventCb(self, sender, event_type, data, profile):
+    def _personalEventCb(self, sender, event_type, data):
         if event_type == "MICROBLOG":
             if not data.has_key('content'):
                 print ("WARNING: No content found in microblog data")
@@ -216,12 +227,31 @@
                 _groups = set(data['groups'].split() if data['groups'] else [])
             else:
                 _groups=None
+            mblog_entry = MicroblogEntry(data)
+            
             for panel in self.mpanels:
-                if isinstance(panel,panels.MicroblogPanel) and (panel.isJidAccepted(sender) or _groups == None or _groups.intersection(panel.accepted_groups)): #TODO: check this
-                    content = data['content']
-                    author = data.get('author')
-                    timestamp = float(data.get('timestamp',0)) #XXX: int doesn't work here
-                    panel.addEntry(content, author, timestamp)
+                if isinstance(panel, panels.MicroblogPanel):
+                    self.addBlogEntry(panel, 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)]
+
+    def addBlogEntry(self, mblog_panel, sender, _groups, mblog_entry):
+        """Check if an entry can go in MicroblogPanel and add to it
+        @param mblog_panel: MicroblogPanel instance
+        @param sender: jid of the entry sender
+        @param _groups: groups which can receive this entry
+        @param mblog_entry: MicroblogEntry instance"""
+        if mblog_panel.isJidAccepted(sender) or _groups == None or _groups.intersection(mblog_panel.accepted_groups):
+            mblog_panel.addEntry(mblog_entry)
+
+    def FillMicroblogPanel(self, mblog_entry):
+        """Fill a microblog panel with entries in cache
+        @param mblog_panel: MicroblogPanel instance
+        """
+        for cache_entry in self.mblog_cache:
+            self.addBlogEntry(mblog_entry, *cache_entry)
 
     def _newMessageCb(self, from_jid, msg, msg_type, to_jid):
         _from = JID(from_jid)