comparison src/memory/memory.py @ 742:03744d9ebc13

plugin XEP-0033: implementation of the addressing feature: - frontends pass the recipients in the extra parameter of sendMessage - backend checks if the target server supports the feature (this is not done yet by prosody plugin) - features and identities are cached per profile and server - messages are duplicated in history for now (TODO: redesign the database) - echos signals are also duplicated to the sender (FIXME)
author souliane <souliane@mailoo.org>
date Wed, 11 Dec 2013 17:16:53 +0100
parents 8f50a0079769
children 5aff0beddb28
comparison
equal deleted inserted replaced
741:00318e60a06a 742:03744d9ebc13
754 754
755 def getHistory(self, from_jid, to_jid, limit=0, between=True, profile="@NONE@"): 755 def getHistory(self, from_jid, to_jid, limit=0, between=True, profile="@NONE@"):
756 assert profile != "@NONE@" 756 assert profile != "@NONE@"
757 return self.storage.getHistory(jid.JID(from_jid), jid.JID(to_jid), limit, between, profile) 757 return self.storage.getHistory(jid.JID(from_jid), jid.JID(to_jid), limit, between, profile)
758 758
759 def addServerFeature(self, feature, profile): 759 def addServerFeature(self, feature, jid_, profile):
760 """Add a feature discovered from server 760 """Add a feature discovered from server
761 @param feature: string of the feature 761 @param feature: string of the feature
762 @param profile: which profile is using this server ?""" 762 @param jid_: the jid of the target server
763 @param profile: which profile is asking this server ?"""
763 if profile not in self.server_features: 764 if profile not in self.server_features:
764 self.server_features[profile] = [] 765 self.server_features[profile] = {}
765 self.server_features[profile].append(feature) 766 features = self.server_features[profile].setdefault(jid_, [])
766 767 features.append(feature)
767 def addServerIdentity(self, category, type_, entity, profile): 768
769 def addServerIdentity(self, category, type_, entity, jid_, profile):
768 """Add an identity discovered from server 770 """Add an identity discovered from server
769 @param feature: string of the feature 771 @param feature: string of the feature
770 @param profile: which profile is using this server ?""" 772 @param jid_: the jid of the target server
773 @param profile: which profile is asking this server ?"""
771 if not profile in self.server_identities: 774 if not profile in self.server_identities:
772 self.server_identities[profile] = {} 775 self.server_identities[profile] = {}
773 if (category, type_) not in self.server_identities[profile]: 776 identities = self.server_identities[profile].setdefault(jid_, {})
774 self.server_identities[profile][(category, type_)] = set() 777 if (category, type_) not in identities:
775 self.server_identities[profile][(category, type_)].add(entity) 778 identities[(category, type_)] = set()
776 779 identities[(category, type_)].add(entity)
777 def getServerServiceEntities(self, category, type_, profile): 780
781 def getServerServiceEntities(self, category, type_, jid_=None, profile=None):
778 """Return all available entities for a service""" 782 """Return all available entities for a service"""
779 if profile in self.server_identities: 783 if jid_ is None:
780 return self.server_identities[profile].get((category, type_), set()) 784 jid_ = self.host.getClientHostJid(profile)
785 if profile in self.server_identities and jid_ in self.server_identities[profile]:
786 return self.server_identities[profile][jid_].get((category, type_), set())
781 else: 787 else:
782 return None 788 return None
783 789
784 def getServerServiceEntity(self, category, type_, profile): 790 def getServerServiceEntity(self, category, type_, jid_=None, profile=None):
785 """Helper method to get first available entity for a service""" 791 """Helper method to get first available entity for a service"""
786 entities = self.getServerServiceEntities(category, type_, profile) 792 if jid_ is None:
793 jid_ = self.host.getClientHostJid(profile)
794 entities = self.getServerServiceEntities(category, type_, jid_, profile)
787 if entities is None: 795 if entities is None:
788 warning(_("Entities (%(category)s/%(type)s) not available, maybe they haven't been asked to server yet ?") % {"category": category, 796 warning(_("Entities (%(category)s/%(type)s) of %(server)s not available, maybe they haven't been asked yet?")
789 "type": type_}) 797 % {"category": category, "type": type_, "server": jid_})
790 return None 798 return None
791 else: 799 else:
792 return list(entities)[0] if entities else None 800 return list(entities)[0] if entities else None
793 801
794 def hasServerFeature(self, feature, profile_key): 802 def hasServerFeature(self, feature, jid_=None, profile_key="@NONE@"):
795 """Tell if the server of the profile has the required feature""" 803 """Tell if the server of the profile has the required feature"""
796 profile = self.getProfileName(profile_key) 804 profile = self.getProfileName(profile_key)
797 if not profile: 805 if not profile:
798 error(_('Trying find server feature for a non-existant profile')) 806 error(_('Trying find server feature for a non-existant profile'))
799 return 807 return None
800 assert profile in self.server_features 808 assert profile in self.server_features
801 return feature in self.server_features[profile] 809 if jid_ is None:
810 jid_ = self.host.getClientHostJid(profile)
811 if jid_ in self.server_features[profile]:
812 return feature in self.server_features[profile][jid_]
813 else:
814 warning(_("Features of %s not available, maybe they haven't been asked yet?") % jid_)
815 return None
802 816
803 def getLastResource(self, contact, profile_key): 817 def getLastResource(self, contact, profile_key):
804 """Return the last resource used by a contact 818 """Return the last resource used by a contact
805 @param contact: contact jid (unicode) 819 @param contact: contact jid (unicode)
806 @param profile_key: %(doc_profile_key)s""" 820 @param profile_key: %(doc_profile_key)s"""