mirror of
https://github.com/paul-nameless/tg
synced 2024-11-25 13:20:25 +00:00
Add checks for sending, deleting, forwarding (#72)
* Check if msg can be deleted * Check if msg can be sent in current chat * Fix is can be deleted * Check is msg can be forwarded
This commit is contained in:
parent
29585ae972
commit
468950bc6a
2 changed files with 61 additions and 22 deletions
|
@ -129,16 +129,10 @@ class Controller:
|
|||
self.present_info("Copied msg")
|
||||
|
||||
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:
|
||||
if not self.model.forward_msgs():
|
||||
self.present_error("Can't forward msg(s)")
|
||||
return
|
||||
from_chat_id, msg_ids = self.model.yanked_msgs
|
||||
if not msg_ids:
|
||||
return
|
||||
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, [])
|
||||
self.present_info("Forwarded msg(s)")
|
||||
|
||||
def copy_msgs(self):
|
||||
chat_id = self.model.chats.id_by_index(self.model.current_chat)
|
||||
|
@ -239,8 +233,14 @@ class Controller:
|
|||
with suspend(self.view):
|
||||
breakpoint()
|
||||
|
||||
def can_send_msg(self) -> bool:
|
||||
chat = self.model.chats.chats[self.model.current_chat]
|
||||
return chat["permissions"]["can_send_messages"]
|
||||
|
||||
def reply_message(self):
|
||||
# write new message
|
||||
if not self.can_send_msg():
|
||||
self.present_info("Can't send msg in this chat")
|
||||
return
|
||||
chat_id = self.model.current_chat_id
|
||||
reply_to_msg = self.model.current_msg_id
|
||||
if msg := self.view.status.get_input():
|
||||
|
@ -250,6 +250,9 @@ class Controller:
|
|||
self.present_info("Message reply wasn't sent")
|
||||
|
||||
def reply_with_long_message(self):
|
||||
if not self.can_send_msg():
|
||||
self.present_info("Can't send msg in this chat")
|
||||
return
|
||||
chat_id = self.model.current_chat_id
|
||||
reply_to_msg = self.model.current_msg_id
|
||||
msg = MsgProxy(self.model.current_msg)
|
||||
|
@ -267,13 +270,28 @@ class Controller:
|
|||
self.present_info("Message wasn't sent")
|
||||
|
||||
def write_short_msg(self):
|
||||
# write new message
|
||||
if not self.can_send_msg():
|
||||
self.present_info("Can't send msg in this chat")
|
||||
return
|
||||
if msg := self.view.status.get_input():
|
||||
self.model.send_message(text=msg)
|
||||
self.present_info("Message sent")
|
||||
else:
|
||||
self.present_info("Message wasn't sent")
|
||||
|
||||
def write_long_msg(self):
|
||||
if not self.can_send_msg():
|
||||
self.present_info("Can't send msg in this chat")
|
||||
return
|
||||
with NamedTemporaryFile("r+", suffix=".txt") as f, suspend(
|
||||
self.view
|
||||
) as s:
|
||||
s.call(config.LONG_MSG_CMD.format(file_path=f.name))
|
||||
with open(f.name) as f:
|
||||
if msg := f.read().strip():
|
||||
self.model.send_message(text=msg)
|
||||
self.present_info("Message sent")
|
||||
|
||||
def send_video(self):
|
||||
file_path = self.view.status.get_input()
|
||||
if not file_path or not os.path.isfile(file_path):
|
||||
|
@ -286,8 +304,11 @@ class Controller:
|
|||
self.tg.send_video(file_path, chat_id, width, height, duration)
|
||||
|
||||
def delete_msgs(self):
|
||||
self.model.delete_msgs()
|
||||
is_deleted = self.model.delete_msgs()
|
||||
self.discard_selected_msgs()
|
||||
if not is_deleted:
|
||||
self.present_error("Can't delete msg(s)")
|
||||
return
|
||||
self.present_info("Message deleted")
|
||||
|
||||
def send_file(self, send_file_fun, *args, **kwargs):
|
||||
|
@ -390,16 +411,6 @@ class Controller:
|
|||
self.model.edit_message(text=text)
|
||||
self.present_info("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))
|
||||
with open(f.name) as f:
|
||||
if msg := f.read().strip():
|
||||
self.model.send_message(text=msg)
|
||||
self.present_info("Message sent")
|
||||
|
||||
def run(self) -> None:
|
||||
try:
|
||||
self.handle(self.chat_bindings, 0.5)
|
||||
|
|
28
tg/models.py
28
tg/models.py
|
@ -136,6 +136,11 @@ class Model:
|
|||
return self.msgs.edit_message(chat_id, self.current_msg_id, text)
|
||||
return False
|
||||
|
||||
def can_be_deleted(self, chat_id: int, msg: Dict[str, Any]) -> bool:
|
||||
if chat_id == msg["sender_user_id"]:
|
||||
return msg["can_be_deleted_only_for_self"]
|
||||
return msg["can_be_deleted_for_all_users"]
|
||||
|
||||
def delete_msgs(self) -> bool:
|
||||
chat_id = self.chats.id_by_index(self.current_chat)
|
||||
if not chat_id:
|
||||
|
@ -143,13 +148,36 @@ class Model:
|
|||
msg_ids = self.selected[chat_id]
|
||||
if msg_ids:
|
||||
message_ids = msg_ids
|
||||
for msg_id in message_ids:
|
||||
msg = self.msgs.get_message(chat_id, msg_id)
|
||||
if not self.can_be_deleted(chat_id, msg):
|
||||
return False
|
||||
else:
|
||||
selected_msg = self.msgs.current_msgs[chat_id]
|
||||
msg = self.msgs.msgs[chat_id][selected_msg]
|
||||
if not self.can_be_deleted(chat_id, msg):
|
||||
return False
|
||||
message_ids = [msg["id"]]
|
||||
|
||||
log.info(f"Deleting msg from the chat {chat_id}: {message_ids}")
|
||||
self.tg.delete_messages(chat_id, message_ids, revoke=True)
|
||||
return True
|
||||
|
||||
def forward_msgs(self) -> bool:
|
||||
chat_id = self.chats.id_by_index(self.current_chat)
|
||||
if not chat_id:
|
||||
return False
|
||||
from_chat_id, msg_ids = self.yanked_msgs
|
||||
if not msg_ids:
|
||||
return False
|
||||
for msg_id in msg_ids:
|
||||
msg = self.msgs.get_message(from_chat_id, msg_id)
|
||||
if not msg["can_be_forwarded"]:
|
||||
return False
|
||||
|
||||
self.tg.forward_messages(chat_id, from_chat_id, msg_ids)
|
||||
self.yanked_msgs = (0, [])
|
||||
return True
|
||||
|
||||
|
||||
class ChatModel:
|
||||
|
|
Loading…
Reference in a new issue