Begin updating the way updates are built

This commit is contained in:
Lonami Exo
2022-02-15 11:57:55 +01:00
parent c914a92dcf
commit 483e2aadf1
18 changed files with 337 additions and 284 deletions

View File

@@ -1,5 +1,6 @@
from typing import Optional, List, TYPE_CHECKING
from datetime import datetime
from dataclasses import dataclass
import mimetypes
from .chatgetter import ChatGetter
from .sendergetter import SenderGetter
@@ -27,17 +28,21 @@ def _fwd(field, doc):
return property(fget, fset, None, doc)
class _InputChat:
"""
Input channels and peer chats use a different name for "id" which breaks the property forwarding.
This class simply holds the two fields with proper names.
"""
@dataclass(frozen=True)
class _TinyChat:
__slots__ = ('id', 'access_hash')
def __init__(self, input):
self.id = getattr(input, 'channel_id', None) or input.chat_id
self.access_hash = getattr(input, 'access_hash', None)
id: int
access_hash: int
@dataclass(frozen=True)
class _TinyChannel:
__slots__ = ('id', 'access_hash', 'megagroup')
id: int
access_hash: int
megagroup: bool # gigagroup is not present in channelForbidden but megagroup is
class Chat:
@@ -159,7 +164,11 @@ class Chat:
except AttributeError:
migrated = None
return Chat(_InputChat(migrated), self._client) if migrated else None
if migrated is None:
return migrated
# Small chats don't migrate to other small chats, nor do they migrate to broadcast channels
return type(self)._new(self._client, _TinyChannel(migrated.channel_id, migrated.access_hash, True))
def __init__(self):
raise TypeError('You cannot create Chat instances by hand!')
@@ -168,7 +177,18 @@ class Chat:
def _new(cls, client, chat):
self = cls.__new__(cls)
self._client = client
self._chat = chat
if isinstance(cls, Entity):
if chat.is_user:
raise TypeError('Tried to construct Chat with non-chat Entity')
elif chat.ty == EntityType.GROUP:
self._chat = _TinyChat(chat.id)
else:
self._chat = _TinyChannel(chat.id, chat.hash, chat.is_group)
else:
self._chat = chat
self._full = None
return self
@@ -237,12 +257,12 @@ class Chat:
.. _megagroup: https://telegram.org/blog/supergroups5k
"""
return True
return isinstance(self._chat, (_tl.Chat, _TinyChat, _tl.ChatForbidden, _tl.ChatEmpty)) or self._chat.megagroup
@property
def is_broadcast(self):
"""
Returns `True` if the chat is a broadcast channel group chat or `broadcast group`_.
Returns `True` if the chat is a broadcast channel group chat or `broadcast group`_.
This property also exists in `User`, where it returns `False`.
@@ -253,7 +273,7 @@ class Chat:
.. _broadcast group: https://telegram.org/blog/autodelete-inv2#groups-with-unlimited-members
"""
return True
return not self.is_group
@property
def full_name(self):

View File

@@ -7,11 +7,9 @@ from ... import errors, _tl
class ChatGetter(abc.ABC):
"""
Helper base class that introduces the chat-related properties and methods.
"""
def __init__(self, chat, client):
self._chat = chat
self._client = client
The parent class must set both ``_chat`` and ``_client``.
"""
@property
def chat(self):
"""
@@ -60,7 +58,7 @@ class ChatGetter(abc.ABC):
# Here there's no need to fetch the chat - get_messages already did
print(message.chat.stringify())
"""
raise RuntimeError('TODO')
raise RuntimeError('TODO fetch if it is tiny')
@property
def chat_id(self):

View File

@@ -428,10 +428,10 @@ class Message(ChatGetter, SenderGetter):
if message.from_id is not None:
sender_id = utils.get_peer_id(message.from_id)
# Note that these calls would reset the client
ChatGetter.__init__(self, message.peer_id, broadcast=message.post)
SenderGetter.__init__(self, sender_id)
self = cls.__new__(cls)
self._client = client
self._sender = entities.get(_tl.PeerUser(update.user_id))
self._chat = entities.get(_tl.PeerUser(update.user_id))
self._message = message
# Convenient storage for custom functions

View File

@@ -4,11 +4,9 @@ import abc
class SenderGetter(abc.ABC):
"""
Helper base class that introduces the sender-related properties and methods.
"""
def __init__(self, sender, client):
self._sender = sender
self._client = client
The parent class must set both ``_sender`` and ``_client``.
"""
@property
def sender(self):
"""
@@ -57,7 +55,7 @@ class SenderGetter(abc.ABC):
# Here there's no need to fetch the sender - get_messages already did
print(message.sender.stringify())
"""
raise RuntimeError('TODO')
raise RuntimeError('TODO fetch if it is tiny')
@property
def sender_id(self):

View File

@@ -1,5 +1,6 @@
from typing import Optional, List, TYPE_CHECKING
from datetime import datetime
from dataclasses import dataclass
import mimetypes
from .chatgetter import ChatGetter
from .sendergetter import SenderGetter
@@ -67,6 +68,14 @@ class BotInfo:
self._user = user
@dataclass(frozen=True)
class _TinyUser:
__slots__ = ('id', 'access_hash')
id: int
access_hash: int
class User:
"""
Represents a :tl:`User` (or :tl:`UserEmpty`, or :tl:`UserFull`) from the API.
@@ -161,9 +170,16 @@ class User:
@classmethod
def _new(cls, client, user):
self = cls.__new__(cls)
self._client = client
self._user = user
if isinstance(user, Entity):
if not user.is_user:
raise TypeError('Tried to construct User with non-user Entity')
self._user = _TinyUser(user.id, user.hash)
else:
self._user = user
self._full = None
raise RuntimeError('self._i_need_to_include_participant_info')