Mercurial > libervia-web
comparison 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 |
comparison
equal
deleted
inserted
replaced
57:e552a67b933d | 58:4fa3d57f72f8 |
---|---|
29 from browser_side.contact import ContactPanel | 29 from browser_side.contact import ContactPanel |
30 from browser_side import panels, dialog | 30 from browser_side import panels, dialog |
31 from browser_side.jid import JID | 31 from browser_side.jid import JID |
32 from browser_side.tools import html_sanitize | 32 from browser_side.tools import html_sanitize |
33 | 33 |
34 MAX_MBLOG_CACHE = 500 #Max microblog entries kept in memories | |
35 | |
34 class LiberviaJsonProxy(JSONProxy): | 36 class LiberviaJsonProxy(JSONProxy): |
35 def __init__(self, *args, **kwargs): | 37 def __init__(self, *args, **kwargs): |
36 JSONProxy.__init__(self, *args, **kwargs) | 38 JSONProxy.__init__(self, *args, **kwargs) |
37 self.handler=self | 39 self.handler=self |
38 self.cb={} | 40 self.cb={} |
72 | 74 |
73 class BridgeSignals(LiberviaJsonProxy): | 75 class BridgeSignals(LiberviaJsonProxy): |
74 def __init__(self): | 76 def __init__(self): |
75 LiberviaJsonProxy.__init__(self, "/json_signal_api", | 77 LiberviaJsonProxy.__init__(self, "/json_signal_api", |
76 ["getSignals"]) | 78 ["getSignals"]) |
79 | |
80 class MicroblogEntry(): | |
81 def __init__(self, data): | |
82 self.id = data['id'] | |
83 self.content = data['content'] | |
84 self.author = data['author'] | |
85 self.timestamp = float(data.get('timestamp',0)) #XXX: int doesn't work here | |
86 | |
77 | 87 |
78 class SatWebFrontend: | 88 class SatWebFrontend: |
79 def onModuleLoad(self): | 89 def onModuleLoad(self): |
80 self.whoami = None | 90 self.whoami = None |
81 self.bridge = BridgeCall() | 91 self.bridge = BridgeCall() |
88 self.discuss_panel = self.panel.discuss_panel | 98 self.discuss_panel = self.panel.discuss_panel |
89 self.tab_panel = self.panel.tab_panel | 99 self.tab_panel = self.panel.tab_panel |
90 self.mpanels = [panels.EmptyPanel(self), panels.MicroblogPanel(self, accept_all=True), panels.EmptyPanel(self)] | 100 self.mpanels = [panels.EmptyPanel(self), panels.MicroblogPanel(self, accept_all=True), panels.EmptyPanel(self)] |
91 self.other_panels = [] #panels not on the main tab #FIXME: temporary, need to be changed | 101 self.other_panels = [] #panels not on the main tab #FIXME: temporary, need to be changed |
92 self.room_list = set() #set of rooms | 102 self.room_list = set() #set of rooms |
103 self.mblog_cache = [] #used to keep blog entries in memory, to show them in new mblog panel | |
93 self.discuss_panel.changePanel(0,self.mpanels[0]) | 104 self.discuss_panel.changePanel(0,self.mpanels[0]) |
94 self.discuss_panel.changePanel(1,self.mpanels[1]) | 105 self.discuss_panel.changePanel(1,self.mpanels[1]) |
95 self.discuss_panel.changePanel(2,self.mpanels[2]) | 106 self.discuss_panel.changePanel(2,self.mpanels[2]) |
96 self._dialog = None | 107 self._dialog = None |
97 RootPanel().add(self.panel) | 108 RootPanel().add(self.panel) |
205 #and if there is any subscription request waiting for us | 216 #and if there is any subscription request waiting for us |
206 self.bridge.call('getWaitingSub', self._getWaitingSubCb) | 217 self.bridge.call('getWaitingSub', self._getWaitingSubCb) |
207 | 218 |
208 ## Signals callbacks ## | 219 ## Signals callbacks ## |
209 | 220 |
210 def _personalEventCb(self, sender, event_type, data, profile): | 221 def _personalEventCb(self, sender, event_type, data): |
211 if event_type == "MICROBLOG": | 222 if event_type == "MICROBLOG": |
212 if not data.has_key('content'): | 223 if not data.has_key('content'): |
213 print ("WARNING: No content found in microblog data") | 224 print ("WARNING: No content found in microblog data") |
214 return | 225 return |
215 if data.has_key('groups'): | 226 if data.has_key('groups'): |
216 _groups = set(data['groups'].split() if data['groups'] else []) | 227 _groups = set(data['groups'].split() if data['groups'] else []) |
217 else: | 228 else: |
218 _groups=None | 229 _groups=None |
230 mblog_entry = MicroblogEntry(data) | |
231 | |
219 for panel in self.mpanels: | 232 for panel in self.mpanels: |
220 if isinstance(panel,panels.MicroblogPanel) and (panel.isJidAccepted(sender) or _groups == None or _groups.intersection(panel.accepted_groups)): #TODO: check this | 233 if isinstance(panel, panels.MicroblogPanel): |
221 content = data['content'] | 234 self.addBlogEntry(panel, sender, _groups, mblog_entry) |
222 author = data.get('author') | 235 |
223 timestamp = float(data.get('timestamp',0)) #XXX: int doesn't work here | 236 self.mblog_cache.append((sender, _groups, mblog_entry)) |
224 panel.addEntry(content, author, timestamp) | 237 if len(self.mblog_cache) > MAX_MBLOG_CACHE: |
238 del self.mblog_cache[0:len(self.mblog_cache-MAX_MBLOG_CACHE)] | |
239 | |
240 def addBlogEntry(self, mblog_panel, sender, _groups, mblog_entry): | |
241 """Check if an entry can go in MicroblogPanel and add to it | |
242 @param mblog_panel: MicroblogPanel instance | |
243 @param sender: jid of the entry sender | |
244 @param _groups: groups which can receive this entry | |
245 @param mblog_entry: MicroblogEntry instance""" | |
246 if mblog_panel.isJidAccepted(sender) or _groups == None or _groups.intersection(mblog_panel.accepted_groups): | |
247 mblog_panel.addEntry(mblog_entry) | |
248 | |
249 def FillMicroblogPanel(self, mblog_entry): | |
250 """Fill a microblog panel with entries in cache | |
251 @param mblog_panel: MicroblogPanel instance | |
252 """ | |
253 for cache_entry in self.mblog_cache: | |
254 self.addBlogEntry(mblog_entry, *cache_entry) | |
225 | 255 |
226 def _newMessageCb(self, from_jid, msg, msg_type, to_jid): | 256 def _newMessageCb(self, from_jid, msg, msg_type, to_jid): |
227 _from = JID(from_jid) | 257 _from = JID(from_jid) |
228 _to = JID(to_jid) | 258 _to = JID(to_jid) |
229 for panel in self.mpanels + self.other_panels: | 259 for panel in self.mpanels + self.other_panels: |