Merge branch 'master' into copy-msg

This commit is contained in:
Paul Nameless 2020-05-29 17:16:45 +08:00
commit c61180f389
4 changed files with 71 additions and 5 deletions

View file

@ -39,7 +39,7 @@ else:
) )
# TODO: use mailcap instead of editor # TODO: use mailcap instead of editor
LONG_MSG_CMD = "vim -c 'startinsert' {file_path}" LONG_MSG_CMD = "vim + -c 'startinsert' {file_path}"
EDITOR = os.environ.get("EDITOR", "vi") EDITOR = os.environ.get("EDITOR", "vi")
if _os_name == _linux: if _os_name == _linux:
@ -56,6 +56,5 @@ else:
if os.path.isfile(DEFAULT_CONFIG): if os.path.isfile(DEFAULT_CONFIG):
config_params = runpy.run_path(DEFAULT_CONFIG) config_params = runpy.run_path(DEFAULT_CONFIG)
for param, value in config_params.items(): for param, value in config_params.items():
var = param.upper() if param.isupper():
if var in globals(): globals()[param] = value
globals()[var] = value

View file

@ -30,7 +30,7 @@ log = logging.getLogger(__name__)
# be removed from the display in order to achive scroll threshold. this could # be removed from the display in order to achive scroll threshold. this could
# cause blan areas on the msg display screen # cause blan areas on the msg display screen
MSGS_LEFT_SCROLL_THRESHOLD = 2 MSGS_LEFT_SCROLL_THRESHOLD = 2
REPLY_MSG_PREFIX = "# >"
key_bind_handler_type = Callable[[Any], Any] key_bind_handler_type = Callable[[Any], Any]
@ -107,6 +107,8 @@ class Controller:
"p": lambda _: self.forward_msgs(), "p": lambda _: self.forward_msgs(),
"y": lambda _: self.copy_msgs(), "y": lambda _: self.copy_msgs(),
"c": lambda _: self.copy_msg_text(), "c": lambda _: self.copy_msg_text(),
"r": lambda _: self.reply_message(),
"R": lambda _: self.reply_with_long_message(),
# message selection # message selection
" ": lambda _: self.toggle_select_msg(), # space " ": lambda _: self.toggle_select_msg(), # space
"^G": lambda _: self.discard_selected_msgs(), "^G": lambda _: self.discard_selected_msgs(),
@ -236,6 +238,29 @@ class Controller:
with suspend(self.view): with suspend(self.view):
breakpoint() breakpoint()
def reply_message(self):
# write new message
if msg := self.view.status.get_input():
self.model.reply_message(text=msg)
self.present_info("Message reply sent")
else:
self.present_info("Message reply wasn't sent")
def reply_with_long_message(self):
msg = MsgProxy(self.model.current_msg)
with NamedTemporaryFile("w+", suffix=".txt") as f, suspend(
self.view
) as s:
f.write(insert_replied_msg(msg))
f.seek(0)
s.call(config.LONG_MSG_CMD.format(file_path=f.name))
with open(f.name) as f:
if msg := strip_replied_msg(f.read().strip()):
self.model.reply_message(text=msg)
self.present_info("Message sent")
else:
self.present_info("Message wasn't sent")
def write_short_msg(self): def write_short_msg(self):
# write new message # write new message
if msg := self.view.status.get_input(): if msg := self.view.status.get_input():
@ -474,3 +499,22 @@ class Controller:
self.model.current_chat = i self.model.current_chat = i
break break
self.render() self.render()
def insert_replied_msg(msg: MsgProxy) -> str:
text = msg.text_content if msg.is_text else msg.content_type
return (
"\n".join([f"{REPLY_MSG_PREFIX} {line}" for line in text.split("\n")])
# adding line with whitespace so text editor could start editing from last line
+ "\n "
)
def strip_replied_msg(msg: str) -> str:
return "\n".join(
[
line
for line in msg.split("\n")
if not line.startswith(REPLY_MSG_PREFIX)
]
)

View file

@ -120,6 +120,14 @@ class Model:
limit = offset + page_size limit = offset + page_size
return self.chats.fetch_chats(offset=offset, limit=limit) return self.chats.fetch_chats(offset=offset, limit=limit)
def reply_message(self, text: str) -> bool:
chat_id = self.chats.id_by_index(self.current_chat)
if chat_id is None:
return False
reply_to_msg = self.current_msg_id
self.tg.reply_message(chat_id, reply_to_msg, text)
return True
def send_message(self, text: str) -> bool: def send_message(self, text: str) -> bool:
chat_id = self.chats.id_by_index(self.current_chat) chat_id = self.chats.id_by_index(self.current_chat)
if chat_id is None: if chat_id is None:

View file

@ -20,6 +20,21 @@ class Tdlib(Telegram):
) )
result.wait() result.wait()
def reply_message(
self, chat_id: int, reply_to_message_id: int, text: str
) -> AsyncResult:
data = {
"@type": "sendMessage",
"chat_id": chat_id,
"reply_to_message_id": reply_to_message_id,
"input_message_content": {
"@type": "inputMessageText",
"text": {"@type": "formattedText", "text": text},
},
}
return self._send_data(data)
def send_doc(self, file_path: str, chat_id: int) -> AsyncResult: def send_doc(self, file_path: str, chat_id: int) -> AsyncResult:
data = { data = {
"@type": "sendMessage", "@type": "sendMessage",