core, frontends: avatar refactoring:

/!\ huge commit

Avatar logic has been reworked around the IDENTITY plugin: plugins able to handle avatar
or other identity related metadata (like nicknames) register to IDENTITY plugin in the
same way as for other features like download/upload. Once registered, IDENTITY plugin will
call them when suitable in order of priority, and handle caching.

Methods to manage those metadata from frontend now use serialised data.

For now `avatar` and `nicknames` are handled:

- `avatar` is now a dict with `path` + metadata like `media_type`, instead of just a string
  path
- `nicknames` is now a list of nicknames in order of priority. This list is never empty,
  and `nicknames[0]` should be the preferred nickname to use by frontends in most cases.
  In addition to contact specified nicknames, user set nickname (the one set in roster) is
  used in priority when available.

Among the side changes done with this commit, there are:

- a new `contactGet` bridge method to get roster metadata for a single contact
- SatPresenceProtocol.send returns a Deferred to check when it has actually been sent
- memory's methods to handle entities data now use `client` as first argument
- metadata filter can be specified with `getIdentity`
- `getAvatar` and `setAvatar` are now part of the IDENTITY plugin instead of XEP-0054 (and
  there signature has changed)
- `isRoom` and `getBareOrFull` are now part of XEP-0045 plugin
- jp avatar/get command uses `xdg-open` first when available for `--show` flag
- `--no-cache` has been added to jp avatar/get and identity/get
- jp identity/set has been simplified, explicit options (`--nickname` only for now) are
  used instead of `--field`. `--field` may come back in the future if necessary for extra
  data.
- QuickContactList `SetContact` now handle None as a value, and doesn't use it to delete the
  metadata anymore
- improved cache handling for `metadata` and `nicknames` in quick frontend
- new `default` argument in QuickContactList `getCache`
This commit is contained in:
Goffi 2020-04-14 21:00:33 +02:00
commit 88072e0453
4 changed files with 1111 additions and 747 deletions

View file

@ -96,7 +96,7 @@ class Const(constants.Const):
LISTENERS = {
"avatar",
"nick",
"nicknames",
"presence",
"selected",
"notification",

View file

@ -44,7 +44,7 @@ class ProfileManager(object):
# and a way to keep some XMLUI request between sessions is expected in backend
host = None
bridge = None
# cache_keys_to_get = ['avatar']
cache_keys_to_get = ['avatar', 'nicknames']
def __init__(self, profile):
self.profile = profile
@ -136,14 +136,10 @@ class ProfileManager(object):
def _plug_profile_getFeaturesCb(self, features):
self.host.features = features
# FIXME: we don't use cached value at the moment, but keep the code for later use
# it was previously used for avatars, but as we don't get full path here,
# it's better to request later
# self.host.bridge.getEntitiesData([], ProfileManager.cache_keys_to_get,
# profile=self.profile,
# callback=self._plug_profile_gotCachedValues,
# errback=self._plug_profile_failedCachedValues)
self._plug_profile_gotCachedValues({})
self.host.bridge.getEntitiesData([], ProfileManager.cache_keys_to_get,
profile=self.profile,
callback=self._plug_profile_gotCachedValues,
errback=self._plug_profile_failedCachedValues)
def _plug_profile_failedCachedValues(self, failure):
log.error("Couldn't get cached values: {}".format(failure))
@ -186,13 +182,6 @@ class ProfileManager(object):
)
def _plug_profile_gotPresences(self, presences):
def gotEntityData(data, contact):
for key in ("avatar", "nick"):
if key in data:
self.host.entityDataUpdatedHandler(
contact, key, data[key], self.profile
)
for contact in presences:
for res in presences[contact]:
jabber_id = ("%s/%s" % (jid.JID(contact).bare, res)) if res else contact
@ -202,15 +191,6 @@ class ProfileManager(object):
self.host.presenceUpdateHandler(
jabber_id, show, priority, statuses, self.profile
)
self.host.bridge.getEntityData(
contact,
["avatar", "nick"],
self.profile,
callback=lambda data, contact=contact: gotEntityData(data, contact),
errback=lambda failure, contact=contact: log.debug(
"No cache data for {}".format(contact)
),
)
# At this point, profile should be fully plugged
# and we launch frontend specific method
@ -553,9 +533,9 @@ class QuickApp(object):
- contactsFilled: called when contact have been fully filled for a profiles
kwargs: profile
- avatar: called when avatar data is updated
args: (entity, avatar file, profile)
- nick: called when nick data is updated
args: (entity, new_nick, profile)
args: (entity, avatar_data, profile)
- nicknames: called when nicknames data is updated
args: (entity, nicknames, profile)
- presence: called when a presence is received
args: (entity, show, priority, statuses, profile)
- selected: called when a widget is selected
@ -1261,15 +1241,18 @@ class QuickApp(object):
target = jid.JID(jid_s)
self.contact_lists[profile].removeContact(target)
def entityDataUpdatedHandler(self, entity_s, key, value, profile):
def entityDataUpdatedHandler(self, entity_s, key, value_raw, profile):
entity = jid.JID(entity_s)
if key == "nick": # this is the roster nick, not the MUC nick
value = data_format.deserialise(value_raw, type_check=None)
if key == "nicknames":
assert isinstance(value, list) or value is None
if entity in self.contact_lists[profile]:
self.contact_lists[profile].setCache(entity, "nick", value)
self.callListeners("nick", entity, value, profile=profile)
self.contact_lists[profile].setCache(entity, "nicknames", value)
self.callListeners("nicknames", entity, value, profile=profile)
elif key == "avatar" and self.AVATARS_HANDLER:
if value and entity in self.contact_lists[profile]:
self.getAvatar(entity, ignore_cache=True, profile=profile)
assert isinstance(value, dict) or value is None
self.contact_lists[profile].setCache(entity, "avatar", value)
self.callListeners("avatar", entity, value, profile=profile)
def actionManager(self, action_data, callback=None, ui_show_cb=None, user_action=True,
progress_cb=None, progress_eb=None, profile=C.PROF_KEY_NONE):
@ -1394,70 +1377,6 @@ class QuickApp(object):
errback=self.dialogFailure,
)
def _avatarGetCb(self, avatar_path, entity, contact_list, profile):
path = avatar_path or self.getDefaultAvatar(entity)
contact_list.setCache(entity, "avatar", path)
self.callListeners("avatar", entity, path, profile=profile)
def _avatarGetEb(self, failure_, entity, contact_list):
# FIXME: bridge needs a proper error handling
if "NotFound" in str(failure_):
log.info("No avatar found for {entity}".format(entity=entity))
else:
log.warning("Can't get avatar: {}".format(failure_))
contact_list.setCache(entity, "avatar", self.getDefaultAvatar(entity))
def getAvatar(
self,
entity,
cache_only=True,
hash_only=False,
ignore_cache=False,
profile=C.PROF_KEY_NONE,
):
"""return avatar path for an entity
@param entity(jid.JID): entity to get avatar from
@param cache_only(bool): if False avatar will be requested if not in cache
with current vCard based implementation, it's better to keep True
except if we request avatars for roster items
@param hash_only(bool): if True avatar hash is returned, else full path
@param ignore_cache(bool): if False, won't check local cache and will request
backend in every case
@return (unicode, None): avatar full path (None if no avatar found)
"""
contact_list = self.contact_lists[profile]
if ignore_cache:
avatar = None
else:
try:
avatar = contact_list.getCache(entity, "avatar", bare_default=None)
except exceptions.NotFound:
avatar = None
if avatar is None:
self.bridge.avatarGet(
str(entity),
cache_only,
hash_only,
profile=profile,
callback=lambda path: self._avatarGetCb(
path, entity, contact_list, profile
),
errback=lambda failure: self._avatarGetEb(failure, entity, contact_list),
)
# we set avatar to empty string to avoid requesting several time the same
# avatar while we are waiting for avatarGet result
contact_list.setCache(entity, "avatar", "")
return avatar
def getDefaultAvatar(self, entity=None):
"""return default avatar to use with given entity
must be implemented by frontend
@param entity(jid.JID): entity for which a default avatar is needed
"""
raise NotImplementedError
def disconnect(self, profile):
log.info("disconnecting")
self.callListeners("disconnect", profile=profile)

View file

@ -29,12 +29,6 @@ import time
log = getLogger(__name__)
try:
from locale import getlocale
except ImportError:
# FIXME: pyjamas workaround
getlocale = lambda x: (None, "utf-8")
ROOM_USER_JOINED = "ROOM_USER_JOINED"
ROOM_USER_LEFT = "ROOM_USER_LEFT"
@ -146,9 +140,15 @@ class Message:
@property
def avatar(self):
"""avatar full path or None if no avatar is found"""
ret = self.host.getAvatar(self.from_jid, profile=self.profile)
return ret
"""avatar data or None if no avatar is found"""
entity = self.from_jid
contact_list = self.host.contact_lists[self.profile]
try:
return contact_list.getCache(entity, "avatar")
except (exceptions.NotFound, KeyError):
# we don't check the result as the avatar listener will be called
self.host.bridge.avatarGet(entity, True, self.profile)
return None
@property
def encrypted(self):
@ -169,12 +169,24 @@ class Message:
):
return entity.resource or ""
if entity.bare in contact_list:
return (
contact_list.getCache(entity, "nick")
or contact_list.getCache(entity, "name")
or entity.node
or entity
)
try:
nicknames = contact_list.getCache(entity, "nicknames")
except (exceptions.NotFound, KeyError):
# we check result as listener will be called
self.host.bridge.identityGet(
entity.bare, ["nicknames"], True, self.profile)
return entity.node or entity
if nicknames:
return nicknames[0]
else:
return (
contact_list.getCache(entity, "name", default=None)
or entity.node
or entity
)
return entity.node or entity
@property
@ -214,14 +226,14 @@ class Message:
return self.extra.get(C.MESS_KEY_ATTACHMENTS)
class MessageWidget(object):
class MessageWidget:
"""Base classe for widgets"""
# This class does nothing and is only used to have a common ancestor
pass
class Occupant(object):
class Occupant:
"""Occupant metadata"""
def __init__(self, parent, data, profile):
@ -616,7 +628,7 @@ class QuickChat(quick_widgets.QuickWidget):
if self.type == C.CHAT_ONE2ONE:
special = self.host.contact_lists[self.profile].getCache(
self.target, C.CONTACT_SPECIAL, create_if_not_found=True
self.target, C.CONTACT_SPECIAL, create_if_not_found=True, default=None
)
if special == C.CONTACT_SPECIAL_GROUP:
# we have a private conversation
@ -900,11 +912,11 @@ class QuickChat(quick_widgets.QuickWidget):
else:
mess_data.status = status
def onAvatar(self, entity, filename, profile):
def onAvatar(self, entity, avatar_data, profile):
if self.type == C.CHAT_GROUP:
if entity.bare == self.target:
try:
self.occupants[entity.resource].update({"avatar": filename})
self.occupants[entity.resource].update({"avatar": avatar_data})
except KeyError:
# can happen for a message in history where the
# entity is not here anymore
@ -913,7 +925,7 @@ class QuickChat(quick_widgets.QuickWidget):
for m in list(self.messages.values()):
if m.nick == entity.resource:
for w in m.widgets:
w.update({"avatar": filename})
w.update({"avatar": avatar_data})
else:
if (
entity.bare == self.target.bare
@ -923,7 +935,7 @@ class QuickChat(quick_widgets.QuickWidget):
for m in list(self.messages.values()):
if m.from_jid.bare == entity.bare:
for w in m.widgets:
w.update({"avatar": filename})
w.update({"avatar": avatar_data})
quick_widgets.register(QuickChat)

View file

@ -105,17 +105,11 @@ class ProfileContactList(object):
callback=self._showOfflineContacts,
)
# FIXME: workaround for a pyjamas issue: calling hash on a class method always
# return a different value if that method is defined directly within the
# class (with the "def" keyword)
self.presenceListener = self.onPresenceUpdate
self.host.addListener("presence", self.presenceListener, [self.profile])
self.nickListener = self.onNickUpdate
self.host.addListener("nick", self.nickListener, [self.profile])
self.notifListener = self.onNotification
self.host.addListener("notification", self.notifListener, [self.profile])
# notifListener only update the entity, so we can re-use it
self.host.addListener("notificationsClear", self.notifListener, [self.profile])
self.host.addListener("presence", self.onPresenceUpdate, [self.profile])
self.host.addListener("nicknames", self.onNicknamesUpdate, [self.profile])
self.host.addListener("notification", self.onNotification, [self.profile])
# onNotification only updates the entity, so we can re-use it
self.host.addListener("notificationsClear", self.onNotification, [self.profile])
@property
def whoami(self):
@ -164,7 +158,7 @@ class ProfileContactList(object):
[
entity
for entity in self._roster
if self.getCache(entity, C.PRESENCE_SHOW) is not None
if self.getCache(entity, C.PRESENCE_SHOW, default=None) is not None
]
)
@ -255,7 +249,9 @@ class ProfileContactList(object):
def fill(self):
handler.fill(self.profile)
def getCache(self, entity, name=None, bare_default=True, create_if_not_found=False):
def getCache(
self, entity, name=None, bare_default=True, create_if_not_found=False,
default=Exception):
"""Return a cache value for a contact
@param entity(jid.JID): entity of the contact from who we want data
@ -272,8 +268,12 @@ class ProfileContactList(object):
If None, bare_default will be set to False if entity is in a room, True else
@param create_if_not_found(bool): if True, create contact if it's not found
in cache
@param default(object): value to return when name is not found in cache
if Exception is used, a KeyError will be returned
otherwise, the given value will be used
@return: full cache if no name is given, or value of "name", or None
@raise NotFound: entity not found in cache
@raise KeyError: name not found in cache
"""
# FIXME: resource handling need to be reworked
# FIXME: bare_default work for requesting full jid to get bare jid,
@ -291,6 +291,10 @@ class ProfileContactList(object):
raise exceptions.NotFound
if name is None:
if default is not Exception:
raise exceptions.InternalError(
"default value can only Exception when name is not specified"
)
# full cache is requested
return cache
@ -313,22 +317,28 @@ class ProfileContactList(object):
elif entity.resource:
try:
return cache[C.CONTACT_RESOURCES][entity.resource][name]
except KeyError:
except KeyError as e:
if bare_default is None:
bare_default = not self.isRoom(entity.bare)
if not bare_default:
return None
if default is Exception:
raise e
else:
return default
try:
return cache[name]
except KeyError:
return None
except KeyError as e:
if default is Exception:
raise e
else:
return default
def setCache(self, entity, name, value):
"""Set or update value for one data in cache
@param entity(JID): entity to update
@param name(unicode): value to set or update
@param name(str): value to set or update
"""
self.setContact(entity, attributes={name: value})
@ -391,7 +401,7 @@ class ProfileContactList(object):
@param special_type: one of special type (e.g. C.CONTACT_SPECIAL_GROUP)
@return (bool): True if entity is from this special type
"""
return self.getCache(entity, C.CONTACT_SPECIAL) == special_type
return self.getCache(entity, C.CONTACT_SPECIAL, default=None) == special_type
def setSpecial(self, entity, special_type):
"""Set special flag on an entity
@ -417,7 +427,7 @@ class ProfileContactList(object):
continue
if (
special_type is not None
and self.getCache(entity, C.CONTACT_SPECIAL) != special_type
and self.getCache(entity, C.CONTACT_SPECIAL, default=None) != special_type
):
continue
yield entity
@ -443,7 +453,7 @@ class ProfileContactList(object):
"""Add a contact to the list if it doesn't exist, else update it.
This method can be called with groups=None for the purpose of updating
the contact's attributes (e.g. nickname). In that case, the groups
the contact's attributes (e.g. nicknames). In that case, the groups
attribute must not be set to the default group but ignored. If not,
you may move your contact from its actual group(s) to the default one.
@ -454,7 +464,6 @@ class ProfileContactList(object):
if entity is a full jid, attributes will be cached in for the full jid only
@param groups (list): list of groups or None to ignore the groups membership.
@param attributes (dict): attibutes of the added jid or to update
if attribute value is None, it will be removed
@param in_roster (bool): True if contact is from roster
"""
if attributes is None:
@ -506,8 +515,8 @@ class ProfileContactList(object):
else:
self._specials.add(entity)
cache[C.CONTACT_MAIN_RESOURCE] = None
if 'nick' in cache:
del cache['nick']
if 'nicknames' in cache:
del cache['nicknames']
# now the attributes we keep in cache
# XXX: if entity is a full jid, we store the value for the resource only
@ -517,23 +526,18 @@ class ProfileContactList(object):
else cache
)
for attribute, value in attributes.items():
if value is None:
# XXX: pyjamas hack: we need to use pop instead of del
try:
cache_attr[attribute].pop(value)
except KeyError:
pass
else:
if attribute == "nick" and self.isSpecial(
entity, C.CONTACT_SPECIAL_GROUP
):
# we don't want to keep nick for MUC rooms
# FIXME: this is here as plugin XEP-0054 can link resource's nick
# with bare jid which in the case of MUC
# set the nick for the whole MUC
# resulting in bad name displayed in some frontends
continue
cache_attr[attribute] = value
if attribute == "nicknames" and self.isSpecial(
entity, C.CONTACT_SPECIAL_GROUP
):
# we don't want to keep nicknames for MUC rooms
# FIXME: this is here as plugin XEP-0054 can link resource's nick
# with bare jid which in the case of MUC
# set the nick for the whole MUC
# resulting in bad name displayed in some frontends
# FIXME: with plugin XEP-0054 + plugin identity refactoring, this
# may not be needed anymore…
continue
cache_attr[attribute] = value
# we can update the display if needed
if self.entityVisible(entity_bare):
@ -554,7 +558,7 @@ class ProfileContactList(object):
"""
try:
show = self.getCache(entity, C.PRESENCE_SHOW)
except exceptions.NotFound:
except (exceptions.NotFound, KeyError):
return False
if check_resource:
@ -682,15 +686,15 @@ class ProfileContactList(object):
elif was_visible:
self.update([entity], C.UPDATE_DELETE, self.profile)
def onNickUpdate(self, entity, new_nick, profile):
"""Update entity's nick
def onNicknamesUpdate(self, entity, nicknames, profile):
"""Update entity's nicknames
@param entity(jid.JID): entity updated
@param new_nick(unicode): new nick of the entity
@param nicknames(list[unicode]): nicknames of the entity
@param profile: %(doc_profile)s
"""
assert profile == self.profile
self.setCache(entity, "nick", new_nick)
self.setCache(entity, "nicknames", nicknames)
def onNotification(self, entity, notif, profile):
"""Update entity with notification
@ -1093,6 +1097,8 @@ class QuickContactList(QuickWidget):
- C.UPDATE_MODIFY: entity updated
- C.UPDATE_ADD: entity added
- C.UPDATE_SELECTION: selection modified
- C.UPDATE_STRUCTURE: organisation of items is modified (not items
themselves)
or None for undefined update
Note that events correspond to addition, modification and deletion
of items on the whole contact list. If the contact is visible or not