correctly handle situation when there are chats with all deleted msgs

This commit is contained in:
Alexander Zveruk 2020-05-03 21:39:38 +03:00
parent d7c4d3f406
commit 2aae0d5da6
3 changed files with 19 additions and 9 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
.mypy_cache/
venv/
__pycache__
.env
dist

View file

@ -221,14 +221,17 @@ class MsgModel:
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"]]
if chat_id is None:
return False
selected_msg = self.current_msgs[chat_id]
msg_item = self.msgs[chat_id].pop(selected_msg)
self.current_msgs[chat_id] = min(
selected_msg, len(self.msgs[chat_id]) - 1
)
log.info(f"Deleting msg from the chat {chat_id}: {msg_item}")
message_ids = [msg_item["id"]]
r = self.tg.delete_messages(chat_id, message_ids, revoke=True)
r.wait(raise_exc=True)
self.current_msgs[chat_id] -= 1
r.wait()
return True

View file

@ -304,7 +304,10 @@ class MsgView:
def get_last_msg(chat):
content = chat['last_message']['content']
last_msg = chat.get('last_message')
if not last_msg:
return "<No messages yet>"
content = last_msg['content']
_type = content['@type']
if _type == 'messageText':
return content['text']['text']
@ -312,7 +315,10 @@ def get_last_msg(chat):
def get_date(chat):
dt = datetime.fromtimestamp(chat['last_message']['date'])
last_msg = chat.get('last_message')
if not last_msg:
return "<NA>"
dt = datetime.fromtimestamp(last_msg['date'])
if datetime.today().date() == dt.date():
return dt.strftime("%H:%M")
return dt.strftime("%d/%b/%y")