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
3 changed files with 1111 additions and 747 deletions

View file

@ -379,7 +379,6 @@ class Const(object):
IGNORE = "ignore"
NO_LIMIT = -1 # used in bridge when a integer value is expected
DEFAULT_MAX_AGE = 1209600 # default max age of cached files, in seconds
HASH_SHA1_EMPTY = "da39a3ee5e6b4b0d3255bfef95601890afd80709"
STANZA_NAMES = ("iq", "message", "presence")
# Stream Hooks

View file

@ -102,12 +102,7 @@ class SAT(service.Service):
self.bridge.register_method("getFeatures", self.getFeatures)
self.bridge.register_method("profileNameGet", self.memory.getProfileName)
self.bridge.register_method("profilesListGet", self.memory.getProfilesList)
self.bridge.register_method(
"getEntityData",
lambda jid_, keys, profile: self.memory.getEntityData(
jid.JID(jid_), keys, profile
),
)
self.bridge.register_method("getEntityData", self.memory._getEntityData)
self.bridge.register_method("getEntitiesData", self.memory._getEntitiesData)
self.bridge.register_method("profileCreate", self.memory.createProfile)
self.bridge.register_method("asyncDeleteProfile", self.memory.asyncDeleteProfile)
@ -118,6 +113,7 @@ class SAT(service.Service):
self.bridge.register_method("profileSetDefault", self.memory.profileSetDefault)
self.bridge.register_method("connect", self._connect)
self.bridge.register_method("disconnect", self.disconnect)
self.bridge.register_method("contactGet", self._contactGet)
self.bridge.register_method("getContacts", self.getContacts)
self.bridge.register_method("getContactsFromGroup", self.getContactsFromGroup)
self.bridge.register_method("getMainResource", self.memory._getMainResource)
@ -539,6 +535,19 @@ class SAT(service.Service):
d_list.addCallback(buildFeatures, list(self.plugins.keys()))
return d_list
def _contactGet(self, entity_jid_s, profile_key):
client = self.getClient(profile_key)
entity_jid = jid.JID(entity_jid_s)
return defer.ensureDeferred(self.getContact(client, entity_jid))
async def getContact(self, client, entity_jid):
# we want to be sure that roster has been received
await client.roster.got_roster
item = client.roster.getItem(entity_jid)
if item is None:
raise exceptions.NotFound(f"{entity_jid} is not in roster!")
return (client.roster.getAttributes(item), list(item.groups))
def getContacts(self, profile_key):
client = self.getClient(profile_key)
@ -708,7 +717,7 @@ class SAT(service.Service):
for resource in resources:
res_jid = copy.copy(bare_jid)
res_jid.resource = resource
cache_data = self.memory.getEntityData(res_jid, profile_key=client.profile)
cache_data = self.memory.getEntityData(client, res_jid)
res_data = {
"resource": resource,
}
@ -957,17 +966,17 @@ class SAT(service.Service):
self.profiles[profile].presence.subscribe(to_jid)
def _updateContact(self, to_jid_s, name, groups, profile_key):
return self.updateContact(jid.JID(to_jid_s), name, groups, profile_key)
client = self.getClient(profile_key)
return self.updateContact(client, jid.JID(to_jid_s), name, groups)
def updateContact(self, to_jid, name, groups, profile_key):
def updateContact(self, client, to_jid, name, groups):
"""update a contact in roster list"""
profile = self.memory.getProfileName(profile_key)
assert profile
groups = set(groups)
roster_item = RosterItem(to_jid)
roster_item.name = name or None
roster_item.name = name or u''
roster_item.groups = set(groups)
return self.profiles[profile].roster.setItem(roster_item)
if not self.trigger.point("roster_update", client, roster_item):
return
return client.roster.setItem(roster_item)
def _delContact(self, to_jid_s, profile_key):
return self.delContact(jid.JID(to_jid_s), profile_key)

View file

@ -650,7 +650,7 @@ class SatXMPPEntity:
# we may have a groupchat message, we check if the we know this jid
try:
entity_type = self.host_app.memory.getEntityDatum(
data["to"], C.ENTITY_TYPE, self.profile
self, data["to"], C.ENTITY_TYPE
)
# FIXME: should entity_type manage resources ?
except (exceptions.UnknownEntityError, KeyError):
@ -1551,15 +1551,21 @@ class SatRosterProtocol(xmppim.RosterClientProtocol):
class SatPresenceProtocol(xmppim.PresenceClientProtocol):
def __init__(self, host):
xmppim.PresenceClientProtocol.__init__(self)
self.host = host
@property
def client(self):
return self.parent
def send(self, obj):
presence_d = defer.succeed(None)
if not self.host.trigger.point("Presence send", self.parent, obj, presence_d):
return
presence_d.addCallback(lambda __: super(SatPresenceProtocol, self).send(obj))
return presence_d
def availableReceived(self, entity, show=None, statuses=None, priority=0):
if not statuses:
@ -1604,7 +1610,7 @@ class SatPresenceProtocol(xmppim.PresenceClientProtocol):
# there is no need to send an unavailable signal
try:
presence = self.host.memory.getEntityDatum(
entity, "presence", self.parent.profile
self.client, entity, "presence"
)
except (KeyError, exceptions.UnknownEntityError):
# the entity has not been seen yet in this session
@ -1657,7 +1663,7 @@ class SatPresenceProtocol(xmppim.PresenceClientProtocol):
if not self.host.trigger.point("presence_available", presence_elt, self.parent):
return
self.send(presence_elt)
return self.send(presence_elt)
@defer.inlineCallbacks
def subscribed(self, entity):