diff sat_frontends/quick_frontend/quick_app.py @ 3254:6cf4bd6972c2

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`
author Goffi <goffi@goffi.org>
date Tue, 14 Apr 2020 21:00:33 +0200
parents f3c99e96ac03
children be6d91572633
line wrap: on
line diff
--- a/sat_frontends/quick_frontend/quick_app.py	Tue Apr 14 20:36:24 2020 +0200
+++ b/sat_frontends/quick_frontend/quick_app.py	Tue Apr 14 21:00:33 2020 +0200
@@ -44,7 +44,7 @@
     #       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 @@
 
     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 @@
         )
 
     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 @@
                 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 @@
             - 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 @@
         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 @@
             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)