Add u: read/unread, r: read all chat msgs, p: pin/unpin, m: mute/unmute (#36)

* Add u: read/unread, r: read all chat msgs, p: pin/unpin, m: mute/unmute

Co-authored-by: Paul Nameless <reacsdas@gmail.com>
Co-authored-by: Alexander Zveruk <sanyazveruk@gmail.com>
This commit is contained in:
Nameless 2020-05-16 17:33:42 +08:00 committed by GitHub
parent 20c8b03d65
commit f23596a0d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 92 additions and 8 deletions

View file

@ -1,9 +1,9 @@
import logging import logging
from typing import Dict, Any, Optional
import os import os
import threading import threading
from datetime import datetime from datetime import datetime
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
from typing import Any, Dict, Optional
from telegram.client import Telegram from telegram.client import Telegram
@ -47,17 +47,18 @@ class Controller:
self.lock = threading.Lock() self.lock = threading.Lock()
self.tg = tg self.tg = tg
self.handlers = { self.handlers = {
"updateNewMessage": self.update_new_msg, "updateChatDraftMessage": self.update_chat_draft_msg,
"updateMessageContent": self.update_msg_content,
"updateChatIsPinned": self.update_chat_is_pinned,
"updateChatIsMarkedAsUnread": self.update_chat_marked_as_unread, "updateChatIsMarkedAsUnread": self.update_chat_marked_as_unread,
"updateChatIsPinned": self.update_chat_is_pinned,
"updateChatLastMessage": self.update_chat_last_msg,
"updateChatNotificationSettings": self.update_chat_notification_settings,
"updateChatOrder": self.update_chat_order,
"updateChatReadInbox": self.update_chat_read_inbox, "updateChatReadInbox": self.update_chat_read_inbox,
"updateChatTitle": self.update_chat_title, "updateChatTitle": self.update_chat_title,
"updateChatLastMessage": self.update_chat_last_msg,
"updateChatDraftMessage": self.update_chat_draft_msg,
"updateChatOrder": self.update_chat_order,
"updateMessageSendSucceeded": self.update_msg_send_succeeded,
"updateFile": self.update_file, "updateFile": self.update_file,
"updateMessageContent": self.update_msg_content,
"updateMessageSendSucceeded": self.update_msg_send_succeeded,
"updateNewMessage": self.update_new_msg,
} }
def send_file(self, send_file_fun, *args, **kwargs): def send_file(self, send_file_fun, *args, **kwargs):
@ -258,6 +259,39 @@ class Controller:
with suspend(self.view): with suspend(self.view):
breakpoint() breakpoint()
elif keys == "u":
chat = self.model.chats.chats[self.model.current_chat]
chat_id = chat["id"]
toggle = not chat["is_marked_as_unread"]
self.tg.toggle_chat_is_marked_as_unread(chat_id, toggle)
elif keys == "p":
chat = self.model.chats.chats[self.model.current_chat]
chat_id = chat["id"]
toggle = not chat["is_pinned"]
self.tg.toggle_chat_is_pinned(chat_id, toggle)
elif keys == "r":
chat = self.model.chats.chats[self.model.current_chat]
chat_id = chat["id"]
msg_id = chat["last_message"]["id"]
self.tg.view_messages(chat_id, [msg_id])
elif keys == "m":
# TODO: if it's msg to yourself, do not change its
# notification setting, because we can't by documentation,
# instead write about it in status
chat = self.model.chats.chats[self.model.current_chat]
chat_id = chat["id"]
notification_settings = chat["notification_settings"]
if notification_settings["mute_for"]:
notification_settings["mute_for"] = 0
else:
notification_settings["mute_for"] = 2147483647
self.tg.set_chat_nottification_settings(
chat_id, notification_settings
)
def refresh_chats(self) -> None: def refresh_chats(self) -> None:
with self.lock: with self.lock:
# using lock here, because refresh_chats is used from another # using lock here, because refresh_chats is used from another
@ -427,6 +461,16 @@ class Controller:
break break
self.refresh_chats() self.refresh_chats()
@handle_exception
def update_chat_notification_settings(self, update):
log.info("Proccessing update_chat_notification_settings")
chat_id = update["chat_id"]
notification_settings = update["notification_settings"]
self.model.chats.update_chat(
chat_id, notification_settings=notification_settings
)
self.refresh_chats()
@handle_exception @handle_exception
def update_msg_send_succeeded(self, update): def update_msg_send_succeeded(self, update):
chat_id = update["message"]["chat_id"] chat_id = update["message"]["chat_id"]

View file

@ -106,6 +106,45 @@ class TelegramApi(Telegram):
} }
return self._send_data(data) return self._send_data(data)
def toggle_chat_is_marked_as_unread(
self, chat_id: int, is_marked_as_unread: bool
):
data = {
"@type": "toggleChatIsMarkedAsUnread",
"chat_id": chat_id,
"is_marked_as_unread": is_marked_as_unread,
}
return self._send_data(data)
def toggle_chat_is_pinned(self, chat_id: int, is_pinned: bool):
data = {
"@type": "toggleChatIsPinned",
"chat_id": chat_id,
"is_pinned": is_pinned,
}
return self._send_data(data)
def set_chat_nottification_settings(
self, chat_id: int, notification_settings: dict
):
data = {
"@type": "setChatNotificationSettings",
"chat_id": chat_id,
"notification_settings": notification_settings,
}
return self._send_data(data)
def view_messages(
self, chat_id: int, message_ids: list, force_read: bool = True
):
data = {
"@type": "viewMessages",
"chat_id": chat_id,
"message_ids": message_ids,
"force_read": force_read,
}
return self._send_data(data)
def main(): def main():
def signal_handler(sig, frame): def signal_handler(sig, frame):
@ -130,6 +169,7 @@ def main():
) )
config.record_cmd = cfg.get("record_cmd") config.record_cmd = cfg.get("record_cmd")
tg.login() tg.login()
wrapper(partial(run, tg)) wrapper(partial(run, tg))