mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-09 13:29:47 +00:00
Make custom.Message functional
This commit is contained in:
@@ -291,16 +291,16 @@ class _AdminLogIter(requestiter.RequestIter):
|
||||
for ev in r.events:
|
||||
if isinstance(ev.action,
|
||||
_tl.ChannelAdminLogEventActionEditMessage):
|
||||
ev.action.prev_message._finish_init(
|
||||
self.client, entities, self.entity)
|
||||
ev.action.prev_message = _custom.Message._new(
|
||||
self.client, ev.action.prev_message, entities, self.entity)
|
||||
|
||||
ev.action.new_message._finish_init(
|
||||
self.client, entities, self.entity)
|
||||
ev.action.new_message = _custom.Message._new(
|
||||
self.client, ev.action.new_message, entities, self.entity)
|
||||
|
||||
elif isinstance(ev.action,
|
||||
_tl.ChannelAdminLogEventActionDeleteMessage):
|
||||
ev.action.message._finish_init(
|
||||
self.client, entities, self.entity)
|
||||
ev.action.message = _custom.Message._new(
|
||||
self.client, ev.action.message, entities, self.entity)
|
||||
|
||||
self.buffer.append(_custom.AdminLogEvent(ev, entities))
|
||||
|
||||
|
@@ -58,10 +58,10 @@ class _DialogsIter(requestiter.RequestIter):
|
||||
for x in itertools.chain(r.users, r.chats)
|
||||
if not isinstance(x, (_tl.UserEmpty, _tl.ChatEmpty))}
|
||||
|
||||
messages = {}
|
||||
for m in r.messages:
|
||||
m._finish_init(self.client, entities, None)
|
||||
messages[_dialog_message_key(m.peer_id, m.id)] = m
|
||||
messages = {
|
||||
_dialog_message_key(m.peer_id, m.id): _custom.Message._new(self.client, m, entities, None)
|
||||
for m in r.messages
|
||||
}
|
||||
|
||||
for d in r.dialogs:
|
||||
# We check the offset date here because Telegram may ignore it
|
||||
|
@@ -3,6 +3,7 @@ import re
|
||||
import typing
|
||||
|
||||
from .. import helpers, utils, _tl
|
||||
from ..types import _custom
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
from .telegramclient import TelegramClient
|
||||
@@ -94,7 +95,7 @@ def _get_response_message(self: 'TelegramClient', request, result, input_chat):
|
||||
|
||||
elif isinstance(update, (
|
||||
_tl.UpdateNewChannelMessage, _tl.UpdateNewMessage)):
|
||||
update.message._finish_init(self, entities, input_chat)
|
||||
update.message = _custom.Message._new(self, update.message, entities, input_chat)
|
||||
|
||||
# Pinning a message with `updatePinnedMessage` seems to
|
||||
# always produce a service message we can't map so return
|
||||
@@ -110,7 +111,7 @@ def _get_response_message(self: 'TelegramClient', request, result, input_chat):
|
||||
|
||||
elif (isinstance(update, _tl.UpdateEditMessage)
|
||||
and helpers._entity_type(request.peer) != helpers._EntityType.CHANNEL):
|
||||
update.message._finish_init(self, entities, input_chat)
|
||||
update.message = _custom.Message._new(self, update.message, entities, input_chat)
|
||||
|
||||
# Live locations use `sendMedia` but Telegram responds with
|
||||
# `updateEditMessage`, which means we won't have `id` field.
|
||||
@@ -123,28 +124,24 @@ def _get_response_message(self: 'TelegramClient', request, result, input_chat):
|
||||
and utils.get_peer_id(request.peer) ==
|
||||
utils.get_peer_id(update.message.peer_id)):
|
||||
if request.id == update.message.id:
|
||||
update.message._finish_init(self, entities, input_chat)
|
||||
return update.message
|
||||
return _custom.Message._new(self, update.message, entities, input_chat)
|
||||
|
||||
elif isinstance(update, _tl.UpdateNewScheduledMessage):
|
||||
update.message._finish_init(self, entities, input_chat)
|
||||
# Scheduled IDs may collide with normal IDs. However, for a
|
||||
# single request there *shouldn't* be a mix between "some
|
||||
# scheduled and some not".
|
||||
id_to_message[update.message.id] = update.message
|
||||
id_to_message[update.message.id] = _custom.Message._new(self, update.message, entities, input_chat)
|
||||
|
||||
elif isinstance(update, _tl.UpdateMessagePoll):
|
||||
if request.media.poll.id == update.poll_id:
|
||||
m = _tl.Message(
|
||||
return _custom.Message._new(self, _tl.Message(
|
||||
id=request.id,
|
||||
peer_id=utils.get_peer(request.peer),
|
||||
media=_tl.MessageMediaPoll(
|
||||
poll=update.poll,
|
||||
results=update.results
|
||||
)
|
||||
)
|
||||
m._finish_init(self, entities, input_chat)
|
||||
return m
|
||||
), entities, input_chat)
|
||||
|
||||
if request is None:
|
||||
return id_to_message
|
||||
|
@@ -5,6 +5,7 @@ import warnings
|
||||
|
||||
from .. import errors, hints, _tl
|
||||
from .._misc import helpers, utils, requestiter
|
||||
from ..types import _custom
|
||||
|
||||
_MAX_CHUNK_SIZE = 100
|
||||
|
||||
@@ -200,8 +201,7 @@ class _MessagesIter(requestiter.RequestIter):
|
||||
# is an attempt to avoid these duplicates, since the message
|
||||
# IDs are returned in descending order (or asc if reverse).
|
||||
self.last_id = message.id
|
||||
message._finish_init(self.client, entities, self.entity)
|
||||
self.buffer.append(message)
|
||||
self.buffer.append(_custom.Message._new(self.client, message, entities, self.entity))
|
||||
|
||||
if len(r.messages) < self.request.limit:
|
||||
return True
|
||||
@@ -315,8 +315,7 @@ class _IDsIter(requestiter.RequestIter):
|
||||
from_id and message.peer_id != from_id):
|
||||
self.buffer.append(None)
|
||||
else:
|
||||
message._finish_init(self.client, entities, self._entity)
|
||||
self.buffer.append(message)
|
||||
self.buffer.append(_custom.Message._new(self.client, message, entities, self._entity))
|
||||
|
||||
|
||||
def iter_messages(
|
||||
@@ -498,7 +497,7 @@ async def send_message(
|
||||
|
||||
result = await self(request)
|
||||
if isinstance(result, _tl.UpdateShortSentMessage):
|
||||
message = _tl.Message(
|
||||
return _custom.Message._new(self, _tl.Message(
|
||||
id=result.id,
|
||||
peer_id=await self._get_peer(entity),
|
||||
message=message,
|
||||
@@ -508,9 +507,7 @@ async def send_message(
|
||||
entities=result.entities,
|
||||
reply_markup=request.reply_markup,
|
||||
ttl_period=result.ttl_period
|
||||
)
|
||||
message._finish_init(self, {}, entity)
|
||||
return message
|
||||
), {}, entity)
|
||||
|
||||
return self._get_response_message(request, result, entity)
|
||||
|
||||
|
@@ -3783,6 +3783,9 @@ class TelegramClient:
|
||||
async def _handle_auto_reconnect(self: 'TelegramClient'):
|
||||
return await updates._handle_auto_reconnect(**locals())
|
||||
|
||||
def _self_id(self: 'TelegramClient') -> typing.Optional[int]:
|
||||
return users._self_id(**locals())
|
||||
|
||||
# endregion Private
|
||||
|
||||
# TODO re-patch everything to remove the intermediate calls
|
||||
|
Reference in New Issue
Block a user