Add function to delete messages in current chat

This commit is contained in:
Vladimir Zenin 2022-05-30 23:29:38 +03:00
parent 49981d1247
commit cc50fea15f
4 changed files with 45 additions and 0 deletions

View file

@ -242,6 +242,7 @@ For navigation arrow keys also can be used.
- `r`: read current chat
- `c`: show list of contacts
- `dd`: delete chat or remove history
- `dm`: delete last 100 messages in current chat (telegram has limits on delete messages; you may have to wait a few minutes between calls this command)
- `ng`: create new group chat
- `ns`: create new secret chat
- `/`: search in chats

View file

@ -635,6 +635,38 @@ class Controller:
self.present_info("Chat was deleted")
@bind(chat_handler, ["dm"])
def delete_messages_in_chat(self) -> None:
"""Delete last 100 user messages in the group"""
chat = self.model.chats.chats[self.model.current_chat]
chat_type = get_chat_type(chat)
if chat_type in (
ChatType.chatTypeSupergroup,
ChatType.chatTypeBasicGroup,
):
resp = self.view.status.get_input(
"Are you sure want to delete last 100 yours messages in this group?[y/N]"
)
if is_no(resp or ""):
return self.present_info("Deleting messages discarded")
rv = self.tg.search_chat_messages_for_member(
chat["id"], self.model.get_me()["id"]
)
messages = rv.update["messages"]
message_ids = [message["id"] for message in messages if message["can_be_deleted_for_all_users"]]
total_count = int(rv.update["total_count"])
if total_count == 0:
return self.present_info("No messages to delete")
elif len(message_ids) == 0:
return self.present_info(f"Total messages: {str(total_count)}. Error receiving messages! Try it in a few minutes.")
else:
self.tg.delete_messages(chat["id"], message_ids, revoke=True)
return self.present_info(f"Deleted {len(message_ids)}/{total_count} messages.")
else:
return self.present_info("This is not a group")
@bind(chat_handler, ["n"])
def next_found_chat(self) -> None:
"""Go to next found chat"""

View file

@ -412,6 +412,17 @@ class Tdlib(Telegram):
}
return self._send_data(data)
def search_chat_messages_for_member(
self, chat_id: int, user_id: Any
) -> AsyncResult:
data = {
"@type": "searchChatMessages",
"chat_id": chat_id,
"sender": {'@type': "messageSenderUser", 'user_id': user_id},
"limit": 100,
}
return self._send_data(data, block=True)
def get_user(self, user_id: int) -> AsyncResult:
data = {
"@type": "getUser",

View file

@ -17,6 +17,7 @@ log = logging.getLogger(__name__)
MAX_KEYBINDING_LENGTH = 5
MULTICHAR_KEYBINDINGS = (
"dd",
"dm",
"sd",
"sp",
"sa",