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

@ -649,7 +649,7 @@ class Memory(object):
full_jid = copy.copy(entity_jid)
full_jid.resource = resource
try:
presence_data = self.getEntityDatum(full_jid, "presence", profile_key)
presence_data = self.getEntityDatum(client, full_jid, "presence")
except KeyError:
continue
entities_presence.setdefault(entity_jid, {})[
@ -667,14 +667,15 @@ class Memory(object):
@param statuses: dictionary of statuses
@param profile_key: %(doc_profile_key)s
"""
client = self.host.getClient(profile_key)
presence_data = PresenceTuple(show, priority, statuses)
self.updateEntityData(
entity_jid, "presence", presence_data, profile_key=profile_key
client, entity_jid, "presence", presence_data
)
if entity_jid.resource and show != C.PRESENCE_UNAVAILABLE:
# If a resource is available, bare jid should not have presence information
try:
self.delEntityDatum(entity_jid.userhostJID(), "presence", profile_key)
self.delEntityDatum(client, entity_jid.userhostJID(), "presence")
except (KeyError, exceptions.UnknownEntityError):
pass
@ -724,7 +725,7 @@ class Memory(object):
full_jid = copy.copy(entity_jid)
full_jid.resource = resource
try:
presence_data = self.getEntityDatum(full_jid, "presence", client.profile)
presence_data = self.getEntityDatum(client, full_jid, "presence")
except KeyError:
log.debug("Can't get presence data for {}".format(full_jid))
else:
@ -762,7 +763,7 @@ class Memory(object):
full_jid = copy.copy(entity_jid)
full_jid.resource = resource
try:
presence_data = self.getEntityDatum(full_jid, "presence", client.profile)
presence_data = self.getEntityDatum(client, full_jid, "presence")
except KeyError:
log.debug("No presence information for {}".format(full_jid))
continue
@ -812,7 +813,7 @@ class Memory(object):
yield full_jid
def updateEntityData(
self, entity_jid, key, value, silent=False, profile_key=C.PROF_KEY_NONE
self, client, entity_jid, key, value, silent=False
):
"""Set a misc data for an entity
@ -823,9 +824,7 @@ class Memory(object):
@param value: value for this key (eg: C.ENTITY_TYPE_MUC)
@param silent(bool): if True, doesn't send signal to frontend, even if there is a
signal flag (see setSignalOnUpdate)
@param profile_key: %(doc_profile_key)s
"""
client = self.host.getClient(profile_key)
profile_cache = self._getProfileCache(client)
if entity_jid in (C.ENTITY_ALL_RESOURCES, C.ENTITY_ALL):
entities = self.getAllEntitiesIter(client, entity_jid == C.ENTITY_ALL)
@ -839,29 +838,23 @@ class Memory(object):
entity_data[key] = value
if key in self._key_signals and not silent:
if not isinstance(value, str):
log.error(
"Setting a non string value ({}) for a key ({}) which has a signal flag".format(
value, key
)
)
else:
self.host.bridge.entityDataUpdated(
jid_.full(), key, value, self.getProfileName(profile_key)
)
self.host.bridge.entityDataUpdated(
jid_.full(),
key,
data_format.serialise(value),
client.profile
)
def delEntityDatum(self, entity_jid, key, profile_key):
def delEntityDatum(self, client, entity_jid, key):
"""Delete a data for an entity
@param entity_jid: JID of the entity, C.ENTITY_ALL_RESOURCES for all resources of all entities,
C.ENTITY_ALL for all entities (all resources + bare jids)
@param key: key to delete (eg: C.ENTITY_TYPE)
@param profile_key: %(doc_profile_key)s
@raise exceptions.UnknownEntityError: if entity is not in cache
@raise KeyError: key is not in cache
"""
client = self.host.getClient(profile_key)
profile_cache = self._getProfileCache(client)
if entity_jid in (C.ENTITY_ALL_RESOURCES, C.ENTITY_ALL):
entities = self.getAllEntitiesIter(client, entity_jid == C.ENTITY_ALL)
@ -884,12 +877,16 @@ class Memory(object):
raise e
def _getEntitiesData(self, entities_jids, keys_list, profile_key):
client = self.host.getClient(profile_key)
ret = self.getEntitiesData(
[jid.JID(jid_) for jid_ in entities_jids], keys_list, profile_key
client, [jid.JID(jid_) for jid_ in entities_jids], keys_list
)
return {jid_.full(): data for jid_, data in ret.items()}
return {
jid_.full(): {k: data_format.serialise(v) for k,v in data.items()}
for jid_, data in ret.items()
}
def getEntitiesData(self, entities_jids, keys_list=None, profile_key=C.PROF_KEY_NONE):
def getEntitiesData(self, client, entities_jids, keys_list=None):
"""Get a list of cached values for several entities at once
@param entities_jids: jids of the entities, or empty list for all entities in cache
@ -916,7 +913,6 @@ class Memory(object):
continue
return entity_data
client = self.host.getClient(profile_key)
profile_cache = self._getProfileCache(client)
ret_data = {}
if entities_jids:
@ -937,7 +933,11 @@ class Memory(object):
return ret_data
def getEntityData(self, entity_jid, keys_list=None, profile_key=C.PROF_KEY_NONE):
def _getEntityData(self, entity_jid_s, keys_list=None, profile=C.PROF_KEY_NONE):
return self.getEntityData(
self.host.getClient(profile), jid.JID(entity_jid_s), keys_list)
def getEntityData(self, client, entity_jid, keys_list=None):
"""Get a list of cached values for entity
@param entity_jid: JID of the entity
@ -949,7 +949,6 @@ class Memory(object):
@raise exceptions.UnknownEntityError: if entity is not in cache
"""
client = self.host.getClient(profile_key)
profile_cache = self._getProfileCache(client)
try:
entity_data = profile_cache[entity_jid.userhostJID()][entity_jid.resource]
@ -964,18 +963,17 @@ class Memory(object):
return {key: entity_data[key] for key in keys_list if key in entity_data}
def getEntityDatum(self, entity_jid, key, profile_key):
def getEntityDatum(self, client, entity_jid, key):
"""Get a datum from entity
@param entity_jid: JID of the entity
@param keys: key to get
@param profile_key: %(doc_profile_key)s
@param key: key to get
@return: requested value
@raise exceptions.UnknownEntityError: if entity is not in cache
@raise KeyError: if there is no value for this key and this entity
"""
return self.getEntityData(entity_jid, (key,), profile_key)[key]
return self.getEntityData(client, entity_jid, (key,))[key]
def delEntityCache(
self, entity_jid, delete_all_resources=True, profile_key=C.PROF_KEY_NONE
@ -1595,7 +1593,7 @@ class Memory(object):
self.getAvailableResources(client, entity_jid)
) # is any resource is available, entity is available
try:
presence_data = self.getEntityDatum(entity_jid, "presence", client.profile)
presence_data = self.getEntityDatum(client, entity_jid, "presence")
except KeyError:
log.debug("No presence information for {}".format(entity_jid))
return False