Adapt the rest of the library to layer 119

This commit is contained in:
Lonami Exo
2020-10-01 12:22:55 +02:00
parent 62737c1caf
commit d5e4398ace
14 changed files with 78 additions and 98 deletions

View File

@@ -60,7 +60,7 @@ class _DialogsIter(RequestIter):
messages = {}
for m in r.messages:
m._finish_init(self.client, entities, None)
messages[_dialog_message_key(m.to_id, m.id)] = m
messages[_dialog_message_key(m.peer_id, m.id)] = m
for d in r.dialogs:
# We check the offset date here because Telegram may ignore it

View File

@@ -157,7 +157,7 @@ class MessageParseMethods:
elif (isinstance(update, types.UpdateEditChannelMessage)
and utils.get_peer_id(request.peer) ==
utils.get_peer_id(update.message.to_id)):
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
@@ -170,7 +170,7 @@ class MessageParseMethods:
if request.media.poll.id == update.poll_id:
m = types.Message(
id=request.id,
to_id=utils.get_peer(request.peer),
peer_id=utils.get_peer(request.peer),
media=types.MessageMediaPoll(
poll=update.poll,
results=update.results

View File

@@ -1,6 +1,7 @@
import inspect
import itertools
import typing
import warnings
from .. import helpers, utils, errors, hints
from ..requestiter import RequestIter
@@ -62,7 +63,7 @@ class _MessagesIter(RequestIter):
from_user = None # Ignore from_user unless it's a user
if from_user:
self.from_id = await self.client.get_peer_id(from_user)
self.from_id = utils.get_peer(from_user)
else:
self.from_id = None
@@ -76,7 +77,9 @@ class _MessagesIter(RequestIter):
if not self.entity:
self.request = functions.messages.SearchGlobalRequest(
q=search or '',
offset_rate=offset_date,
min_date=None,
max_date=offset_date,
offset_rate=None,
offset_peer=types.InputPeerEmpty(),
offset_id=offset_id,
limit=1
@@ -270,7 +273,7 @@ class _IDsIter(RequestIter):
else:
r = await self.client(functions.messages.GetMessagesRequest(ids))
if self._entity:
from_id = await self.client.get_peer_id(self._entity)
from_id = utils.get_peer(self._entity)
if isinstance(r, types.messages.MessagesNotModified):
self.buffer.extend(None for _ in ids)
@@ -289,7 +292,7 @@ class _IDsIter(RequestIter):
# arbitrary chats. Validate these unless ``from_id is None``.
for message in r.messages:
if isinstance(message, types.MessageEmpty) or (
from_id and message.chat_id != from_id):
from_id and message.peer_id != from_id):
self.buffer.append(None)
else:
message._finish_init(self.client, entities, self._entity)
@@ -753,7 +756,7 @@ class MessageMethods:
if isinstance(result, types.UpdateShortSentMessage):
message = types.Message(
id=result.id,
to_id=utils.get_peer(entity),
peer_id=utils.get_peer(entity),
message=message,
date=result.date,
out=result.out,
@@ -805,15 +808,7 @@ class MessageMethods:
this behaviour.
as_album (`bool`, optional):
Whether several image messages should be forwarded as an
album (grouped) or not. The default behaviour is to treat
albums specially and send outgoing requests with
``as_album=True`` only for the albums if message objects
are used. If IDs are used it will group by default.
In short, the default should do what you expect,
`True` will group always (even converting separate
images into albums), and `False` will never group.
This flag no longer has any effect.
schedule (`hints.DateLike`, optional):
If set, the message(s) won't forward immediately, and
@@ -846,6 +841,9 @@ class MessageMethods:
# Forwarding as a copy
await client.send_message(chat, message)
"""
if as_album is not None:
warnings.warn('the as_album argument is deprecated and no longer has any effect')
single = not utils.is_list_like(messages)
if single:
messages = (messages,)
@@ -858,44 +856,24 @@ class MessageMethods:
else:
from_peer_id = None
def _get_key(m):
def get_key(m):
if isinstance(m, int):
if from_peer_id is not None:
return from_peer_id, None
return from_peer_id
raise ValueError('from_peer must be given if integer IDs are used')
elif isinstance(m, types.Message):
return m.chat_id, m.grouped_id
return m.chat_id
else:
raise TypeError('Cannot forward messages of type {}'.format(type(m)))
# We want to group outgoing chunks differently if we are "smart"
# about sending as album.
#
# Why? We need separate requests for ``as_album=True/False``, so
# if we want that behaviour, when we group messages to create the
# chunks, we need to consider the grouped ID too. But if we don't
# care about that, we don't need to consider it for creating the
# chunks, so we can make less requests.
if as_album is None:
get_key = _get_key
else:
def get_key(m):
return _get_key(m)[0] # Ignore grouped_id
sent = []
for chat_id, chunk in itertools.groupby(messages, key=get_key):
for _chat_id, chunk in itertools.groupby(messages, key=get_key):
chunk = list(chunk)
if isinstance(chunk[0], int):
chat = from_peer
grouped = True if as_album is None else as_album
else:
chat = await chunk[0].get_input_chat()
if as_album is None:
grouped = any(m.grouped_id is not None for m in chunk)
else:
grouped = as_album
chunk = [m.id for m in chunk]
req = functions.messages.ForwardMessagesRequest(
@@ -903,10 +881,6 @@ class MessageMethods:
id=chunk,
to_peer=entity,
silent=silent,
# Trying to send a single message as grouped will cause
# GROUPED_MEDIA_INVALID. If more than one message is forwarded
# (even without media...), this error goes away.
grouped=len(chunk) > 1 and grouped,
schedule_date=schedule
)
result = await self(req)
@@ -1018,7 +992,7 @@ class MessageMethods:
elif isinstance(entity, types.Message):
text = message # Shift the parameters to the right
message = entity
entity = entity.to_id
entity = entity.peer_id
text, msg_entities = await self._parse_message_text(text, parse_mode)
file_handle, media, image = await self._file_to_media(file,