diff sat/memory/disco.py @ 3718:16e36f0dd1cb

memory (disco), core (main): new `hasIdentity` method
author Goffi <goffi@goffi.org>
date Tue, 25 Jan 2022 17:07:15 +0100
parents be6d91572633
children 193b56ef58e0
line wrap: on
line diff
--- a/sat/memory/disco.py	Tue Jan 25 17:06:28 2022 +0100
+++ b/sat/memory/disco.py	Tue Jan 25 17:07:15 2022 +0100
@@ -17,11 +17,12 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+from typing import Optional
 from sat.core.i18n import _
 from sat.core import exceptions
 from sat.core.log import getLogger
+from sat.core.core_types import SatXMPPEntity
 
-log = getLogger(__name__)
 from twisted.words.protocols.jabber import jid
 from twisted.words.protocols.jabber.error import StanzaError
 from twisted.internet import defer
@@ -35,6 +36,9 @@
 from hashlib import sha1
 
 
+log = getLogger(__name__)
+
+
 TIMEOUT = 15
 CAP_HASH_ERROR = "ERROR"
 
@@ -162,6 +166,25 @@
         if identity is not None and identity not in disco_infos.identities:
             raise failure.Failure(exceptions.FeatureNotFound())
 
+    async def hasIdentity(
+        self,
+        client: SatXMPPEntity,
+        category: str,
+        type_: str,
+        jid_: Optional[jid.JID] = None,
+        node: str = ""
+    ) -> bool:
+        """Tell if an entity has the requested identity
+
+        @param category: identity category
+        @param type_: identity type
+        @param jid_: jid of the target, or None for profile's server
+        @param node(unicode): optional node to use for disco request
+        @return: True if the entity has the given identity
+        """
+        disco_infos = await self.getInfos(client, jid_, node)
+        return (category, type_) in disco_infos.identities
+
     def getInfos(self, client, jid_=None, node="", use_cache=True):
         """get disco infos from jid_, filling capability hash if needed