Add forward msg functionality and multiple msgs selection

This commit is contained in:
Paul Nameless 2020-05-21 21:55:57 +08:00
parent 3413d8fed8
commit a215b0bc65
5 changed files with 80 additions and 0 deletions

View file

@ -97,8 +97,49 @@ class Controller:
"I": self.write_long_msg,
"A": self.write_long_msg,
"bp": self.breakpoint,
" ": self.select_msg,
"^[": self.discard_selected_msgs, # esc
"y": self.copy_msgs,
"p": self.forward_msgs,
}
def forward_msgs(self, _: int):
chat_id = self.model.chats.id_by_index(self.model.current_chat)
if not self.model.yanked_msgs:
return
from_chat_id, msg_ids = self.model.yanked_msgs
self.tg.forward_msgs(chat_id, from_chat_id, msg_ids)
self.present_info(f"Forwarded {len(msg_ids)} messages")
def copy_msgs(self, _: int):
# can_be_forwarded
chat_id = self.model.chats.id_by_index(self.model.current_chat)
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(0)
self.present_info(f"Copied {len(msg_ids)} messages")
def select_msg(self, _: int):
chat_id = self.model.chats.id_by_index(self.model.current_chat)
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(1)
self.refresh_msgs()
self.present_info("Removed selections")
def discard_selected_msgs(self, _: int):
chat_id = self.model.chats.id_by_index(self.model.current_chat)
self.model.selected[chat_id] = []
self.refresh_msgs()
def jump_bottom(self, _: int):
log.info("jump_bottom:")
if self.model.jump_bottom():

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]] = None
def get_me(self):
return self.users.get_me()

View file

@ -187,3 +187,9 @@ class MsgProxy:
@property
def sender_id(self) -> int:
return self.msg["sender_user_id"]
@property
def forward(self) -> Optional[Dict[str, Any]]:
if "forward_info" not in self.msg:
return None
return self.msg["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,6 +282,7 @@ 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)