changeset 58:4fa3d57f72f8

browser side: microblog entries caching
author Goffi <goffi@goffi.org>
date Sun, 29 May 2011 17:57:02 +0200
parents e552a67b933d
children d0fa4e96a5e4
files browser_side/panels.py libervia.py
diffstat 2 files changed, 50 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/browser_side/panels.py	Sun May 29 16:07:46 2011 +0200
+++ b/browser_side/panels.py	Sun May 29 17:57:02 2011 +0200
@@ -112,7 +112,7 @@
 Project page: <a href="http://wiki.goffi.org/wiki/Salut_à_Toi"target="_blank">http://wiki.goffi.org/wiki/Salut_à_Toi</a><br />
 <br />
 Any help welcome :)
-<p style='font-size:x-small;text-align:center'>This project is dedicated to Roger Poisson</p>
+<p style='font-size:small;text-align:center'>This project is dedicated to Roger Poisson</p>
 """)
         _dialog = dialog.GenericDialog("About", _about)
         _dialog.show()
@@ -276,12 +276,14 @@
         if item_type=="GROUP":
             _new_panel = MicroblogPanel(self.host, item)
             _new_panel.setAcceptedGroup(item)
+            self.host.FillMicroblogPanel(_new_panel)
         elif item_type=="CONTACT":
             _contact = JID(item)
             _new_panel = ChatPanel(self.host, _contact)
             _new_panel.historyPrint()
         elif item_type=="CONTACT_TITLE":
             _new_panel = MicroblogPanel(self.host, accept_all=True)
+            self.host.FillMicroblogPanel(_new_panel)
         else:
             return
         self.host.mpanels.remove(self)
@@ -465,15 +467,15 @@
 
 class MicroblogEntry(SimplePanel):
 
-    def __init__(self, body, author, timestamp):
+    def __init__(self, mblog_entry):
         SimplePanel.__init__(self)
 
-        _datetime = datetime.fromtimestamp(timestamp)
+        _datetime = datetime.fromtimestamp(mblog_entry.timestamp)
 
         panel = HTMLPanel("<div class='mb_entry_header'><span class='mb_entry_author'>%(author)s</span> on <span class='mb_entry_timestamp'>%(timestamp)s</span></div><div class='mb_entry_body'>%(body)s</div>" %
-            {"author": html_sanitize(author),
+            {"author": html_sanitize(mblog_entry.author),
             "timestamp": _datetime,
-            "body": html_sanitize(body)}
+            "body": html_sanitize(mblog_entry.content)}
             )
         panel.setStyleName('microblogEntry')
         self.add(panel)
@@ -490,6 +492,7 @@
         self.accept_all = accept_all
         title=html_sanitize(title)
         self.accepted_groups = []
+        self.entries = {}
         _class = ['mb_panel_header']
         if title == '&nbsp;':
             _class.append('empty_header')
@@ -499,12 +502,16 @@
         self.setStyleName('microblogPanel')
         self.setWidget(self.vpanel)
 
-    def addEntry(self, text, author=None, timestamp=None):
+    def addEntry(self, mblog_entry):
         """Add an entry to the panel
         @param text: main text of the entry
+        @param id: unique id of the entry
         @param author: who wrote the entry
         @param date: when the entry was written"""
-        _entry = MicroblogEntry(text, author, timestamp)
+        if mblog_entry.id in self.entries:
+            return
+        _entry = MicroblogEntry(mblog_entry)
+        self.entries[mblog_entry.id] = _entry
         self.vpanel.insert(_entry,1)
 
     def setAcceptedGroup(self, group):
--- 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)