plugin identity: make the plugin compatible with component + description:
the plugin can now be used with components. the new `description` field is now available. rel 368
This commit is contained in:
parent
35be6b741f
commit
fe11bc9a2b
1 changed files with 58 additions and 20 deletions
|
|
@ -54,6 +54,7 @@ PLUGIN_INFO = {
|
||||||
C.PI_NAME: "Identity Plugin",
|
C.PI_NAME: "Identity Plugin",
|
||||||
C.PI_IMPORT_NAME: IMPORT_NAME,
|
C.PI_IMPORT_NAME: IMPORT_NAME,
|
||||||
C.PI_TYPE: C.PLUG_TYPE_MISC,
|
C.PI_TYPE: C.PLUG_TYPE_MISC,
|
||||||
|
C.PI_MODES: C.PLUG_MODE_BOTH,
|
||||||
C.PI_PROTOCOLS: [],
|
C.PI_PROTOCOLS: [],
|
||||||
C.PI_DEPENDENCIES: [],
|
C.PI_DEPENDENCIES: [],
|
||||||
C.PI_RECOMMENDATIONS: ["XEP-0045"],
|
C.PI_RECOMMENDATIONS: ["XEP-0045"],
|
||||||
|
|
@ -95,6 +96,12 @@ class Identity:
|
||||||
"update_is_new_data": self.nicknamesUpdateIsNewData,
|
"update_is_new_data": self.nicknamesUpdateIsNewData,
|
||||||
"store": True,
|
"store": True,
|
||||||
},
|
},
|
||||||
|
"description": {
|
||||||
|
"type": str,
|
||||||
|
"get_all": True,
|
||||||
|
"get_post_treatment": self.descriptionGetPostTreatment,
|
||||||
|
"store": True,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
host.trigger.add("roster_update", self._rosterUpdateTrigger)
|
host.trigger.add("roster_update", self._rosterUpdateTrigger)
|
||||||
host.memory.setSignalOnUpdate("avatar")
|
host.memory.setSignalOnUpdate("avatar")
|
||||||
|
|
@ -263,6 +270,14 @@ class Identity:
|
||||||
f"{value} has wrong type: it is {type(value)} while {value_type} was "
|
f"{value} has wrong type: it is {type(value)} while {value_type} was "
|
||||||
f"expected")
|
f"expected")
|
||||||
|
|
||||||
|
def getFieldType(self, metadata_name: str) -> str:
|
||||||
|
"""Return the type the requested field
|
||||||
|
|
||||||
|
@param metadata_name: name of the field to check
|
||||||
|
@raise KeyError: the request field doesn't exist
|
||||||
|
"""
|
||||||
|
return self.metadata[metadata_name]["type"]
|
||||||
|
|
||||||
async def get(
|
async def get(
|
||||||
self,
|
self,
|
||||||
client: SatXMPPEntity,
|
client: SatXMPPEntity,
|
||||||
|
|
@ -330,7 +345,10 @@ class Identity:
|
||||||
if data:
|
if data:
|
||||||
self.checkType(metadata_name, data)
|
self.checkType(metadata_name, data)
|
||||||
if get_all:
|
if get_all:
|
||||||
all_data.extend(data)
|
if isinstance(data, list):
|
||||||
|
all_data.extend(data)
|
||||||
|
else:
|
||||||
|
all_data.append(data)
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
|
@ -396,13 +414,13 @@ class Identity:
|
||||||
await utils.asDeferred(post_treatment, client, entity, data)
|
await utils.asDeferred(post_treatment, client, entity, data)
|
||||||
|
|
||||||
async def update(
|
async def update(
|
||||||
self,
|
self,
|
||||||
client: SatXMPPEntity,
|
client: SatXMPPEntity,
|
||||||
origin: str,
|
origin: str,
|
||||||
metadata_name: str,
|
metadata_name: str,
|
||||||
data: Any,
|
data: Any,
|
||||||
entity: Optional[jid.JID]
|
entity: Optional[jid.JID]
|
||||||
):
|
):
|
||||||
"""Update a metadata in cache
|
"""Update a metadata in cache
|
||||||
|
|
||||||
This method may be called by plugins when an identity metadata is available.
|
This method may be called by plugins when an identity metadata is available.
|
||||||
|
|
@ -626,17 +644,20 @@ class Identity:
|
||||||
if there is no user part.
|
if there is no user part.
|
||||||
For MUC, room nick is always put first
|
For MUC, room nick is always put first
|
||||||
"""
|
"""
|
||||||
# we first check roster
|
|
||||||
nicknames = []
|
nicknames = []
|
||||||
|
|
||||||
|
# for MUC we add resource
|
||||||
if entity.resource:
|
if entity.resource:
|
||||||
# getIdentityJid let the resource only if the entity is a MUC room
|
# getIdentityJid let the resource only if the entity is a MUC room
|
||||||
# occupant jid
|
# occupant jid
|
||||||
nicknames.append(entity.resource)
|
nicknames.append(entity.resource)
|
||||||
|
|
||||||
roster_item = client.roster.getItem(entity.userhostJID())
|
# we first check roster (if we are not in a component)
|
||||||
if roster_item is not None and roster_item.name:
|
if not client.is_component:
|
||||||
# user set name has priority over entity set name
|
roster_item = client.roster.getItem(entity.userhostJID())
|
||||||
nicknames.append(roster_item.name)
|
if roster_item is not None and roster_item.name:
|
||||||
|
# user set name has priority over entity set name
|
||||||
|
nicknames.append(roster_item.name)
|
||||||
|
|
||||||
nicknames.extend(plugin_nicknames)
|
nicknames.extend(plugin_nicknames)
|
||||||
|
|
||||||
|
|
@ -652,6 +673,15 @@ class Identity:
|
||||||
def nicknamesUpdateIsNewData(self, client, entity, cached_data, new_nicknames):
|
def nicknamesUpdateIsNewData(self, client, entity, cached_data, new_nicknames):
|
||||||
return not set(new_nicknames).issubset(cached_data)
|
return not set(new_nicknames).issubset(cached_data)
|
||||||
|
|
||||||
|
async def descriptionGetPostTreatment(
|
||||||
|
self,
|
||||||
|
client: SatXMPPEntity,
|
||||||
|
entity: jid.JID,
|
||||||
|
plugin_description: List[str]
|
||||||
|
) -> str:
|
||||||
|
"""Join all descriptions in a unique string"""
|
||||||
|
return '\n'.join(plugin_description)
|
||||||
|
|
||||||
def _getIdentity(self, entity_s, metadata_filter, use_cache, profile):
|
def _getIdentity(self, entity_s, metadata_filter, use_cache, profile):
|
||||||
entity = jid.JID(entity_s)
|
entity = jid.JID(entity_s)
|
||||||
client = self.host.getClient(profile)
|
client = self.host.getClient(profile)
|
||||||
|
|
@ -661,15 +691,20 @@ class Identity:
|
||||||
return d
|
return d
|
||||||
|
|
||||||
async def getIdentity(
|
async def getIdentity(
|
||||||
self, client, entity=None, metadata_filter=None, use_cache=True):
|
self,
|
||||||
|
client: SatXMPPEntity,
|
||||||
|
entity: Optional[jid.JID] = None,
|
||||||
|
metadata_filter: Optional[List[str]] = None,
|
||||||
|
use_cache: bool = True
|
||||||
|
) -> Dict[str, Any]:
|
||||||
"""Retrieve identity of an entity
|
"""Retrieve identity of an entity
|
||||||
|
|
||||||
@param entity(jid.JID, None): entity to check
|
@param entity: entity to check
|
||||||
@param metadata_filter(list[str], None): if not None or empty, only return
|
@param metadata_filter: if not None or empty, only return
|
||||||
metadata in this filter
|
metadata in this filter
|
||||||
@param use_cache(bool): if False, cache won't be checked
|
@param use_cache: if False, cache won't be checked
|
||||||
should be True most of time, to avoid useless network requests
|
should be True most of time, to avoid useless network requests
|
||||||
@return (dict): identity data
|
@return: identity data
|
||||||
"""
|
"""
|
||||||
id_data = {}
|
id_data = {}
|
||||||
|
|
||||||
|
|
@ -741,7 +776,10 @@ class Identity:
|
||||||
@param with_guests: if True, get affiliations of people invited by email
|
@param with_guests: if True, get affiliations of people invited by email
|
||||||
|
|
||||||
"""
|
"""
|
||||||
entities = client.roster.getJids() + [client.jid.userhostJID()]
|
if client.is_component:
|
||||||
|
entities = [client.jid.userhostJID()]
|
||||||
|
else:
|
||||||
|
entities = client.roster.getJids() + [client.jid.userhostJID()]
|
||||||
|
|
||||||
return await self.getIdentities(
|
return await self.getIdentities(
|
||||||
client,
|
client,
|
||||||
|
|
@ -757,7 +795,7 @@ class Identity:
|
||||||
async def setIdentity(self, client, id_data):
|
async def setIdentity(self, client, id_data):
|
||||||
"""Update profile's identity
|
"""Update profile's identity
|
||||||
|
|
||||||
@param id_data(dict): data to update, key can be on of self.metadata keys
|
@param id_data(dict): data to update, key can be one of self.metadata keys
|
||||||
"""
|
"""
|
||||||
if not id_data.keys() <= self.metadata.keys():
|
if not id_data.keys() <= self.metadata.keys():
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue