plugin XEP-0054: cache now use storage
bridge: getCardCache updated to manage profile
This commit is contained in:
parent
6f113496ee
commit
a88559442a
1 changed files with 28 additions and 18 deletions
|
|
@ -26,7 +26,6 @@ from twisted.words.protocols.jabber import client, jid, xmlstream
|
|||
from twisted.words.protocols.jabber import error as jab_error
|
||||
from twisted.words.protocols.jabber.xmlstream import IQ
|
||||
import os.path
|
||||
import pdb
|
||||
|
||||
from zope.interface import implements
|
||||
|
||||
|
|
@ -35,6 +34,7 @@ from wokkel import disco, iwokkel
|
|||
from base64 import b64decode
|
||||
from hashlib import sha1
|
||||
from time import sleep
|
||||
from sat.memory.persistent import PersistentBinaryDict
|
||||
|
||||
try:
|
||||
from twisted.words.protocols.xmlstream import XMPPHandler
|
||||
|
|
@ -68,7 +68,6 @@ class XEP_0054():
|
|||
info(_("Plugin XEP_0054 initialization"))
|
||||
self.host = host
|
||||
self.avatar_path = os.path.join(self.host.memory.getConfig('', 'local_dir'), AVATAR_PATH)
|
||||
self.vcard_cache = host.memory.getPrivate("vcard_cache") or {} #used to store nicknames and avatar, key = jid
|
||||
if not os.path.exists(self.avatar_path):
|
||||
os.makedirs(self.avatar_path)
|
||||
host.bridge.addMethod("getCard", ".plugin", in_sign='ss', out_sign='s', method=self.getCard)
|
||||
|
|
@ -87,23 +86,26 @@ class XEP_0054():
|
|||
@param value: new value of the item
|
||||
@param profile: profile which received the update
|
||||
"""
|
||||
if not self.vcard_cache.has_key(jid.userhost()):
|
||||
self.vcard_cache[jid.userhost()] = {}
|
||||
client = self.host.getClient(profile)
|
||||
if not jid.userhost() in client._vcard_cache:
|
||||
client._vcard_cache[jid.userhost()] = {}
|
||||
|
||||
cache = self.vcard_cache[jid.userhost()]
|
||||
old_value = cache[name] if cache.has_key(name) else None
|
||||
cache = client._vcard_cache[jid.userhost()]
|
||||
old_value = cache[name] if name in cache else None
|
||||
if not old_value or value != old_value:
|
||||
cache[name] = value
|
||||
self.host.memory.setPrivate("vcard_cache", self.vcard_cache)
|
||||
client._vcard_cache.force(jid.userhost()) #we force saving of data to storage
|
||||
self.host.bridge.updatedValue('card_'+name, {'jid':jid.userhost(), name:value}, profile)
|
||||
|
||||
def get_cache(self, jid, name):
|
||||
def get_cache(self, jid, name, profile):
|
||||
"""return cached value for jid
|
||||
@param jid: target contact
|
||||
@param name: name of the value ('nick' or 'avatar')
|
||||
@param profile: %(doc_profile)s
|
||||
@return: wanted value or None"""
|
||||
client = self.host.getClient(profile)
|
||||
try:
|
||||
return self.vcard_cache[jid.userhost()][name]
|
||||
return client._vcard_cache[jid.userhost()][name]
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
|
|
@ -204,16 +206,22 @@ class XEP_0054():
|
|||
return ""
|
||||
return filename
|
||||
|
||||
def getCardCache(self, target):
|
||||
def getCardCache(self, target, profile_key):
|
||||
"""Request for cached values of profile
|
||||
return the cached nickname and avatar if exists, else get VCard
|
||||
@param target: target's jid
|
||||
@param profile_key: %(doc_profile_key)s
|
||||
"""
|
||||
profile = self.host.memory.getProfileName(profile_key)
|
||||
if not profile:
|
||||
error(_("Profile not found"))
|
||||
return {}
|
||||
to_jid = jid.JID(target)
|
||||
result = {}
|
||||
nick = self.get_cache(to_jid, 'nick')
|
||||
nick = self.get_cache(to_jid, 'nick', profile)
|
||||
if nick:
|
||||
result['nick'] = nick
|
||||
avatar = self.get_cache(to_jid, 'avatar')
|
||||
avatar = self.get_cache(to_jid, 'avatar', profile)
|
||||
if avatar:
|
||||
result['avatar'] = avatar
|
||||
return result
|
||||
|
|
@ -228,6 +236,7 @@ class XEP_0054_handler(XMPPHandler):
|
|||
self.host = plugin_parent.host
|
||||
|
||||
def connectionInitialized(self):
|
||||
self.parent._vcard_cache = PersistentBinaryDict(NS_VCARD, self.parent.profile)
|
||||
self.xmlstream.addObserver(VCARD_UPDATE, self.update)
|
||||
|
||||
def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
|
||||
|
|
@ -241,11 +250,12 @@ class XEP_0054_handler(XMPPHandler):
|
|||
return the cached nickname if exists, else get VCard
|
||||
"""
|
||||
from_jid = jid.JID(presence['from'])
|
||||
#FIXME: wokkel's data_form should be used here
|
||||
x_elem = filter (lambda x:x.name == "x", presence.elements())[0] #We only want the "x" element
|
||||
for elem in x_elem.elements():
|
||||
if elem.name == 'photo':
|
||||
hash = str(elem)
|
||||
old_avatar = self.plugin_parent.get_cache(from_jid, 'avatar')
|
||||
old_avatar = self.plugin_parent.get_cache(from_jid, 'avatar', self.parent.profile)
|
||||
if not old_avatar or old_avatar != hash:
|
||||
debug(_('New avatar found, requesting vcard'))
|
||||
self.plugin_parent.getCard(from_jid.userhost(), self.parent.profile)
|
||||
|
|
|
|||
Loading…
Reference in a new issue