This commit is contained in:
Alexander Zveruk 2020-05-20 22:41:48 +03:00
parent 2f68cfe556
commit 8feda11051
4 changed files with 79 additions and 20 deletions

View file

@ -8,6 +8,7 @@ DEFAULT_FILES = os.path.expanduser("~/.cache/tg/")
max_download_size = "10MB"
record_cmd = None
long_msg_cmd = "vim -c 'startinsert' {file_path}"
editor = os.environ.get("EDITOR", "vi")
def get_cfg(config=DEFAULT_CONFIG):

View file

@ -92,7 +92,7 @@ class Controller:
log.exception("Error happened in main loop")
def download_current_file(self):
msg = MsgProxy(self.model.current_msg())
msg = MsgProxy(self.model.current_msg)
log.debug("Downloading msg: %s", msg.msg)
file_id = msg.file_id
if file_id:
@ -105,12 +105,11 @@ class Controller:
log.info("Downloaded: file_id=%s", file_id)
def open_current_msg(self):
msg = MsgProxy(self.model.current_msg())
msg = MsgProxy(self.model.current_msg)
log.info("Open msg: %s", msg.msg)
if msg.is_text:
text = msg["content"]["text"]["text"]
with NamedTemporaryFile("w", suffix=".txt") as f:
f.write(text)
f.write(msg.text_content)
f.flush()
with suspend(self.view) as s:
s.open_file(f.name)
@ -122,19 +121,38 @@ class Controller:
log.info("Opening file: %s", path)
s.open_file(path)
def write_long_msg(self):
file_path = "/tmp/tg-msg.txt"
with suspend(self.view) as s:
s.call(config.long_msg_cmd.format(file_path=file_path))
if not os.path.isfile(file_path):
def edit_msg(self):
msg = MsgProxy(self.model.current_msg)
log.info("Editing msg: %s", msg.msg)
if not self.model.is_me(msg.sender_id):
log.warning("Can't edit other user message")
return
with open(file_path) as f:
msg = f.read().strip()
os.remove(file_path)
if msg:
self.model.send_message(text=msg)
with self.lock:
self.view.status.draw("Message sent")
if not msg.is_text:
log.warning("Can't edit not text message")
return
with NamedTemporaryFile("r+", suffix=".txt") as f, suspend(
self.view
) as s:
f.write(msg.text_content)
f.flush()
s.call(f"{config.editor} {f.name}")
f.seek(0)
if msg := f.read().strip():
self.model.edit_message(text=msg)
with self.lock:
self.view.status.draw("Message edited")
def write_long_msg(self):
with NamedTemporaryFile("r+", suffix=".txt") as f, suspend(
self.view
) as s:
s.call(config.long_msg_cmd.format(file_path=f.name))
f.seek(0)
if msg := f.read().strip():
self.model.send_message(text=msg)
with self.lock:
self.view.status.draw("Message sent")
def resize_handler(self, signum, frame):
curses.endwin()
@ -221,16 +239,19 @@ class Controller:
elif keys == "/":
# search
pass
elif keys == "gg":
# move to the top
pass
elif keys == "e":
# edit msg
pass
self.edit_msg()
elif keys == "r":
# reply to this msg
# print to status line
pass
elif keys in ("i", "a"):
# write new message
msg = self.view.status.get_input()
@ -386,7 +407,7 @@ class Controller:
return
# notify
if msg.sender_id == self.model.get_me()["id"]:
if self.model.is_me(msg["sender_user_id"]):
return
user = self.model.users.get_user(msg.sender_id)
name = f"{user['first_name']} {user['last_name']}"

View file

@ -108,6 +108,18 @@ class TelegramApi(Telegram):
}
return self._send_data(data)
def edit_message(self, chat_id: int, message_id: int, msg: str):
data = {
"@type": "editMessageText",
"message_id": message_id,
"chat_id": chat_id,
"input_message_content": {
"@type": "inputMessageText",
"text": {"@type": "formattedText", "text": msg},
},
}
return self._send_data(data)
def toggle_chat_is_marked_as_unread(
self, chat_id: int, is_marked_as_unread: bool
):

View file

@ -18,6 +18,9 @@ class Model:
def get_me(self):
return self.users.get_me()
def is_me(self, user_id: int) -> bool:
return self.get_me()["id"] == user_id
def get_user(self, user_id):
return self.users.get_user(user_id)
@ -42,7 +45,8 @@ class Model:
limit = offset + page_size
return self.msgs.fetch_msgs(chat_id, offset=offset, limit=limit)
def current_msg(self):
@property
def current_msg(self) -> Dict[str, Any]:
chat_id = self.chats.id_by_index(self.current_chat)
if chat_id is None:
return {}
@ -50,6 +54,10 @@ class Model:
log.info("current-msg: %s", current_msg)
return self.msgs.msgs[chat_id][current_msg]
@property
def current_msg_id(self) -> int:
return self.current_msg["id"]
def jump_bottom(self):
chat_id = self.chats.id_by_index(self.current_chat)
return self.msgs.jump_bottom(chat_id)
@ -103,6 +111,11 @@ class Model:
self.msgs.send_message(chat_id, text)
return True
def edit_message(self, text: str) -> bool:
if chat_id := self.chats.id_by_index(self.current_chat):
return self.msgs.edit_message(chat_id, self.current_msg_id, text)
return False
def delete_msg(self) -> bool:
chat_id = self.chats.id_by_index(self.current_chat)
if chat_id:
@ -322,6 +335,18 @@ class MsgModel:
if i < len(self.msgs[chat_id])
]
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.wait()
if result.error:
log.info(f"send message error: {result.error_info}")
return False
else:
log.info(f"message has been sent: {result.update}")
return True
def send_message(self, chat_id: int, text: str) -> None:
log.info("Sending msg")
result = self.tg.send_message(chat_id=chat_id, text=text)