Simplify accepted values in forward, delete and mark read

Forward and delete are meant to delete lists.
Now only lists are supported, which should not be an issue
as message.forward_to and message.delete both exist.

mark_read really only works with one message at a time,
so list support was removed for it, as well as the now
redundant max_id.
This commit is contained in:
Lonami Exo 2022-01-16 13:03:00 +01:00
parent 1e779a91b7
commit 6eadc8aed8
3 changed files with 38 additions and 44 deletions

View File

@ -752,3 +752,10 @@ renamed send_read_acknowledge. add send_read_acknowledge as alias for mark_read?
force sms removed as it was broken anyway and not very reliable force sms removed as it was broken anyway and not very reliable
you can now await client.action for a one-off any action not just cancel you can now await client.action for a one-off any action not just cancel
fwd msg and delete msg now mandate a list rather than a single int or msg
(since there's msg.delete and msg.forward_to this should be no issue).
they are meant to work on lists.
also mark read only supports single now. a list would just be max anyway.
removed max id since it's not really of much use.

View File

@ -518,7 +518,7 @@ async def send_message(
async def forward_messages( async def forward_messages(
self: 'TelegramClient', self: 'TelegramClient',
entity: 'hints.EntityLike', entity: 'hints.EntityLike',
messages: 'typing.Union[hints.MessageIDLike, typing.Sequence[hints.MessageIDLike]]', messages: 'typing.Union[typing.Sequence[hints.MessageIDLike]]',
from_peer: 'hints.EntityLike' = None, from_peer: 'hints.EntityLike' = None,
*, *,
background: bool = None, background: bool = None,
@ -530,10 +530,6 @@ async def forward_messages(
if as_album is not None: if as_album is not None:
warnings.warn('the as_album argument is deprecated and no longer has any effect') 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,)
entity = await self.get_input_entity(entity) entity = await self.get_input_entity(entity)
if from_peer: if from_peer:
@ -639,16 +635,13 @@ async def edit_message(
async def delete_messages( async def delete_messages(
self: 'TelegramClient', self: 'TelegramClient',
entity: 'hints.EntityLike', entity: 'hints.EntityLike',
message_ids: 'typing.Union[hints.MessageIDLike, typing.Sequence[hints.MessageIDLike]]', messages: 'typing.Union[typing.Sequence[hints.MessageIDLike]]',
*, *,
revoke: bool = True) -> 'typing.Sequence[_tl.messages.AffectedMessages]': revoke: bool = True) -> 'typing.Sequence[_tl.messages.AffectedMessages]':
if not utils.is_list_like(message_ids): messages = (
message_ids = (message_ids,)
message_ids = (
m.id if isinstance(m, ( m.id if isinstance(m, (
_tl.Message, _tl.MessageService, _tl.MessageEmpty)) _tl.Message, _tl.MessageService, _tl.MessageEmpty))
else int(m) for m in message_ids else int(m) for m in messages
) )
if entity: if entity:
@ -660,42 +653,36 @@ async def delete_messages(
if ty == helpers._EntityType.CHANNEL: if ty == helpers._EntityType.CHANNEL:
res = await self([_tl.fn.channels.DeleteMessages( res = await self([_tl.fn.channels.DeleteMessages(
entity, list(c)) for c in utils.chunks(message_ids)]) entity, list(c)) for c in utils.chunks(messages)])
else: else:
res = await self([_tl.fn.messages.DeleteMessages( res = await self([_tl.fn.messages.DeleteMessages(
list(c), revoke) for c in utils.chunks(message_ids)]) list(c), revoke) for c in utils.chunks(messages)])
return sum(r.pts_count for r in res) return sum(r.pts_count for r in res)
async def mark_read( async def mark_read(
self: 'TelegramClient', self: 'TelegramClient',
entity: 'hints.EntityLike', entity: 'hints.EntityLike',
message: 'typing.Union[hints.MessageIDLike, typing.Sequence[hints.MessageIDLike]]' = None, message: 'hints.MessageIDLike' = None,
*, *,
max_id: int = None,
clear_mentions: bool = False) -> bool: clear_mentions: bool = False) -> bool:
if max_id is None: if not message:
if not message: max_id = 0
max_id = 0 elif isinstance(message, int):
else: max_id = message
if utils.is_list_like(message): else:
max_id = max(msg.id for msg in message) max_id = message.id
else:
max_id = message.id
entity = await self.get_input_entity(entity) entity = await self.get_input_entity(entity)
if clear_mentions: if clear_mentions:
await self(_tl.fn.messages.ReadMentions(entity)) await self(_tl.fn.messages.ReadMentions(entity))
if max_id is None:
return True
if max_id is not None: if helpers._entity_type(entity) == helpers._EntityType.CHANNEL:
if helpers._entity_type(entity) == helpers._EntityType.CHANNEL: return await self(_tl.fn.channels.ReadHistory(
return await self(_tl.fn.channels.ReadHistory( utils.get_input_channel(entity), max_id=max_id))
utils.get_input_channel(entity), max_id=max_id)) else:
else: return await self(_tl.fn.messages.ReadHistory(
return await self(_tl.fn.messages.ReadHistory( entity, max_id=max_id))
entity, max_id=max_id))
return False return False

View File

@ -2254,7 +2254,7 @@ class TelegramClient:
async def forward_messages( async def forward_messages(
self: 'TelegramClient', self: 'TelegramClient',
entity: 'hints.EntityLike', entity: 'hints.EntityLike',
messages: 'typing.Union[hints.MessageIDLike, typing.Sequence[hints.MessageIDLike]]', messages: 'typing.Union[typing.Sequence[hints.MessageIDLike]]',
from_peer: 'hints.EntityLike' = None, from_peer: 'hints.EntityLike' = None,
*, *,
background: bool = None, background: bool = None,
@ -2276,8 +2276,8 @@ class TelegramClient:
entity (`entity`): entity (`entity`):
To which entity the message(s) will be forwarded. To which entity the message(s) will be forwarded.
messages (`list` | `int` | `Message <telethon.tl._custom.message.Message>`): messages (`list`):
The message(s) to forward, or their integer IDs. The messages to forward, or their integer IDs.
from_peer (`entity`): from_peer (`entity`):
If the given messages are integer IDs and not instances If the given messages are integer IDs and not instances
@ -2465,7 +2465,7 @@ class TelegramClient:
async def delete_messages( async def delete_messages(
self: 'TelegramClient', self: 'TelegramClient',
entity: 'hints.EntityLike', entity: 'hints.EntityLike',
message_ids: 'typing.Union[hints.MessageIDLike, typing.Sequence[hints.MessageIDLike]]', messages: 'typing.Union[typing.Sequence[hints.MessageIDLike]]',
*, *,
revoke: bool = True) -> 'typing.Sequence[_tl.messages.AffectedMessages]': revoke: bool = True) -> 'typing.Sequence[_tl.messages.AffectedMessages]':
""" """
@ -2486,8 +2486,8 @@ class TelegramClient:
be `None` for normal chats, but **must** be present be `None` for normal chats, but **must** be present
for channels and megagroups. for channels and megagroups.
message_ids (`list` | `int` | `Message <telethon.tl._custom.message.Message>`): messages (`list`):
The IDs (or ID) or messages to be deleted. The messages to delete, or their integer IDs.
revoke (`bool`, optional): revoke (`bool`, optional):
Whether the message should be deleted for everyone or not. Whether the message should be deleted for everyone or not.
@ -2517,9 +2517,8 @@ class TelegramClient:
async def mark_read( async def mark_read(
self: 'TelegramClient', self: 'TelegramClient',
entity: 'hints.EntityLike', entity: 'hints.EntityLike',
message: 'typing.Union[hints.MessageIDLike, typing.Sequence[hints.MessageIDLike]]' = None, message: 'hints.MessageIDLike' = None,
*, *,
max_id: int = None,
clear_mentions: bool = False) -> bool: clear_mentions: bool = False) -> bool:
""" """
Marks messages as read and optionally clears mentions. Marks messages as read and optionally clears mentions.
@ -2527,8 +2526,8 @@ class TelegramClient:
This effectively marks a message as read (or more than one) in the This effectively marks a message as read (or more than one) in the
given conversation. given conversation.
If neither message nor maximum ID are provided, all messages will be If no message or maximum ID is provided, all messages will be
marked as read by assuming that ``max_id = 0``. marked as read.
If a message or maximum ID is provided, all the messages up to and If a message or maximum ID is provided, all the messages up to and
including such ID will be marked as read (for all messages whose ID including such ID will be marked as read (for all messages whose ID
@ -2540,8 +2539,9 @@ class TelegramClient:
entity (`entity`): entity (`entity`):
The chat where these messages are located. The chat where these messages are located.
message (`list` | `Message <telethon.tl._custom.message.Message>`): message (`Message <telethon.tl._custom.message.Message>`):
Either a list of messages or a single message. The last (most-recent) message which was read, or its ID.
This is only useful if you want to mark a chat as partially read.
max_id (`int`): max_id (`int`):
Until which message should the read acknowledge be sent for. Until which message should the read acknowledge be sent for.