From 6395a46e87ca87af18770abd541daad0a27a830f Mon Sep 17 00:00:00 2001 From: Goffi Date: Tue, 13 Sep 2016 22:02:52 +0200 Subject: [PATCH] plugin XEP-0054(XEP-0153): added a getAvatar: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/plugins/plugin_xep_0054.py | 35 +++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/plugins/plugin_xep_0054.py b/src/plugins/plugin_xep_0054.py index 9853c455..93a5834a 100755 --- a/src/plugins/plugin_xep_0054.py +++ b/src/plugins/plugin_xep_0054.py @@ -86,8 +86,9 @@ class XEP_0054(object): 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 @@ class XEP_0054(object): 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)