diff --git a/telethon/network/mtproto_plain_sender.py b/telethon/network/mtproto_plain_sender.py index 7fae3c20..bbb3e297 100644 --- a/telethon/network/mtproto_plain_sender.py +++ b/telethon/network/mtproto_plain_sender.py @@ -1,4 +1,3 @@ -import random import time from ..extensions import BinaryReader, BinaryWriter @@ -42,17 +41,12 @@ class MtProtoPlainSender: return response def _get_new_msg_id(self): - """Generates a new message ID based on the current time (in ms) since epoch""" - # See https://core.telegram.org/mtproto/description#message-identifier-msg-id - ms_time = int(time.time() * 1000) - new_msg_id = (((ms_time // 1000) << 32) - | # "must approximately equal unix time*2^32" - ((ms_time % 1000) << 22) - | # "approximate moment in time the message was created" - random.randint(0, 524288) - << 2) # "message identifiers are divisible by 4" - - # Ensure that we always return a message ID which is higher than the previous one + """Generates a new message ID based on the current time since epoch""" + # See core.telegram.org/mtproto/description#message-identifier-msg-id + now = time.time() + nanoseconds = int((now - int(now)) * 1e+9) + # "message identifiers are divisible by 4" + new_msg_id = (int(now) << 32) | (nanoseconds << 2) if self._last_msg_id >= new_msg_id: new_msg_id = self._last_msg_id + 4 diff --git a/telethon/tl/session.py b/telethon/tl/session.py index ee5f1091..3dfc2ba6 100644 --- a/telethon/tl/session.py +++ b/telethon/tl/session.py @@ -2,7 +2,6 @@ import json import os import pickle import platform -import random import time from threading import Lock from base64 import b64encode, b64decode @@ -65,15 +64,10 @@ class Session: return self.sequence * 2 def get_new_msg_id(self): - """Generates a new message ID based on the current time (in ms) since epoch""" - # Refer to mtproto_plain_sender.py for the original method, this is a simple copy - ms_time = int(time.time() * 1000) - new_msg_id = (((ms_time // 1000 + self.time_offset) << 32) - | # "must approximately equal unix time*2^32" - ((ms_time % 1000) << 22) - | # "approximate moment in time the message was created" - random.randint(0, 524288) - << 2) # "message identifiers are divisible by 4" + now = time.time() + nanoseconds = int((now - int(now)) * 1e+9) + # "message identifiers are divisible by 4" + new_msg_id = (int(now) << 32) | (nanoseconds << 2) if self.last_message_id >= new_msg_id: new_msg_id = self.last_message_id + 4 @@ -133,7 +127,7 @@ class JsonSession: self._sequence = 0 self.salt = 0 # Unsigned long self.time_offset = 0 - self.last_message_id = 0 # Long + self._last_msg_id = 0 # Long def save(self): """Saves the current session object as session_user_id.session""" @@ -229,19 +223,16 @@ class JsonSession: def get_new_msg_id(self): """Generates a new unique message ID based on the current time (in ms) since epoch""" - # Refer to mtproto_plain_sender.py for the original method, - ms_time = int(time.time() * 1000) - new_msg_id = (((ms_time // 1000 + self.time_offset) << 32) - | # "must approximately equal unix time*2^32" - ((ms_time % 1000) << 22) - | # "approximate moment in time the message was created" - random.randint(0, 524288) - << 2) # "message identifiers are divisible by 4" + # Refer to mtproto_plain_sender.py for the original method + now = time.time() + nanoseconds = int((now - int(now)) * 1e+9) + # "message identifiers are divisible by 4" + new_msg_id = (int(now) << 32) | (nanoseconds << 2) - if self.last_message_id >= new_msg_id: - new_msg_id = self.last_message_id + 4 + if self._last_msg_id >= new_msg_id: + new_msg_id = self._last_msg_id + 4 - self.last_message_id = new_msg_id + self._last_msg_id = new_msg_id return new_msg_id def update_time_offset(self, correct_msg_id):