Skip to content

Commit 468f1eb

Browse files
Refactor get_peer_id and get_peer_type
1 parent 9f606a3 commit 468f1eb

4 files changed

Lines changed: 34 additions & 33 deletions

File tree

pyrogram/client.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ async def fetch_peers(self, peers: List[Union[raw.types.User, raw.types.Chat, ra
747747
elif isinstance(peer, raw.types.Channel):
748748
peer_id = utils.get_channel_id(peer.id)
749749
access_hash = peer.access_hash
750-
peer_type = "channel" if peer.broadcast else "supergroup"
750+
peer_type = "direct" if peer.monoforum else "channel" if peer.broadcast else "forum" if peer.forum else "supergroup"
751751

752752
if peer.username:
753753
usernames.append(peer.username.lower())
@@ -761,10 +761,14 @@ async def fetch_peers(self, peers: List[Union[raw.types.User, raw.types.Chat, ra
761761
continue
762762

763763
parsed_peers.append((peer_id, access_hash, peer_type, phone_number))
764-
parsed_usernames.append((peer_id, usernames))
764+
765+
if usernames:
766+
parsed_usernames.append((peer_id, usernames))
765767

766768
await self.storage.update_peers(parsed_peers)
767-
await self.storage.update_usernames(parsed_usernames)
769+
770+
if parsed_usernames:
771+
await self.storage.update_usernames(parsed_usernames)
768772

769773
return is_min
770774

pyrogram/methods/advanced/recover_gaps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import pyrogram
2323
from pyrogram import raw
2424
from pyrogram.errors import ChannelInvalid, ChannelPrivate, PersistentTimestampInvalid, PersistentTimestampOutdated
25-
from pyrogram.utils import MIN_MONOFORUM_CHANNEL_ID
25+
from pyrogram.utils import ZERO_CHANNEL_ID
2626

2727
log = logging.getLogger(__name__)
2828

@@ -69,7 +69,7 @@ async def recover_gaps(self: "pyrogram.Client") -> Tuple[int, int]:
6969
pts=local_pts,
7070
limit=10000,
7171
force=False
72-
) if id < 0 or id > MIN_MONOFORUM_CHANNEL_ID else
72+
) if id < ZERO_CHANNEL_ID else
7373
raw.functions.updates.GetDifference(
7474
pts=local_pts,
7575
date=local_date,

pyrogram/storage/sqlite_storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def get_input_peer(peer_id: int, access_hash: int, peer_type: str):
9696
chat_id=-peer_id
9797
)
9898

99-
if peer_type in ["channel", "supergroup"]:
99+
if peer_type in ["direct", "channel", "forum", "supergroup"]:
100100
return raw.types.InputPeerChannel(
101101
channel_id=utils.get_channel_id(peer_id),
102102
access_hash=access_hash

pyrogram/utils.py

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -292,60 +292,61 @@ def unpack_inline_message_id(inline_message_id: str) -> "raw.base.InputBotInline
292292
access_hash=unpacked[3]
293293
)
294294

295+
ZERO_SECRET_CHAT_ID = -2000000000000
296+
ZERO_CHANNEL_ID = -1000000000000
295297

296-
MIN_CHANNEL_ID = -100999999999999
297-
MAX_CHANNEL_ID = -1000000000000
298-
MIN_MONOFORUM_CHANNEL_ID = 1070000000000
299-
MAX_MONOFORUM_CHANNEL_ID = 107999999999999
300-
MIN_CHAT_ID = -999999999999
301-
MAX_USER_ID = 999999999999
298+
MAX_CHANNEL_ID = 1000000000000 - (1 << 31)
299+
MIN_MONOFORUM_CHANNEL_ID = 1000000000000 + (1 << 31) + 1
300+
MAX_MONOFORUM_CHANNEL_ID = 3000000000000
301+
MAX_USER_ID = (1 << 40) - 1
302+
MAX_CHAT_ID = 999999999999
302303

303304

304305
def get_raw_peer_id(peer: Union[raw.base.Peer, raw.base.InputPeer, raw.base.RequestedPeer]) -> Optional[int]:
305306
"""Get the raw peer id from a Peer object"""
306-
if isinstance(peer, (raw.types.PeerUser, raw.types.InputPeerUser, raw.types.RequestedPeerUser)):
307+
if hasattr(peer, "user_id"):
307308
return peer.user_id
308309

309-
if isinstance(peer, (raw.types.PeerChat, raw.types.InputPeerChat, raw.types.RequestedPeerChat)):
310+
if hasattr(peer, "chat_id"):
310311
return peer.chat_id
311312

312-
if isinstance(peer, (raw.types.PeerChannel, raw.types.InputPeerChannel, raw.types.RequestedPeerChannel)):
313+
if hasattr(peer, "channel_id"):
313314
return peer.channel_id
314315

315-
return None
316+
raise ValueError(f"Peer type invalid: {peer}")
316317

317318

318319
def get_peer_id(peer: Union[raw.base.Peer, raw.base.InputPeer, raw.base.RequestedPeer]) -> int:
319320
"""Get the non-raw peer id from a Peer object"""
320-
if isinstance(peer, (raw.types.PeerUser, raw.types.InputPeerUser, raw.types.RequestedPeerUser)):
321+
if hasattr(peer, "user_id"):
321322
return peer.user_id
322323

323-
if isinstance(peer, (raw.types.PeerChat, raw.types.InputPeerChat, raw.types.RequestedPeerChat)):
324+
if hasattr(peer, "chat_id"):
324325
return -peer.chat_id
325326

326-
if isinstance(peer, (raw.types.PeerChannel, raw.types.InputPeerChannel, raw.types.RequestedPeerChannel)):
327-
if MIN_MONOFORUM_CHANNEL_ID <= peer.channel_id < MAX_MONOFORUM_CHANNEL_ID:
328-
return peer.channel_id
329-
330-
return MAX_CHANNEL_ID - peer.channel_id
327+
if hasattr(peer, "channel_id"):
328+
return ZERO_CHANNEL_ID - peer.channel_id
331329

332330
raise ValueError(f"Peer type invalid: {peer}")
333331

334332

335333
def get_peer_type(peer_id: int) -> str:
336334
if peer_id < 0:
337-
if MIN_CHAT_ID <= peer_id:
335+
if -MAX_CHAT_ID <= peer_id:
338336
return "chat"
339337

340-
if MIN_CHANNEL_ID <= peer_id < MAX_CHANNEL_ID:
338+
if ZERO_CHANNEL_ID - MAX_CHANNEL_ID <= peer_id and peer_id != ZERO_CHANNEL_ID:
341339
return "channel"
342340

341+
if ZERO_SECRET_CHAT_ID + (-1 << 31) <= peer_id and peer_id != ZERO_SECRET_CHAT_ID:
342+
return "secret_chat"
343+
344+
if ZERO_CHANNEL_ID - MAX_MONOFORUM_CHANNEL_ID <= peer_id:
345+
return "monoforum"
346+
343347
elif 0 < peer_id <= MAX_USER_ID:
344348
return "user"
345349

346-
elif MIN_MONOFORUM_CHANNEL_ID <= peer_id < MAX_MONOFORUM_CHANNEL_ID:
347-
return "monoforum"
348-
349350
raise ValueError(f"Peer id invalid: {peer_id}")
350351

351352

@@ -388,7 +389,6 @@ async def get_reply_to(
388389
todo_item_id=reply_parameters.checklist_task_id
389390
)
390391

391-
392392
if message_thread_id:
393393
return raw.types.InputReplyToMessage(
394394
reply_to_msg_id=message_thread_id
@@ -403,10 +403,7 @@ async def get_reply_to(
403403

404404

405405
def get_channel_id(peer_id: int) -> int:
406-
if MIN_MONOFORUM_CHANNEL_ID <= peer_id < MAX_MONOFORUM_CHANNEL_ID:
407-
return peer_id
408-
409-
return MAX_CHANNEL_ID - peer_id
406+
return ZERO_CHANNEL_ID - peer_id
410407

411408

412409
def btoi(b: bytes) -> int:

0 commit comments

Comments
 (0)