message deleting

This commit is contained in:
Alexander Zveruk 2020-05-03 21:18:19 +03:00
parent e4b1c15a58
commit d7c4d3f406
2 changed files with 23 additions and 3 deletions

View file

@ -60,6 +60,10 @@ class Controller:
elif keys == 'G': elif keys == 'G':
if self.model.jump_bottom(): if self.model.jump_bottom():
self.refresh_msgs() self.refresh_msgs()
elif keys == 'd':
if self.model.delete_msg():
self.refresh_msgs()
elif keys == '/': elif keys == '/':
# search # search

View file

@ -79,6 +79,11 @@ class Model:
def get_chats(self, offset=0, limit=10): def get_chats(self, offset=0, limit=10):
return self.chats.get_chats(offset=offset, limit=limit) 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: class ChatModel:
@ -152,10 +157,10 @@ class MsgModel:
self.current_msgs = defaultdict(int) self.current_msgs = defaultdict(int)
def next_msg(self, chat_id, step=1): def next_msg(self, chat_id, step=1):
current_msgs = self.current_msgs[chat_id] current_msg = self.current_msgs[chat_id]
if current_msgs == 0: if current_msg == 0:
return False return False
self.current_msgs[chat_id] = max(0, current_msgs - step) self.current_msgs[chat_id] = max(0, current_msg - step)
return True return True
def jump_bottom(self, chat_id): def jump_bottom(self, chat_id):
@ -215,6 +220,17 @@ class MsgModel:
else: else:
log.info(f'message has been sent: {result.update}') 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: class UserModel: