mirror of
https://github.com/paul-nameless/tg
synced 2024-11-22 11:53:08 +00:00
commit
8c48c26025
4 changed files with 69 additions and 9 deletions
|
@ -62,6 +62,7 @@ class Controller:
|
|||
"updateMessageSendSucceeded": self.update_msg_send_succeeded,
|
||||
"updateNewMessage": self.update_new_msg,
|
||||
"updateMessageContentOpened": self.update_message_content_opened,
|
||||
"updateChatReadOutbox": self.update_chat_read_outbox,
|
||||
}
|
||||
self.chat_size = 0.5
|
||||
signal(SIGWINCH, self.resize_handler)
|
||||
|
@ -471,6 +472,17 @@ class Controller:
|
|||
self.model.chats.update_chat(chat_id, is_pinned=is_pinned, order=order)
|
||||
self._refresh_current_chat(current_chat_id)
|
||||
|
||||
@handle_exception
|
||||
def update_chat_read_outbox(self, update: Dict[str, Any]):
|
||||
log.info("Proccessing updateChatReadOutbox")
|
||||
chat_id = update["chat_id"]
|
||||
last_read_outbox_message_id = update["last_read_outbox_message_id"]
|
||||
current_chat_id = self.model.chats.id_by_index(self.model.current_chat)
|
||||
self.model.chats.update_chat(
|
||||
chat_id, last_read_outbox_message_id=last_read_outbox_message_id,
|
||||
)
|
||||
self._refresh_current_chat(current_chat_id)
|
||||
|
||||
@handle_exception
|
||||
def update_chat_read_inbox(self, update: Dict[str, Any]):
|
||||
log.info("Proccessing updateChatReadInbox")
|
||||
|
|
|
@ -19,7 +19,7 @@ def run(tg: Telegram, stdscr: window) -> None:
|
|||
# run this function in thread?
|
||||
model = Model(tg)
|
||||
status_view = StatusView(stdscr)
|
||||
msg_view = MsgView(stdscr, model.msgs, model.users)
|
||||
msg_view = MsgView(stdscr, model.msgs, model, model.users)
|
||||
chat_view = ChatView(stdscr)
|
||||
view = View(stdscr, chat_view, msg_view, status_view)
|
||||
controller = Controller(model, view, tg)
|
||||
|
|
|
@ -11,6 +11,7 @@ log = logging.getLogger(__name__)
|
|||
|
||||
class Model:
|
||||
def __init__(self, tg: Telegram) -> None:
|
||||
self.tg = tg
|
||||
self.chats = ChatModel(tg)
|
||||
self.msgs = MsgModel(tg)
|
||||
self.users = UserModel(tg)
|
||||
|
@ -83,17 +84,29 @@ class Model:
|
|||
return True
|
||||
return False
|
||||
|
||||
def view_current_msg(self):
|
||||
chat_id = self.chats.id_by_index(self.current_chat)
|
||||
msg = MsgProxy(self.current_msg())
|
||||
msg_id = msg["id"]
|
||||
self.tg.view_messages(chat_id, [msg_id])
|
||||
|
||||
def next_msg(self, step: int = 1) -> bool:
|
||||
chat_id = self.chats.id_by_index(self.current_chat)
|
||||
if not chat_id:
|
||||
return False
|
||||
return self.msgs.next_msg(chat_id, step)
|
||||
is_next = self.msgs.next_msg(chat_id, step)
|
||||
if is_next:
|
||||
self.view_current_msg()
|
||||
return is_next
|
||||
|
||||
def prev_msg(self, step: int = 1) -> bool:
|
||||
chat_id = self.chats.id_by_index(self.current_chat)
|
||||
if not chat_id:
|
||||
return False
|
||||
return self.msgs.prev_msg(chat_id, step)
|
||||
is_prev = self.msgs.prev_msg(chat_id, step)
|
||||
if is_prev:
|
||||
self.view_current_msg()
|
||||
return is_prev
|
||||
|
||||
def get_chats(
|
||||
self,
|
||||
|
|
|
@ -4,8 +4,8 @@ from _curses import window # type: ignore
|
|||
from datetime import datetime
|
||||
from typing import Any, Dict, List, Optional, Tuple, cast
|
||||
|
||||
from tg.colors import blue, cyan, get_color, magenta, reverse, white
|
||||
from tg.models import MsgModel, UserModel
|
||||
from tg.colors import blue, cyan, get_color, magenta, reverse, white, yellow
|
||||
from tg.models import Model, MsgModel, UserModel
|
||||
from tg.msg import MsgProxy
|
||||
from tg.utils import emoji_pattern, num, truncate_to_len
|
||||
|
||||
|
@ -230,10 +230,12 @@ class MsgView:
|
|||
self,
|
||||
stdscr: window,
|
||||
msg_model: MsgModel,
|
||||
model: Model,
|
||||
users: UserModel,
|
||||
p: float = 0.5,
|
||||
) -> None:
|
||||
self.msg_model = msg_model
|
||||
self.model = model
|
||||
self.users = users
|
||||
self.stdscr = stdscr
|
||||
self.h = 0
|
||||
|
@ -241,6 +243,10 @@ class MsgView:
|
|||
self.x = 0
|
||||
self.win = self.stdscr.subwin(self.h, self.w, 0, self.x)
|
||||
self._refresh = self.win.refresh
|
||||
self.states = {
|
||||
"messageSendingStateFailed": "failed",
|
||||
"messageSendingStatePending": "pending",
|
||||
}
|
||||
|
||||
def resize(self, rows: int, cols: int, p: float = 0.5) -> None:
|
||||
self.h = rows - 1
|
||||
|
@ -249,6 +255,31 @@ class MsgView:
|
|||
self.win.resize(self.h, self.w)
|
||||
self.win.mvwin(0, self.x)
|
||||
|
||||
def _get_flags(self, msg_proxy: MsgProxy):
|
||||
flags = []
|
||||
chat = self.model.chats.chats[self.model.current_chat]
|
||||
|
||||
if (
|
||||
not self.model.is_me(msg_proxy.sender_id)
|
||||
and msg_proxy.msg_id > chat["last_read_inbox_message_id"]
|
||||
):
|
||||
flags.append("new")
|
||||
elif (
|
||||
self.model.is_me(msg_proxy.sender_id)
|
||||
and msg_proxy.msg_id > chat["last_read_outbox_message_id"]
|
||||
):
|
||||
if not self.model.is_me(chat["id"]):
|
||||
flags.append("unseen")
|
||||
if state := msg_proxy.msg.get("sending_state"):
|
||||
log.info("state: %s", state)
|
||||
state_type = state["@type"]
|
||||
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)
|
||||
|
||||
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))
|
||||
if reply_msg_content := self._parse_msg(reply_msg):
|
||||
|
@ -297,15 +328,18 @@ class MsgView:
|
|||
|
||||
msg = self._format_msg(msg_proxy, user_id_item)
|
||||
user_id = self._get_user_by_id(user_id_item)
|
||||
|
||||
# count wide character utf-8 symbols that take > 1 bytes to
|
||||
# print it causes invalid offset
|
||||
label_elements = f" {dt} ", user_id
|
||||
flags = self._get_flags(msg_proxy)
|
||||
if user_id and flags:
|
||||
# if not channel add space between name and flags
|
||||
flags = " " + flags
|
||||
label_elements = f" {dt} ", user_id, flags
|
||||
label_len = sum(len(e) for e in label_elements)
|
||||
elements = *label_elements, f" {msg}"
|
||||
|
||||
needed_lines = 0
|
||||
for i, msg_line in enumerate(msg.split("\n")):
|
||||
# count wide character utf-8 symbols that take > 1 bytes to
|
||||
# print it causes invalid offset
|
||||
emojies_count = sum(
|
||||
map(len, emoji_pattern.findall(msg_line))
|
||||
)
|
||||
|
@ -361,6 +395,7 @@ class MsgView:
|
|||
attrs = (
|
||||
get_color(cyan, -1),
|
||||
get_color(blue, -1),
|
||||
get_color(yellow, -1),
|
||||
get_color(white, -1),
|
||||
)
|
||||
|
||||
|
|
Loading…
Reference in a new issue