Merge pull request #52 from paul-nameless/forward

Add forward msg functionality and multiple msgs selection
This commit is contained in:
Nameless 2020-05-22 14:15:40 +08:00 committed by GitHub
commit c435ed4264
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 86 additions and 1 deletions

View file

@ -97,8 +97,56 @@ class Controller:
"I": lambda _: self.write_long_msg(),
"A": lambda _: self.write_long_msg(),
"bp": lambda _: self.breakpoint(),
" ": lambda _: self.toggle_select_msg(),
"^[": lambda _: self.discard_selected_msgs(), # esc
"y": lambda _: self.copy_msgs(),
"p": lambda _: self.forward_msgs(),
}
def forward_msgs(self):
# TODO: check <can_be_forwarded> flag
chat_id = self.model.chats.id_by_index(self.model.current_chat)
if not chat_id:
return
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.present_info(f"Forwarded {len(msg_ids)} messages")
def copy_msgs(self):
chat_id = self.model.chats.id_by_index(self.model.current_chat)
if not chat_id:
return
msg_ids = self.model.selected[chat_id]
if not msg_ids:
self.present_error("No msgs selected")
return
self.model.yanked_msgs = (chat_id, msg_ids)
self.discard_selected_msgs()
self.present_info(f"Copied {len(msg_ids)} messages")
def toggle_select_msg(self):
chat_id = self.model.chats.id_by_index(self.model.current_chat)
if not chat_id:
return
msg = MsgProxy(self.model.current_msg)
if msg.msg_id in self.model.selected[chat_id]:
self.model.selected[chat_id].remove(msg.msg_id)
else:
self.model.selected[chat_id].append(msg.msg_id)
self.model.next_msg()
self.refresh_msgs()
def discard_selected_msgs(self):
chat_id = self.model.chats.id_by_index(self.model.current_chat)
if not chat_id:
return
self.model.selected[chat_id] = []
self.refresh_msgs()
self.present_info("Discarded selected messages")
def jump_bottom(self):
if self.model.jump_bottom():
self.refresh_msgs()

View file

@ -16,6 +16,8 @@ class Model:
self.users = UserModel(tg)
self.current_chat = 0
self.downloads: Dict[int, Tuple[int, int]] = {}
self.selected: Dict[int, List[int]] = defaultdict(list)
self.yanked_msgs: Tuple[int, List[int]] = (0, [])
def get_me(self):
return self.users.get_me()

View file

@ -185,3 +185,7 @@ class MsgProxy:
@property
def sender_id(self) -> int:
return self.msg["sender_user_id"]
@property
def forward(self) -> Optional[Dict[str, Any]]:
return self.msg.get("forward_info")

View file

@ -1,3 +1,5 @@
from typing import Any, Dict, List
from telegram.client import AsyncResult, Telegram
@ -149,3 +151,25 @@ class Tdlib(Telegram):
"message_id": message_id,
}
return self._send_data(data)
def forward_msgs(
self,
chat_id: int,
from_chat_id: int,
message_ids: List[int],
as_album: bool = False,
send_copy: bool = False,
remove_caption: bool = False,
options: Dict[str, Any] = {},
) -> AsyncResult:
data = {
"@type": "forwardMessages",
"chat_id": chat_id,
"from_chat_id": from_chat_id,
"message_ids": message_ids,
"as_album": as_album,
"send_copy": send_copy,
"remove_caption": remove_caption,
"options": options,
}
return self._send_data(data)

View file

@ -259,6 +259,12 @@ class MsgView:
flags = []
chat = self.model.chats.chats[self.model.current_chat]
if msg_proxy.msg_id in self.model.selected[chat["id"]]:
flags.append("selected")
if msg_proxy.forward is not None:
flags.append("forwarded")
if (
not self.model.is_me(msg_proxy.sender_id)
and msg_proxy.msg_id > chat["last_read_inbox_message_id"]
@ -276,9 +282,10 @@ class MsgView:
flags.append(self.states.get(state_type, state_type))
if msg_proxy.msg["edit_date"]:
flags.append("edited")
if not flags:
return ""
return ",".join(flags)
return " ".join(flags)
def _format_reply_msg(self, chat_id: int, msg: str, reply_to: int) -> str:
reply_msg = MsgProxy(self.msg_model.get_message(chat_id, reply_to))