Move rest of UserAvatar into new module
This commit is contained in:
parent
0a6b2126b3
commit
ccb3c2decc
3 changed files with 64 additions and 49 deletions
|
@ -152,6 +152,7 @@ class PEPEventType(IntEnum):
|
||||||
MOOD = 2
|
MOOD = 2
|
||||||
LOCATION = 3
|
LOCATION = 3
|
||||||
NICKNAME = 4
|
NICKNAME = 4
|
||||||
|
AVATAR = 5
|
||||||
|
|
||||||
|
|
||||||
ACTIVITIES = {
|
ACTIVITIES = {
|
||||||
|
|
|
@ -21,15 +21,31 @@ import binascii
|
||||||
import nbxmpp
|
import nbxmpp
|
||||||
|
|
||||||
from gajim.common import app
|
from gajim.common import app
|
||||||
|
from gajim.common.const import PEPEventType
|
||||||
from gajim.common.exceptions import StanzaMalformed
|
from gajim.common.exceptions import StanzaMalformed
|
||||||
|
from gajim.common.modules.pep import AbstractPEPModule, AbstractPEPData
|
||||||
|
|
||||||
log = logging.getLogger('gajim.c.m.user_avatar')
|
log = logging.getLogger('gajim.c.m.user_avatar')
|
||||||
|
|
||||||
|
|
||||||
class UserAvatar:
|
class UserAvatarData(AbstractPEPData):
|
||||||
|
|
||||||
|
type_ = PEPEventType.AVATAR
|
||||||
|
|
||||||
|
def __init__(self, avatar):
|
||||||
|
self._pep_specific_data = avatar
|
||||||
|
|
||||||
|
|
||||||
|
class UserAvatar(AbstractPEPModule):
|
||||||
|
|
||||||
|
name = 'user-avatar'
|
||||||
|
namespace = 'urn:xmpp:avatar:metadata'
|
||||||
|
pep_class = UserAvatarData
|
||||||
|
store_publish = False
|
||||||
|
_log = log
|
||||||
|
|
||||||
def __init__(self, con):
|
def __init__(self, con):
|
||||||
self._con = con
|
AbstractPEPModule.__init__(self, con, con.name)
|
||||||
self._account = con.name
|
|
||||||
|
|
||||||
self.handlers = []
|
self.handlers = []
|
||||||
|
|
||||||
|
@ -83,7 +99,7 @@ class UserAvatar:
|
||||||
log.warning('Error: %s %s', stanza.getFrom(), error)
|
log.warning('Error: %s %s', stanza.getFrom(), error)
|
||||||
return
|
return
|
||||||
|
|
||||||
log.info('Received: %s %s', jid, sha)
|
log.info('Received Avatar: %s %s', jid, sha)
|
||||||
app.interface.save_avatar(data)
|
app.interface.save_avatar(data)
|
||||||
|
|
||||||
if self._con.get_own_jid().bareMatch(jid):
|
if self._con.get_own_jid().bareMatch(jid):
|
||||||
|
@ -94,3 +110,45 @@ class UserAvatar:
|
||||||
app.contacts.set_avatar(self._account, jid, sha)
|
app.contacts.set_avatar(self._account, jid, sha)
|
||||||
|
|
||||||
app.interface.update_avatar(self._account, jid)
|
app.interface.update_avatar(self._account, jid)
|
||||||
|
|
||||||
|
def _extract_info(self, item):
|
||||||
|
metadata = item.getTag('metadata', namespace=self.namespace)
|
||||||
|
if metadata is None:
|
||||||
|
raise StanzaMalformed('No metadata node')
|
||||||
|
|
||||||
|
info = metadata.getTags('info', one=True)
|
||||||
|
if not info:
|
||||||
|
return None
|
||||||
|
|
||||||
|
avatar = info.getAttrs()
|
||||||
|
return avatar or None
|
||||||
|
|
||||||
|
def _update_contacts(self, jid, user_pep):
|
||||||
|
avatar = user_pep._pep_specific_data
|
||||||
|
own_jid = self._con.get_own_jid()
|
||||||
|
if avatar is None:
|
||||||
|
# Remove avatar
|
||||||
|
log.info('Remove: %s', jid)
|
||||||
|
app.contacts.set_avatar(self._account, str(jid), None)
|
||||||
|
app.logger.set_avatar_sha(own_jid.getStripped(), str(jid), None)
|
||||||
|
app.interface.update_avatar(self._account, str(jid))
|
||||||
|
else:
|
||||||
|
if own_jid.bareMatch(jid):
|
||||||
|
sha = app.config.get_per(
|
||||||
|
'accounts', self._account, 'avatar_sha')
|
||||||
|
else:
|
||||||
|
sha = app.contacts.get_avatar_sha(self._account, str(jid))
|
||||||
|
|
||||||
|
if sha == avatar['id']:
|
||||||
|
log.info('Avatar already known: %s %s',
|
||||||
|
jid, avatar['id'])
|
||||||
|
return
|
||||||
|
self.get_pubsub_avatar(jid, avatar['id'])
|
||||||
|
|
||||||
|
def send(self, data):
|
||||||
|
# Not implemented yet
|
||||||
|
return
|
||||||
|
|
||||||
|
def retract(self):
|
||||||
|
# Not implemented yet
|
||||||
|
return
|
||||||
|
|
|
@ -80,48 +80,4 @@ class AbstractPEP(object):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class AvatarNotificationPEP(AbstractPEP):
|
SUPPORTED_PERSONAL_USER_EVENTS = []
|
||||||
'''XEP-0084: Avatars'''
|
|
||||||
|
|
||||||
type_ = 'avatar-notification'
|
|
||||||
namespace = 'urn:xmpp:avatar:metadata'
|
|
||||||
|
|
||||||
def _extract_info(self, items):
|
|
||||||
self.avatar = None
|
|
||||||
for item in items.getTags('item'):
|
|
||||||
metadata = item.getTag('metadata')
|
|
||||||
if metadata is None:
|
|
||||||
app.log('c.m.user_avatar').warning(
|
|
||||||
'Invalid avatar stanza:\n%s', items)
|
|
||||||
break
|
|
||||||
info = item.getTag('metadata').getTag('info')
|
|
||||||
if info is not None:
|
|
||||||
self.avatar = info.getAttrs()
|
|
||||||
break
|
|
||||||
|
|
||||||
return (None, False)
|
|
||||||
|
|
||||||
def _on_receive(self, jid, account):
|
|
||||||
con = app.connections[account]
|
|
||||||
if self.avatar is None:
|
|
||||||
# Remove avatar
|
|
||||||
app.log('c.m.user_avatar').info('Remove: %s', jid)
|
|
||||||
app.contacts.set_avatar(account, jid, None)
|
|
||||||
own_jid = con.get_own_jid().getStripped()
|
|
||||||
app.logger.set_avatar_sha(own_jid, jid, None)
|
|
||||||
app.interface.update_avatar(account, jid)
|
|
||||||
else:
|
|
||||||
sha = app.contacts.get_avatar_sha(account, jid)
|
|
||||||
app.log('c.m.user_avatar').info(
|
|
||||||
'Update: %s %s', jid, self.avatar['id'])
|
|
||||||
if sha == self.avatar['id']:
|
|
||||||
app.log('c.m.user_avatar').info(
|
|
||||||
'Avatar already known: %s %s',
|
|
||||||
jid, self.avatar['id'])
|
|
||||||
return
|
|
||||||
con.get_module('UserAvatar').get_pubsub_avatar(
|
|
||||||
jid, self.avatar['id'])
|
|
||||||
|
|
||||||
|
|
||||||
SUPPORTED_PERSONAL_USER_EVENTS = [
|
|
||||||
AvatarNotificationPEP]
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue