From d7c4d3f4066068484752c3a74c5da9253636f8d5 Mon Sep 17 00:00:00 2001 From: Alexander Zveruk Date: Sun, 3 May 2020 21:18:19 +0300 Subject: [PATCH] message deleting --- tg/controllers/__init__.py | 4 ++++ tg/models/__init__.py | 22 +++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/tg/controllers/__init__.py b/tg/controllers/__init__.py index 71d50bf..6e1da1f 100644 --- a/tg/controllers/__init__.py +++ b/tg/controllers/__init__.py @@ -60,6 +60,10 @@ class Controller: elif keys == 'G': if self.model.jump_bottom(): self.refresh_msgs() + elif keys == 'd': + if self.model.delete_msg(): + self.refresh_msgs() + elif keys == '/': # search diff --git a/tg/models/__init__.py b/tg/models/__init__.py index 346e9cd..be36194 100644 --- a/tg/models/__init__.py +++ b/tg/models/__init__.py @@ -79,6 +79,11 @@ class Model: def get_chats(self, offset=0, limit=10): return self.chats.get_chats(offset=offset, limit=limit) + def delete_msg(self): + chat_id = self.get_current_chat_id() + if chat_id: + return self.msgs.delete_msg(chat_id) + class ChatModel: @@ -152,10 +157,10 @@ class MsgModel: self.current_msgs = defaultdict(int) def next_msg(self, chat_id, step=1): - current_msgs = self.current_msgs[chat_id] - if current_msgs == 0: + current_msg = self.current_msgs[chat_id] + if current_msg == 0: return False - self.current_msgs[chat_id] = max(0, current_msgs - step) + self.current_msgs[chat_id] = max(0, current_msg - step) return True def jump_bottom(self, chat_id): @@ -215,6 +220,17 @@ class MsgModel: else: log.info(f'message has been sent: {result.update}') + def delete_msg(self, chat_id): + current_msg = self.current_msgs[chat_id] + msg = self.msgs[chat_id].pop(current_msg) + log.info(f"Deleting msg {msg}") + + message_ids = [msg["id"]] + r = self.tg.delete_messages(chat_id, message_ids, revoke=True) + r.wait(raise_exc=True) + self.current_msgs[chat_id] -= 1 + return True + class UserModel: