mirror of
				https://github.com/LonamiWebs/Telethon.git
				synced 2025-11-04 09:57:29 +03:00 
			
		
		
		
	Cleanup utils.get_peer_id
This commit is contained in:
		
							parent
							
								
									3bf70692b4
								
							
						
					
					
						commit
						6d877549cb
					
				| 
						 | 
					@ -306,10 +306,10 @@ def get_peer_id(peer, add_mark=False, get_kind=False):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
       If 'get_kind', the kind will be returned as a second value.
 | 
					       If 'get_kind', the kind will be returned as a second value.
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    i, k = None, None  # determined ID and kind
 | 
					    # First we assert it's a Peer TLObject, or early return for integers
 | 
				
			||||||
    if not isinstance(peer, TLObject):
 | 
					    if not isinstance(peer, TLObject):
 | 
				
			||||||
        if isinstance(peer, int):
 | 
					        if isinstance(peer, int):
 | 
				
			||||||
            i, k = peer, PeerUser
 | 
					            return (peer, PeerUser) if get_kind else peer
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            _raise_cast_fail(peer, 'int')
 | 
					            _raise_cast_fail(peer, 'int')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -317,23 +317,25 @@ def get_peer_id(peer, add_mark=False, get_kind=False):
 | 
				
			||||||
        # Not a Peer or an InputPeer, so first get its Input version
 | 
					        # Not a Peer or an InputPeer, so first get its Input version
 | 
				
			||||||
        peer = get_input_peer(peer, allow_self=False)
 | 
					        peer = get_input_peer(peer, allow_self=False)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Set the right ID/kind, or raise if the TLObject is not recognised
 | 
				
			||||||
 | 
					    i, k = None, None
 | 
				
			||||||
    if isinstance(peer, PeerUser) or isinstance(peer, InputPeerUser):
 | 
					    if isinstance(peer, PeerUser) or isinstance(peer, InputPeerUser):
 | 
				
			||||||
        i, k = peer.user_id, PeerUser
 | 
					        i, k = peer.user_id, PeerUser
 | 
				
			||||||
    elif isinstance(peer, PeerChat) or isinstance(peer, InputPeerChat):
 | 
					    elif isinstance(peer, PeerChat) or isinstance(peer, InputPeerChat):
 | 
				
			||||||
        k = PeerChat
 | 
					        i, k = peer.chat_id, PeerChat
 | 
				
			||||||
        # Chats are marked by turning them into negative numbers
 | 
					 | 
				
			||||||
        i = -peer.chat_id if add_mark else peer.chat_id
 | 
					 | 
				
			||||||
    elif isinstance(peer, PeerChannel) or isinstance(peer, InputPeerChannel):
 | 
					    elif isinstance(peer, PeerChannel) or isinstance(peer, InputPeerChannel):
 | 
				
			||||||
        k = PeerChannel
 | 
					        i, k = peer.channel_id, PeerChannel
 | 
				
			||||||
        i = peer.channel_id
 | 
					    else:
 | 
				
			||||||
 | 
					        _raise_cast_fail(peer, 'int')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if add_mark:
 | 
					    if add_mark:
 | 
				
			||||||
 | 
					        if k == PeerChat:
 | 
				
			||||||
 | 
					            i = -i
 | 
				
			||||||
 | 
					        elif k == PeerChannel:
 | 
				
			||||||
            # Concat -100 through math tricks, .to_supergroup() on Madeline
 | 
					            # Concat -100 through math tricks, .to_supergroup() on Madeline
 | 
				
			||||||
            # IDs will be strictly positive -> log works
 | 
					            # IDs will be strictly positive -> log works
 | 
				
			||||||
            i = -(i + pow(10, math.floor(math.log10(i) + 3)))
 | 
					            i = -(i + pow(10, math.floor(math.log10(i) + 3)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if i is None:
 | 
					 | 
				
			||||||
        _raise_cast_fail(peer, 'int')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return (i, k) if get_kind else i  # return kind only if get_kind
 | 
					    return (i, k) if get_kind else i  # return kind only if get_kind
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user