# HG changeset patch # User Goffi # Date 1473796972 -7200 # Node ID 11fb5f5e2f899e06a062ec878fef330b4ac86cb3 # Parent c2fdee1bd908b654a9ecc0b64a852f8677fcc46f plugin XEP-0054(XEP-0153): added a getAvatar: getAvatar can be used to do the whole process to get avatar file (or empty string if not avatar is found) : it first look in cache and request vCard if nothing is found. diff -r c2fdee1bd908 -r 11fb5f5e2f89 src/plugins/plugin_xep_0054.py --- a/src/plugins/plugin_xep_0054.py Sun Sep 11 23:17:31 2016 +0200 +++ b/src/plugins/plugin_xep_0054.py Tue Sep 13 22:02:52 2016 +0200 @@ -86,8 +86,9 @@ if not os.path.exists(self.avatar_path): os.makedirs(self.avatar_path) self.cache = {} - host.bridge.addMethod("getCard", ".plugin", in_sign='ss', out_sign='s', method=self._getCard) + host.bridge.addMethod("getCard", ".plugin", in_sign='ss', out_sign='', method=self._getCard, async=True) host.bridge.addMethod("getAvatarFile", ".plugin", in_sign='s', out_sign='s', method=self.getAvatarFile) + host.bridge.addMethod("getAvatar", ".plugin", in_sign='ss', out_sign='s', method=self._getAvatar, async=True) host.bridge.addMethod("setAvatar", ".plugin", in_sign='ss', out_sign='', method=self.setAvatar, async=True) host.trigger.add("presence_available", self.presenceAvailableTrigger) host.memory.setSignalOnUpdate("avatar") @@ -307,20 +308,44 @@ reg_request["from"] = client.jid.full() reg_request["to"] = to_jid.full() reg_request.addElement('vCard', NS_VCARD) - reg_request.send(to_jid.full()).addCallbacks(self._getCardCb, self._getCardEb, callbackArgs=[to_jid, client], errbackArgs=[to_jid, client]) - return reg_request["id"] + d = reg_request.send(to_jid.full()).addCallbacks(self._getCardCb, self._getCardEb, callbackArgs=[to_jid, client], errbackArgs=[to_jid, client]) + return d def getAvatarFile(self, avatar_hash): """Give the full path of avatar from hash - @param hash: SHA1 hash - @return full_path + @param avatar_hash(unicode): SHA1 hash + @return(unicode): full_path or empty string if avatar_hash is empty + or if avatar is not found in cache """ + if not avatar_hash: + return "" filename = self.avatar_path + '/' + avatar_hash if not os.path.exists(filename): log.error(_(u"Asking for an uncached avatar [%s]") % avatar_hash) return "" return filename + def _getAvatar(self, entity, profile): + client = self.host.getClient(profile) + return self.getAvatar(client, jid.JID(entity)) + + def _getAvatarGotCard(self, dummy, client, entity): + try: + self.cache[client.profile][entity.full()]['avatar'] + except KeyError: + return "" + + def getAvatar(self, client, entity): + try: + avatar_hash = self.cache[client.profile][entity.full()]['avatar'] + except KeyError: + d = self.getCard(client, entity) + d.addCallback(self._getAvatarGotCard, client, entity) + else: + d = defer.succeed(avatar_hash) + d.addCallback(self.getAvatarFile) + return d + def _buildSetAvatar(self, vcard_set, filepath): try: img = Image.open(filepath)