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

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)