comparison sat/plugins/plugin_misc_identity.py @ 3338:203a491fcd86

plugin identity: new methods `identitiesGet` and `identitiesBaseGet` `identitiesGet` retrieve several identities at once, in parallel. `identitiesBaseGet` retrieve essential identities (roster + own)
author Goffi <goffi@goffi.org>
date Thu, 13 Aug 2020 23:46:18 +0200
parents 9e1ba1e1179f
children be6d91572633
comparison
equal deleted inserted replaced
3337:932cf08dfdc3 3338:203a491fcd86
13 # GNU Affero General Public License for more details. 13 # GNU Affero General Public License for more details.
14 14
15 # You should have received a copy of the GNU Affero General Public License 15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>. 16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 from typing import Dict, Union, Coroutine, Any, Optional 18 from typing import Dict, List, Union, Coroutine, Any, Optional
19 from collections import namedtuple 19 from collections import namedtuple
20 from pathlib import Path 20 from pathlib import Path
21 from twisted.internet import defer 21 from twisted.internet import defer
22 from twisted.words.protocols.jabber import jid 22 from twisted.words.protocols.jabber import jid
23 from sat.core.xmpp import SatXMPPEntity 23 from sat.core.xmpp import SatXMPPEntity
92 out_sign="s", 92 out_sign="s",
93 method=self._getIdentity, 93 method=self._getIdentity,
94 async_=True, 94 async_=True,
95 ) 95 )
96 host.bridge.addMethod( 96 host.bridge.addMethod(
97 "identitiesGet",
98 ".plugin",
99 in_sign="asass",
100 out_sign="s",
101 method=self._getIdentities,
102 async_=True,
103 )
104 host.bridge.addMethod(
105 "identitiesBaseGet",
106 ".plugin",
107 in_sign="s",
108 out_sign="s",
109 method=self._getBaseIdentities,
110 async_=True,
111 )
112 host.bridge.addMethod(
97 "identitySet", 113 "identitySet",
98 ".plugin", 114 ".plugin",
99 in_sign="ss", 115 in_sign="ss",
100 out_sign="", 116 out_sign="",
101 method=self._setIdentity, 117 method=self._setIdentity,
176 f"{roster_item.jid}" 192 f"{roster_item.jid}"
177 ) 193 )
178 defer.ensureDeferred( 194 defer.ensureDeferred(
179 self.update( 195 self.update(
180 client, 196 client,
197 IMPORT_NAME,
181 "nicknames", 198 "nicknames",
182 [roster_item.name], 199 [roster_item.name],
183 roster_item.jid 200 roster_item.jid
184 ) 201 )
185 ) 202 )
559 def _getIdentity(self, entity_s, metadata_filter, use_cache, profile): 576 def _getIdentity(self, entity_s, metadata_filter, use_cache, profile):
560 entity = jid.JID(entity_s) 577 entity = jid.JID(entity_s)
561 client = self.host.getClient(profile) 578 client = self.host.getClient(profile)
562 d = defer.ensureDeferred( 579 d = defer.ensureDeferred(
563 self.getIdentity(client, entity, metadata_filter, use_cache)) 580 self.getIdentity(client, entity, metadata_filter, use_cache))
564 d.addCallback(lambda data: data_format.serialise(data)) 581 d.addCallback(data_format.serialise)
565 return d 582 return d
566 583
567 async def getIdentity( 584 async def getIdentity(
568 self, client, entity=None, metadata_filter=None, use_cache=True): 585 self, client, entity=None, metadata_filter=None, use_cache=True):
569 """Retrieve identity of an entity 586 """Retrieve identity of an entity
585 for metadata_name in metadata_names: 602 for metadata_name in metadata_names:
586 id_data[metadata_name] = await self.get( 603 id_data[metadata_name] = await self.get(
587 client, metadata_name, entity, use_cache) 604 client, metadata_name, entity, use_cache)
588 605
589 return id_data 606 return id_data
607
608 def _getIdentities(self, entities_s, metadata_filter, profile):
609 entities = [jid.JID(e) for e in entities_s]
610 client = self.host.getClient(profile)
611 d = defer.ensureDeferred(self.getIdentities(client, entities, metadata_filter))
612 d.addCallback(lambda d: data_format.serialise({str(j):i for j, i in d.items()}))
613 return d
614
615 async def getIdentities(
616 self,
617 client: SatXMPPEntity,
618 entities: List[jid.JID],
619 metadata_filter: Optional[List[str]] = None,
620 ) -> dict:
621 """Retrieve several identities at once
622
623 @param entities: entities from which identities must be retrieved
624 @param metadata_filter: same as for [getIdentity]
625 @return: identities metadata where key is jid
626 if an error happens while retrieve a jid entity, it won't be present in the
627 result (and a warning will be logged)
628 """
629 identities = {}
630 get_identity_list = []
631 for entity_jid in entities:
632 get_identity_list.append(
633 defer.ensureDeferred(
634 self.getIdentity(
635 client,
636 entity=entity_jid,
637 metadata_filter=metadata_filter,
638 )
639 )
640 )
641 identities_result = await defer.DeferredList(get_identity_list)
642 for idx, (success, identity) in enumerate(identities_result):
643 entity_jid = entities[idx]
644 if not success:
645 log.warning(f"Can't get identity for {entity_jid}")
646 else:
647 identities[entity_jid] = identity
648 return identities
649
650 def _getBaseIdentities(self, profile_key):
651 client = self.host.getClient(profile_key)
652 d = defer.ensureDeferred(self.getBaseIdentities(client))
653 d.addCallback(lambda d: data_format.serialise({str(j):i for j, i in d.items()}))
654 return d
655
656 async def getBaseIdentities(
657 self,
658 client: SatXMPPEntity,
659 ) -> dict:
660 """Retrieve identities for entities in roster + own identity + invitations
661
662 @param with_guests: if True, get affiliations of people invited by email
663
664 """
665 entities = client.roster.getJids() + [client.jid.userhostJID()]
666
667 return await self.getIdentities(
668 client,
669 entities,
670 ['avatar', 'nicknames']
671 )
590 672
591 def _setIdentity(self, id_data_s, profile): 673 def _setIdentity(self, id_data_s, profile):
592 client = self.host.getClient(profile) 674 client = self.host.getClient(profile)
593 id_data = data_format.deserialise(id_data_s) 675 id_data = data_format.deserialise(id_data_s)
594 return defer.ensureDeferred(self.setIdentity(client, id_data)) 676 return defer.ensureDeferred(self.setIdentity(client, id_data))