diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index 009a07a5..50727ce2 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -440,20 +440,17 @@ class TelegramClient(TelegramBareClient): min_id=min_id, add_offset=add_offset)) - # The result may be a messages slice (not all messages were retrieved) or - # simply a messages TLObject. In the later case, no "count" attribute is specified: - # the total messages count is retrieved by counting all the retrieved messages + # The result may be a messages slice (not all messages were retrieved) + # or simply a messages TLObject. In the later case, no "count" + # attribute is specified, so the total messages count is simply + # the count of retrieved messages total_messages = getattr(result, 'count', len(result.messages)) # Iterate over all the messages and find the sender User - users = [] - for msg in result.messages: - for usr in result.users: - if msg.from_id == usr.id: - users.append(usr) - break + entities = [find_user_or_chat(msg.from_id, result.users, result.chats) + for msg in result.messages] - return total_messages, result.messages, users + return total_messages, result.messages, entities def send_read_acknowledge(self, entity, messages=None, max_id=None): """Sends a "read acknowledge" (i.e., notifying the given peer that we've diff --git a/telethon/utils.py b/telethon/utils.py index cacc60fd..7d11ef17 100644 --- a/telethon/utils.py +++ b/telethon/utils.py @@ -67,19 +67,22 @@ def find_user_or_chat(peer, users, chats): Returns None if it was not found""" try: if isinstance(peer, PeerUser): - user = next(u for u in users if u.id == peer.user_id) - return user + return next(u for u in users if u.id == peer.user_id) elif isinstance(peer, PeerChat): - chat = next(c for c in chats if c.id == peer.chat_id) - return chat + return next(c for c in chats if c.id == peer.chat_id) elif isinstance(peer, PeerChannel): - channel = next(c for c in chats if c.id == peer.channel_id) - return channel + return next(c for c in chats if c.id == peer.channel_id) - except StopIteration: - return None + except StopIteration: return + + if isinstance(peer, int): + try: return next(u for u in users if u.id == peer) + except StopIteration: pass + + try: return next(c for c in chats if c.id == peer) + except StopIteration: pass def get_appropriated_part_size(file_size):