diff src/memory/memory.py @ 1251:51a85e8f599a

memory: add method isContactConnected
author souliane <souliane@mailoo.org>
date Mon, 20 Oct 2014 16:38:49 +0200
parents c6cf44e6330b
children c585df65828a
line wrap: on
line diff
--- a/src/memory/memory.py	Mon Oct 20 15:09:37 2014 +0200
+++ b/src/memory/memory.py	Mon Oct 20 16:38:49 2014 +0200
@@ -420,6 +420,22 @@
         log.debug("Memory getPresenceStatus (%s)" % entities_presence)
         return entities_presence
 
+    def isContactConnected(self, entity_jid, profile_key):
+        """Tell from the presence information if the given contact is connected.
+
+        @param entity_jid (JID): the entity to check
+        @param profile_key: %(doc_profile_key)s
+        @return: boolean
+        """
+        profile = self.getProfileName(profile_key)
+        if not profile:
+            raise exceptions.ProfileUnknownError(_('Trying to get entity data for a non-existant profile'))
+        try:
+            presence = self._entities_cache[profile][entity_jid]['presence']
+            return len([True for status in presence.values() if status[0] != 'unavailable']) > 0
+        except KeyError:
+            return False
+
     def setPresenceStatus(self, entity_jid, show, priority, statuses, profile_key):
         """Change the presence status of an entity
         @param entity_jid: jid.JID of the entity