Add msg edited handler (#68)

* Add msg edited handler

* Rename tdlib functions to match original names
This commit is contained in:
Nameless 2020-06-05 14:21:36 +08:00 committed by GitHub
parent b6d8a63a30
commit 27a89e7745
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 18 deletions

View file

@ -136,7 +136,7 @@ class Controller:
from_chat_id, msg_ids = self.model.yanked_msgs
if not msg_ids:
return
self.tg.forward_msgs(chat_id, from_chat_id, msg_ids)
self.tg.forward_messages(chat_id, from_chat_id, msg_ids)
self.present_info(f"Forwarded {len(msg_ids)} messages")
self.model.yanked_msgs = (0, [])

View file

@ -287,12 +287,9 @@ class MsgModel:
self, chat_id: int, msg_id: int, message_content: Dict[str, Any]
) -> bool:
log.info(f"updating {msg_id=} {message_content=}")
for msg in self.msgs[chat_id]:
if msg["id"] != msg_id:
continue
msg["content"] = message_content
return True
return False
return self.update_msg_fields(
chat_id, msg_id, fields=dict(content=message_content)
)
def update_msg_content_opened(self, chat_id: int, msg_id: int):
for message in self.msgs[chat_id]:
@ -308,6 +305,19 @@ class MsgModel:
# https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1update_message_content_opened.html
return
def update_msg_fields(
self, chat_id: int, msg_id: int, fields: Dict[str, Any]
):
msg = None
for message in self.msgs[chat_id]:
if message["id"] == msg_id:
msg = message
break
if not msg:
return False
msg.update(fields)
return True
def add_message(self, chat_id: int, message: Dict[str, Any]) -> bool:
msg_id = message["id"]
msg_set = self.msg_ids[chat_id]
@ -381,7 +391,7 @@ class MsgModel:
def edit_message(self, chat_id: int, message_id: int, text: str) -> bool:
log.info("Editing msg")
result = self.tg.edit_message(chat_id, message_id, text)
result = self.tg.edit_message_text(chat_id, message_id, text)
result.wait()
if result.error:

View file

@ -104,14 +104,14 @@ class Tdlib(Telegram):
}
return self._send_data(data)
def edit_message(self, chat_id: int, message_id: int, msg: str):
def edit_message_text(self, chat_id: int, message_id: int, text: str):
data = {
"@type": "editMessageText",
"message_id": message_id,
"chat_id": chat_id,
"input_message_content": {
"@type": "inputMessageText",
"text": {"@type": "formattedText", "text": msg},
"text": {"@type": "formattedText", "text": text},
},
}
return self._send_data(data)
@ -167,7 +167,7 @@ class Tdlib(Telegram):
}
return self._send_data(data)
def forward_msgs(
def forward_messages(
self,
chat_id: int,
from_chat_id: int,

View file

@ -38,7 +38,7 @@ def update_handler(update_type):
@update_handler("updateMessageContent")
def update_msg_content(controller: Controller, update: Dict[str, Any]):
def update_message_content(controller: Controller, update: Dict[str, Any]):
chat_id = update["chat_id"]
message_id = update["message_id"]
controller.model.msgs.update_msg_content(
@ -50,8 +50,22 @@ def update_msg_content(controller: Controller, update: Dict[str, Any]):
controller.render_msgs()
@update_handler("updateMessageEdited")
def update_message_edited(controller: Controller, update: Dict[str, Any]):
chat_id = update["chat_id"]
message_id = update["message_id"]
edit_date = update["edit_date"]
controller.model.msgs.update_msg_fields(
chat_id, message_id, fields=dict(edit_date=edit_date)
)
current_chat_id = controller.model.current_chat_id
if current_chat_id == chat_id:
controller.render_msgs()
@update_handler("updateNewMessage")
def update_new_msg(controller: Controller, update: Dict[str, Any]):
def update_new_message(controller: Controller, update: Dict[str, Any]):
msg = MsgProxy(update["message"])
controller.model.msgs.add_message(msg.chat_id, msg.msg)
current_chat_id = controller.model.current_chat_id
@ -86,7 +100,7 @@ def update_chat_title(controller: Controller, update: Dict[str, Any]):
@update_handler("updateChatIsMarkedAsUnread")
def update_chat_marked_as_unread(
def update_chat_is_marked_as_unread(
controller: Controller, update: Dict[str, Any]
):
log.info("Proccessing updateChatIsMarkedAsUnread")
@ -144,7 +158,7 @@ def update_chat_read_inbox(controller: Controller, update: Dict[str, Any]):
@update_handler("updateChatDraftMessage")
def update_chat_draft_msg(controller: Controller, update: Dict[str, Any]):
def update_chat_draft_message(controller: Controller, update: Dict[str, Any]):
log.info("Proccessing updateChatDraftMessage")
chat_id = update["chat_id"]
# FIXME: ignoring draft message itself for now because UI can't show it
@ -157,7 +171,7 @@ def update_chat_draft_msg(controller: Controller, update: Dict[str, Any]):
@update_handler("updateChatLastMessage")
def update_chat_last_msg(controller: Controller, update: Dict[str, Any]):
def update_chat_last_message(controller: Controller, update: Dict[str, Any]):
log.info("Proccessing updateChatLastMessage")
chat_id = update["chat_id"]
last_message = update.get("last_message")
@ -186,7 +200,7 @@ def update_chat_notification_settings(controller: Controller, update):
@update_handler("updateMessageSendSucceeded")
def update_msg_send_succeeded(controller: Controller, update):
def update_message_send_succeeded(controller: Controller, update):
chat_id = update["message"]["chat_id"]
msg_id = update["old_message_id"]
controller.model.msgs.add_message(chat_id, update["message"])
@ -230,7 +244,7 @@ def update_message_content_opened(
@update_handler("updateDeleteMessages")
def update_delete_msgs(controller: Controller, update: Dict[str, Any]):
def update_delete_messages(controller: Controller, update: Dict[str, Any]):
chat_id = update["chat_id"]
msg_ids = update["message_ids"]
for msg_id in msg_ids: