From 801df444f121accf0c08e43a09ee3b25d82c1647 Mon Sep 17 00:00:00 2001 From: Paul Nameless Date: Wed, 24 Jun 2020 21:18:36 +0800 Subject: [PATCH 1/2] Show unseen flag in chat pane --- README.md | 3 +++ tg/main.py | 4 ++-- tg/utils.py | 2 +- tg/views.py | 31 +++++++++++++++++-------------- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 8f10789..2766674 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,10 @@ CHAT_FLAGS = { "online": "●", "pinned": "P", "muted": "M", + # chat is marked as unread "unread": "U", + # last msg haven't been seen by recipient + "unseen": "✓", } MSG_FLAGS = { "selected": "*", diff --git a/tg/main.py b/tg/main.py index 1c6b02b..05ac374 100644 --- a/tg/main.py +++ b/tg/main.py @@ -25,8 +25,8 @@ def run(tg: Tdlib, stdscr: window) -> None: model = Model(tg) status_view = StatusView(stdscr) - msg_view = MsgView(stdscr, model.msgs, model, model.users) - chat_view = ChatView(stdscr, model.users) + msg_view = MsgView(stdscr, model) + chat_view = ChatView(stdscr, model) view = View(stdscr, chat_view, msg_view, status_view) controller = Controller(model, view, tg) diff --git a/tg/utils.py b/tg/utils.py index 47a9a7d..c27f1da 100644 --- a/tg/utils.py +++ b/tg/utils.py @@ -24,7 +24,7 @@ emoji_pattern = re.compile( "\U0001F300-\U0001F5FF" # symbols & pictographs "\U0001F680-\U0001F6FF" # transport & map symbols "\U0001F1E0-\U0001F1FF" # flags (iOS) - "\U00002702-\U000027B0" + # "\U00002702-\U000027B0" # "\U000024C2-\U0001F251" "]+", flags=re.UNICODE, diff --git a/tg/views.py b/tg/views.py index 6719bc1..e7914f8 100644 --- a/tg/views.py +++ b/tg/views.py @@ -126,13 +126,13 @@ class StatusView: class ChatView: - def __init__(self, stdscr: window, users: UserModel, p: float = 0.5): + def __init__(self, stdscr: window, model: Model): self.stdscr = stdscr self.h = 0 self.w = 0 self.win = stdscr.subwin(self.h, self.w, 0, 0) self._refresh = self.win.refresh - self.users = users + self.model = model def resize(self, rows: int, cols: int, p: float = 0.25) -> None: self.h = rows - 1 @@ -214,7 +214,17 @@ class ChatView: ) -> str: flags = [] - if self.users.is_online(chat["id"]): + msg = chat.get("last_message") + if ( + msg + and self.model.is_me(msg["sender_user_id"]) + and msg["id"] > chat["last_read_outbox_message_id"] + and not self.model.is_me(chat["id"]) + ): + # last msg haven't been seen by recipient + flags.append("unseen") + + if self.model.users.is_online(chat["id"]): flags.append("online") if is_pinned: @@ -234,16 +244,9 @@ class ChatView: class MsgView: def __init__( - self, - stdscr: window, - msg_model: MsgModel, - model: Model, - users: UserModel, - p: float = 0.5, - ) -> None: - self.msg_model = msg_model + self, stdscr: window, model: Model, + ): self.model = model - self.users = users self.stdscr = stdscr self.h = 0 self.w = 0 @@ -297,7 +300,7 @@ class MsgView: def _format_reply_msg( self, chat_id: int, msg: str, reply_to: int, width_limit: int ) -> str: - _msg = self.msg_model.get_message(chat_id, reply_to) + _msg = self.model.msgs.get_message(chat_id, reply_to) if not _msg: return msg reply_msg = MsgProxy(_msg) @@ -473,7 +476,7 @@ class MsgView: def _get_user_by_id(self, user_id: int) -> str: if user_id == 0: return "" - user = self.users.get_user(user_id) + user = self.model.users.get_user(user_id) if user["first_name"] and user["last_name"]: return f'{user["first_name"]} {user["last_name"]}'[:20] From 2f53e6edae71b50563ba792899a2269608bb5ceb Mon Sep 17 00:00:00 2001 From: Paul Nameless Date: Fri, 26 Jun 2020 10:30:51 +0800 Subject: [PATCH 2/2] Add type hints for remove_message function --- tg/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tg/models.py b/tg/models.py index f6fbfee..c46fe89 100644 --- a/tg/models.py +++ b/tg/models.py @@ -323,7 +323,7 @@ class MsgModel: return result.update return next(iter(m for m in self.msgs[chat_id] if m["id"] == msg_id)) - def remove_message(self, chat_id, msg_id): + def remove_message(self, chat_id: int, msg_id: int): msg_set = self.msg_ids[chat_id] if msg_id not in msg_set: return False