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.
This commit is contained in:
Goffi 2016-09-13 22:02:52 +02:00
commit 6395a46e87

View file

@ -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)