comparison 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
comparison
equal deleted inserted replaced
585:bade589dbd5a 586:3eb3a2c0c011
130 130
131 class BridgeCall(LiberviaJsonProxy): 131 class BridgeCall(LiberviaJsonProxy):
132 def __init__(self): 132 def __init__(self):
133 LiberviaJsonProxy.__init__(self, "/json_api", 133 LiberviaJsonProxy.__init__(self, "/json_api",
134 ["getContacts", "addContact", "sendMessage", "sendMblog", "sendMblogComment", 134 ["getContacts", "addContact", "sendMessage", "sendMblog", "sendMblogComment",
135 "getLastMblogs", "getMassiveLastMblogs", "getMblogComments", "getProfileJid", 135 "getMblogs", "getMassiveMblogs", "getMblogComments", "getProfileJid",
136 "getHistory", "getPresenceStatuses", "joinMUC", "mucLeave", "getRoomsJoined", 136 "getHistory", "getPresenceStatuses", "joinMUC", "mucLeave", "getRoomsJoined",
137 "inviteMUC", "launchTarotGame", "getTarotCardsPaths", "tarotGameReady", 137 "inviteMUC", "launchTarotGame", "getTarotCardsPaths", "tarotGameReady",
138 "tarotGamePlayCards", "launchRadioCollective", "getMblogs", "getMblogsWithComments", 138 "tarotGamePlayCards", "launchRadioCollective", "getMblogs", "getMblogsWithComments",
139 "getWaitingSub", "subscription", "delContact", "updateContact", "getCard", 139 "getWaitingSub", "subscription", "delContact", "updateContact", "getCard",
140 "getEntityData", "getParamsUI", "asyncGetParamA", "setParam", "launchAction", 140 "getEntityData", "getParamsUI", "asyncGetParamA", "setParam", "launchAction",
201 self._register.call('registerParams', None) 201 self._register.call('registerParams', None)
202 self._register.call('isRegistered', self._isRegisteredCB) 202 self._register.call('isRegistered', self._isRegisteredCB)
203 self.initialised = False 203 self.initialised = False
204 self.init_cache = [] # used to cache events until initialisation is done 204 self.init_cache = [] # used to cache events until initialisation is done
205 self.cached_params = {} 205 self.cached_params = {}
206 self.next_rsm_index = 0
206 207
207 def importPlugins(self): 208 def importPlugins(self):
208 self.plugins = {} 209 self.plugins = {}
209 inhibited_menus = [] 210 inhibited_menus = []
210 # FIXME: plugins import should be dynamic and generic like in sat 211 # FIXME: plugins import should be dynamic and generic like in sat
539 def _getParamsUICB(self, xml_ui): 540 def _getParamsUICB(self, xml_ui):
540 """Hide the parameters item if there's nothing to display""" 541 """Hide the parameters item if there's nothing to display"""
541 if not xml_ui: 542 if not xml_ui:
542 self.panel.menu.removeItemParams() 543 self.panel.menu.removeItemParams()
543 544
544 def _ownBlogsFills(self, mblogs): 545 def _ownBlogsFills(self, mblogs, mblog_panel=None):
545 #put our own microblogs in cache, then fill all panels with them 546 """Put our own microblogs in cache, then fill the panels with them.
547
548 @param mblogs (dict): dictionary mapping a publisher JID to blogs data.
549 @param mblog_panel (MicroblogPanel): the panel to fill, or all if None.
550 """
551 cache = []
546 for publisher in mblogs: 552 for publisher in mblogs:
547 for mblog in mblogs[publisher]: 553 for mblog in mblogs[publisher][0]:
548 if 'content' not in mblog: 554 if 'content' not in mblog:
549 log.warning("No content found in microblog [%s]" % mblog) 555 log.warning("No content found in microblog [%s]" % mblog)
550 continue 556 continue
551 if 'groups' in mblog: 557 if 'groups' in mblog:
552 _groups = set(mblog['groups'].split() if mblog['groups'] else []) 558 _groups = set(mblog['groups'].split() if mblog['groups'] else [])
553 else: 559 else:
554 _groups = None 560 _groups = None
555 mblog_entry = panels.MicroblogItem(mblog) 561 mblog_entry = panels.MicroblogItem(mblog)
556 self.mblog_cache.append((_groups, mblog_entry)) 562 cache.append((_groups, mblog_entry))
557 563
564 self.mblog_cache.extend(cache)
558 if len(self.mblog_cache) > MAX_MBLOG_CACHE: 565 if len(self.mblog_cache) > MAX_MBLOG_CACHE:
559 del self.mblog_cache[0:len(self.mblog_cache - MAX_MBLOG_CACHE)] 566 del self.mblog_cache[0:len(self.mblog_cache - MAX_MBLOG_CACHE)]
560 for lib_wid in self.libervia_widgets: 567
568 widget_list = [mblog_panel] if mblog_panel else self.libervia_widgets
569 for lib_wid in widget_list:
561 if isinstance(lib_wid, panels.MicroblogPanel): 570 if isinstance(lib_wid, panels.MicroblogPanel):
562 self.FillMicroblogPanel(lib_wid) 571 self.fillMicroblogPanel(lib_wid, cache)
572
573 if self.initialised:
574 return
563 self.initialised = True # initialisation phase is finished here 575 self.initialised = True # initialisation phase is finished here
564 for event_data in self.init_cache: # so we have to send all the cached events 576 for event_data in self.init_cache: # so we have to send all the cached events
565 self._personalEventCb(*event_data) 577 self._personalEventCb(*event_data)
566 del self.init_cache 578 del self.init_cache
567 579
575 self.bridge.call('getWaitingSub', self._getWaitingSubCb) 587 self.bridge.call('getWaitingSub', self._getWaitingSubCb)
576 #we fill the panels already here 588 #we fill the panels already here
577 for lib_wid in self.libervia_widgets: 589 for lib_wid in self.libervia_widgets:
578 if isinstance(lib_wid, panels.MicroblogPanel): 590 if isinstance(lib_wid, panels.MicroblogPanel):
579 if lib_wid.accept_all(): 591 if lib_wid.accept_all():
580 self.bridge.call('getMassiveLastMblogs', lib_wid.massiveInsert, 'ALL', [], 10) 592 self.bridge.call('getMassiveMblogs', lib_wid.massiveInsert, 'ALL', [])
581 else: 593 else:
582 self.bridge.call('getMassiveLastMblogs', lib_wid.massiveInsert, 'GROUP', lib_wid.accepted_groups, 10) 594 self.bridge.call('getMassiveMblogs', lib_wid.massiveInsert, 'GROUP', lib_wid.accepted_groups)
583 595
584 #we ask for our own microblogs: 596 #we ask for our own microblogs:
585 self.bridge.call('getMassiveLastMblogs', self._ownBlogsFills, 'JID', [self.whoami.bare], 10) 597 self.loadOurMainEntries()
586 598
587 # initialize plugins which waited for the connection to be done 599 # initialize plugins which waited for the connection to be done
588 for plugin in self.plugins.values(): 600 for plugin in self.plugins.values():
589 if hasattr(plugin, 'profileConnected'): 601 if hasattr(plugin, 'profileConnected'):
590 plugin.profileConnected() 602 plugin.profileConnected()
603
604 def loadOurMainEntries(self, index=0, mblog_panel=None):
605 """Load a page of our own blogs from the cache or ask them to the
606 backend. Then fill the panels with them.
607
608 @param index (int): starting index of the blog page to retrieve.
609 @param mblog_panel (MicroblogPanel): the panel to fill, or all if None.
610 """
611 delta = index - self.next_rsm_index
612 if delta < 0:
613 assert(mblog_panel is not None)
614 self.fillMicroblogPanel(mblog_panel, self.mblog_cache[index:index + C.RSM_MAX_ITEMS])
615 return
616
617 def cb(result):
618 self._ownBlogsFills(result, mblog_panel)
619
620 rsm = {'max': str(delta + C.RSM_MAX_ITEMS), 'index': str(self.next_rsm_index)}
621 self.bridge.call('getMassiveMblogs', cb, 'JID', [self.whoami.bare], rsm)
622 self.next_rsm_index = index + C.RSM_MAX_ITEMS
591 623
592 ## Signals callbacks ## 624 ## Signals callbacks ##
593 625
594 def _personalEventCb(self, sender, event_type, data): 626 def _personalEventCb(self, sender, event_type, data):
595 if not self.initialised: 627 if not self.initialised:
645 @param mblog_entry: panels.MicroblogItem instance""" 677 @param mblog_entry: panels.MicroblogItem instance"""
646 if mblog_entry.type == "comment" or mblog_panel.isJidAccepted(sender) or (_groups == None and self.whoami and sender == self.whoami.bare) \ 678 if mblog_entry.type == "comment" or mblog_panel.isJidAccepted(sender) or (_groups == None and self.whoami and sender == self.whoami.bare) \
647 or (_groups and _groups.intersection(mblog_panel.accepted_groups)): 679 or (_groups and _groups.intersection(mblog_panel.accepted_groups)):
648 mblog_panel.addEntry(mblog_entry) 680 mblog_panel.addEntry(mblog_entry)
649 681
650 def FillMicroblogPanel(self, mblog_panel): 682 def fillMicroblogPanel(self, mblog_panel, mblogs):
651 """Fill a microblog panel with entries in cache 683 """Fill a microblog panel with entries in cache
652 @param mblog_panel: MicroblogPanel instance 684 @param mblog_panel: MicroblogPanel instance
653 """ 685 """
654 #XXX: only our own entries are cached 686 #XXX: only our own entries are cached
655 for cache_entry in self.mblog_cache: 687 for cache_entry in mblogs:
656 _groups, mblog_entry = cache_entry 688 _groups, mblog_entry = cache_entry
657 self.addBlogEntry(mblog_panel, self.whoami.bare, *cache_entry) 689 self.addBlogEntry(mblog_panel, self.whoami.bare, *cache_entry)
658 690
659 def getEntityMBlog(self, entity): 691 def getEntityMBlog(self, entity):
660 log.info("geting mblog for entity [%s]" % (entity,)) 692 log.info("geting mblog for entity [%s]" % (entity,))
661 for lib_wid in self.libervia_widgets: 693 for lib_wid in self.libervia_widgets:
662 if isinstance(lib_wid, panels.MicroblogPanel): 694 if isinstance(lib_wid, panels.MicroblogPanel):
663 if lib_wid.isJidAccepted(entity): 695 if lib_wid.isJidAccepted(entity):
664 self.bridge.call('getMassiveLastMblogs', lib_wid.massiveInsert, 'JID', [entity], 10) 696 self.bridge.call('getMassiveMblogs', lib_wid.massiveInsert, 'JID', [entity])
665 697
666 def getLiberviaWidget(self, class_, entity, ignoreOtherTabs=True): 698 def getLiberviaWidget(self, class_, entity, ignoreOtherTabs=True):
667 """Get the corresponding panel if it exists. 699 """Get the corresponding panel if it exists.
668 @param class_ (class): class of the panel (ChatPanel, MicroblogPanel...) 700 @param class_ (class): class of the panel (ChatPanel, MicroblogPanel...)
669 @param entity (dict): dictionnary to define the entity. 701 @param entity (dict): dictionnary to define the entity.