Skip to content

Commit

Permalink
Fix InputPeer* with None hash, drop them off database (closes #354)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonami committed Oct 30, 2017
1 parent 05626c8 commit 05f7f07
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
20 changes: 17 additions & 3 deletions telethon/tl/entity_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ def __init__(self, input_list=None, enabled=True, enabled_full=True):
self._entities = {} # marked_id: user|chat|channel

if input_list:
self._input_entities = {k: v for k, v in input_list}
# TODO For compatibility reasons some sessions were saved with
# 'access_hash': null in the JSON session file. Drop these, as
# it means we don't have access to such InputPeers. Issue #354.
self._input_entities = {
k: v for k, v in input_list if v is not None
}
else:
self._input_entities = {} # marked_id: hash

Expand Down Expand Up @@ -69,8 +74,17 @@ def expand(self, entities):

try:
p = utils.get_input_peer(e, allow_self=False)
new_input[utils.get_peer_id(p, add_mark=True)] = \
getattr(p, 'access_hash', 0) # chats won't have hash
marked_id = utils.get_peer_id(p, add_mark=True)

if isinstance(p, InputPeerChat):
# Chats don't have a hash
new_input[marked_id] = 0
elif p.access_hash:
# Some users and channels seem to be returned without
# an 'access_hash', meaning Telegram doesn't want you
# to access them. This is the reason behind ensuring
# that the 'access_hash' is non-zero. See issue #354.
new_input[marked_id] = p.access_hash

if self.enabled_full:
if isinstance(e, (User, Chat, Channel)):
Expand Down
8 changes: 4 additions & 4 deletions telethon/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ def get_input_peer(entity, allow_self=True):
if entity.is_self and allow_self:
return InputPeerSelf()
else:
return InputPeerUser(entity.id, entity.access_hash)
return InputPeerUser(entity.id, entity.access_hash or 0)

if isinstance(entity, (Chat, ChatEmpty, ChatForbidden)):
return InputPeerChat(entity.id)

if isinstance(entity, (Channel, ChannelForbidden)):
return InputPeerChannel(entity.id, entity.access_hash)
return InputPeerChannel(entity.id, entity.access_hash or 0)

# Less common cases
if isinstance(entity, UserEmpty):
Expand Down Expand Up @@ -120,7 +120,7 @@ def get_input_channel(entity):
return entity

if isinstance(entity, (Channel, ChannelForbidden)):
return InputChannel(entity.id, entity.access_hash)
return InputChannel(entity.id, entity.access_hash or 0)

if isinstance(entity, InputPeerChannel):
return InputChannel(entity.channel_id, entity.access_hash)
Expand All @@ -140,7 +140,7 @@ def get_input_user(entity):
if entity.is_self:
return InputUserSelf()
else:
return InputUser(entity.id, entity.access_hash)
return InputUser(entity.id, entity.access_hash or 0)

if isinstance(entity, InputPeerSelf):
return InputUserSelf()
Expand Down

0 comments on commit 05f7f07

Please sign in to comment.