fix bug with message update wrong type in model

This commit is contained in:
Alexander Zveruk 2020-05-19 23:29:50 +03:00
parent 70bb3bce8a
commit 96d23510cd
2 changed files with 20 additions and 21 deletions

View file

@ -321,29 +321,29 @@ class Controller:
self.view.msgs.draw(current_msg_idx, msgs, MSGS_LEFT_SCROLL_THRESHOLD)
@handle_exception
def update_msg_content(self, update):
content = MsgProxy(update["new_content"])
def update_msg_content(self, update: Dict[str, Any]):
chat_id = update["chat_id"]
message_id = update["message_id"]
self.model.msgs.update_msg_content(chat_id, message_id, content)
self.model.msgs.update_msg_content(
chat_id, message_id, update["new_content"]
)
current_chat_id = self.model.chats.id_by_index(self.model.current_chat)
if current_chat_id == chat_id:
self.refresh_msgs()
@handle_exception
def update_new_msg(self, update):
def update_new_msg(self, update: Dict[str, Any]):
msg = MsgProxy(update["message"])
chat_id = msg["chat_id"]
self.model.msgs.add_message(chat_id, msg)
self.model.msgs.add_message(msg.chat_id, msg.msg)
current_chat_id = self.model.chats.id_by_index(self.model.current_chat)
if current_chat_id == chat_id:
if current_chat_id == msg.chat_id:
self.refresh_msgs()
if msg.file_id and msg.size <= config.max_download_size:
self.download(msg.file_id, chat_id, msg["id"])
self.download(msg.file_id, msg.chat_id, msg["id"])
self._notify_for_message(chat_id, msg)
self._notify_for_message(msg.chat_id, msg)
def _notify_for_message(self, chat_id: int, msg: Dict[str, Any]):
def _notify_for_message(self, chat_id: int, msg: MsgProxy):
# do not notify, if muted
# TODO: optimize
chat = None
@ -356,17 +356,12 @@ class Controller:
return
# notify
user_id = msg["sender_user_id"]
if msg["sender_user_id"] == self.model.get_me()["id"]:
if msg.sender_id == self.model.get_me()["id"]:
return
user = self.model.users.get_user(user_id)
name = "{} {}".format(user["first_name"], user["last_name"])
_type = msg["content"]["@type"]
user = self.model.users.get_user(msg.sender_id)
name = f"{user['first_name']} {user['last_name']}"
if _type == "messageText":
text = msg["content"]["text"]["text"]
else:
text = MsgProxy.types.get(_type, "")
text = msg.text_content if msg.is_text else msg.content_type
notify(text, title=name)
@handle_exception

View file

@ -135,6 +135,10 @@ class MsgProxy:
def is_text(self):
return self.msg["content"]["@type"] == "messageText"
@property
def text_content(self) -> str:
return self.msg["content"]["text"]["text"]
@property
def is_downloaded(self):
doc = self.get_doc(self.msg)
@ -145,8 +149,8 @@ class MsgProxy:
return self.msg.get("reply_to_message_id")
@property
def chat_id(self):
return self.msg.get("chat_id")
def chat_id(self) -> int:
return self.msg["chat_id"]
@property
def sender_id(self) -> int: