From 86429e7291a79cea8bdda27fa1f5b64860b1ba69 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 27 Dec 2017 11:54:08 +0100 Subject: [PATCH] Lowercase usernames before adding them to the database --- telethon/tl/session.py | 13 ++++++------- telethon/utils.py | 9 ++++++--- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/telethon/tl/session.py b/telethon/tl/session.py index 12bc3937..e3dea190 100644 --- a/telethon/tl/session.py +++ b/telethon/tl/session.py @@ -330,7 +330,7 @@ class Session: p_hash = p.access_hash if p_hash is not None: - username = getattr(e, 'username', None) + username = getattr(e, 'username', '').lower() or None phone = getattr(e, 'phone', None) name = utils.get_display_name(e) or None rows.append((marked_id, p_hash, username, phone, name)) @@ -357,6 +357,11 @@ class Session: Raises ValueError if it cannot be found. """ c = self._conn.cursor() + if isinstance(key, TLObject): + if type(key).SUBCLASS_OF_ID == 0xc91c90b6: # crc32(b'InputPeer') + return key + key = utils.get_peer_id(key, add_mark=True) + if isinstance(key, str): phone = utils.parse_phone(key) if phone: @@ -367,12 +372,6 @@ class Session: c.execute('select id, hash from entities where username=?', (username,)) - if isinstance(key, TLObject): - # crc32(b'InputPeer') and crc32(b'Peer') - if type(key).SUBCLASS_OF_ID == 0xc91c90b6: - return key - key = utils.get_peer_id(key, add_mark=True) - if isinstance(key, int): c.execute('select id, hash from entities where id=?', (key,)) diff --git a/telethon/utils.py b/telethon/utils.py index 04970632..0662a99d 100644 --- a/telethon/utils.py +++ b/telethon/utils.py @@ -325,14 +325,17 @@ def parse_phone(phone): def parse_username(username): """Parses the given username or channel access hash, given a string, username or URL. Returns a tuple consisting of - both the stripped username and whether it is a joinchat/ hash. + both the stripped, lowercase username and whether it is + a joinchat/ hash (in which case is not lowercase'd). """ username = username.strip() m = USERNAME_RE.match(username) if m: - return username[m.end():], bool(m.group(1)) + result = username[m.end():] + is_invite = bool(m.group(1)) + return result if is_invite else result.lower(), is_invite else: - return username, False + return username.lower(), False def get_peer_id(peer, add_mark=False):