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/ .mypy_cache/
venv/
__pycache__ __pycache__
.env .env
dist dist

View file

@ -221,14 +221,17 @@ class MsgModel:
log.info(f'message has been sent: {result.update}') log.info(f'message has been sent: {result.update}')
def delete_msg(self, chat_id): def delete_msg(self, chat_id):
current_msg = self.current_msgs[chat_id] if chat_id is None:
msg = self.msgs[chat_id].pop(current_msg) return False
log.info(f"Deleting msg {msg}") selected_msg = self.current_msgs[chat_id]
msg_item = self.msgs[chat_id].pop(selected_msg)
message_ids = [msg["id"]] 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 = self.tg.delete_messages(chat_id, message_ids, revoke=True)
r.wait(raise_exc=True) r.wait()
self.current_msgs[chat_id] -= 1
return True return True

View file

@ -304,7 +304,10 @@ class MsgView:
def get_last_msg(chat): 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'] _type = content['@type']
if _type == 'messageText': if _type == 'messageText':
return content['text']['text'] return content['text']['text']
@ -312,7 +315,10 @@ def get_last_msg(chat):
def get_date(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(): if datetime.today().date() == dt.date():
return dt.strftime("%H:%M") return dt.strftime("%H:%M")
return dt.strftime("%d/%b/%y") return dt.strftime("%d/%b/%y")