Fix updating user info from entities attached to updates

Also made it trust info from users who don't have the puppet's phone number.
This commit is contained in:
Tulir Asokan
2019-05-15 20:05:25 +03:00
parent 8689d0e8b0
commit ce92cd31bf
2 changed files with 18 additions and 8 deletions
+1 -1
View File
@@ -281,7 +281,7 @@ class AbstractUser(ABC):
async def _handle_entity_updates(self, entities: Dict[int, Union[User, Chat, Channel]]) -> None:
try:
users = (entity for entity in entities.items() if isinstance(entity, User))
users = (entity for entity in entities.values() if isinstance(entity, User))
puppets = ((pu.Puppet.get(TelegramID(user.id)), user) for user in users)
await asyncio.gather(*[puppet.update_info(self, info)
for puppet, info in puppets if puppet])
+17 -7
View File
@@ -359,19 +359,25 @@ class Puppet:
) -> bool:
if self.disable_updates:
return False
ignore_source = (not source.is_relaybot
and self.displayname_source is not None
and self.displayname_source != source.tgid)
if ignore_source:
is_main_source = (source.is_relaybot or (self.displayname_source is not None
and self.displayname_source != source.tgid))
# No phone -> not in contact list -> can't set custom name -> name is trustworthy
is_trustworthy_source = isinstance(info, User) and info.phone is None
if not is_main_source and not is_trustworthy_source:
return False
if isinstance(info, UpdateUserName):
elif isinstance(info, UpdateUserName):
info = await source.client.get_entity(PeerUser(self.tgid))
displayname = self.get_displayname(info)
if displayname != self.displayname:
await self.default_mxid_intent.set_display_name(displayname)
self.displayname = displayname
self.displayname_source = source.tgid
try:
await self.default_mxid_intent.set_display_name(displayname)
except MatrixRequestError:
self.log.exception("Failed to set displayname")
self.displayname = ""
self.displayname_source = None
return True
elif source.is_relaybot or self.displayname_source is None:
self.displayname_source = source.tgid
@@ -386,8 +392,12 @@ class Puppet:
file = await util.transfer_file_to_matrix(source.client, self.default_mxid_intent,
photo)
if file:
await self.default_mxid_intent.set_avatar(file.mxc)
self.photo_id = photo_id
try:
await self.default_mxid_intent.set_avatar(file.mxc)
except MatrixRequestError:
self.log.exception("Failed to set avatar")
self.photo_id = ""
return True
return False