diff sat/core/sat_main.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 b0c57c9a4bd8
children f300d78f08f3
line wrap: on
line diff
--- a/sat/core/sat_main.py	Tue Apr 14 20:36:24 2020 +0200
+++ b/sat/core/sat_main.py	Tue Apr 14 21:00:33 2020 +0200
@@ -102,12 +102,7 @@
         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 @@
         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 @@
         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 @@
         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 @@
         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)