Merge pull request #11 from paul-nameless/correcty-handle-sending-msgs

Correcty handle sending msgs
This commit is contained in:
Nameless 2020-05-04 14:29:39 +08:00 committed by GitHub
commit a79dd4b4d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 9 deletions

View file

@ -6,7 +6,7 @@ from utils import notify
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
SUPPORTED_MSG_TYPES = "updateNewMessage", "updateChatLastMessage" SUPPORTED_MSG_TYPES = "updateNewMessage", "updateChatLastMessage", "updateMessageSendSucceeded"
class Controller: class Controller:
@ -148,9 +148,9 @@ class Controller:
_type = update['@type'] _type = update['@type']
log.info('===Received %s type: %s', _type, update) log.info('===Received %s type: %s', _type, update)
if _type == 'updateNewMessage': if _type == 'updateNewMessage':
# with self.lock(): # with self.lock:
chat_id = update['message']['chat_id'] chat_id = update['message']['chat_id']
self.model.msgs.msgs[chat_id].append(update['message']) self.model.msgs.add_message(chat_id, update['message'])
# msgs = self.model.get_current_chat_msg() # msgs = self.model.get_current_chat_msg()
self.refresh_msgs() self.refresh_msgs()
if not update.get('disable_notification'): if not update.get('disable_notification'):
@ -162,7 +162,12 @@ class Controller:
message = update['last_message'] message = update['last_message']
self.model.chats.update_last_message(chat_id, message) self.model.chats.update_last_message(chat_id, message)
self.refresh_chats() self.refresh_chats()
elif _type == "updateMessageSendSucceeded":
chat_id = update['message']['chat_id']
msg_id = update['old_message_id']
self.model.msgs.add_message(chat_id, update['message'])
self.model.msgs.remove_message(chat_id, msg_id)
self.refresh_msgs()
except Exception: except Exception:
log.exception("Error happened in update_handler") log.exception("Error happened in update_handler")
# message_content = update['message']['content'].get('text', {}) # message_content = update['message']['content'].get('text', {})

View file

@ -173,6 +173,7 @@ class MsgModel:
self.tg = tg self.tg = tg
self.msgs = defaultdict(list) # Dict[int, list] self.msgs = defaultdict(list) # Dict[int, list]
self.current_msgs = defaultdict(int) self.current_msgs = defaultdict(int)
self.msg_ids = defaultdict(set)
def next_msg(self, chat_id, step=1): def next_msg(self, chat_id, step=1):
current_msg = self.current_msgs[chat_id] current_msg = self.current_msgs[chat_id]
@ -200,6 +201,29 @@ class MsgModel:
return True return True
return False return False
def remove_message(self, chat_id, msg_id):
msg_set = self.msg_ids[chat_id]
if msg_id not in msg_set:
return False
log.info(f"removing msg {msg_id=}")
# FIXME: potential bottleneck, replace with constan time operation
self.msgs[chat_id] = [m for m in self.msgs[chat_id] if m['id'] != msg_id]
msg_set.remove(msg_id)
return True
def add_message(self, chat_id, message):
msg_id = message['id']
msg_set = self.msg_ids[chat_id]
if msg_id in msg_set:
return False
log.info(f"adding {msg_id=} {message}")
self.msgs[chat_id].append(message)
msg_set.add(msg_id)
return True
def add_messages(self, chat_id, messages):
return any(self.add_message(chat_id, msg) for msg in messages)
def fetch_msgs(self, chat_id, offset=0, limit=10): def fetch_msgs(self, chat_id, offset=0, limit=10):
if offset + limit < len(self.msgs[chat_id]): if offset + limit < len(self.msgs[chat_id]):
return sorted(self.msgs[chat_id], key=lambda d: d['id'])[::-1][offset:limit] return sorted(self.msgs[chat_id], key=lambda d: d['id'])[::-1][offset:limit]
@ -218,11 +242,7 @@ class MsgModel:
) )
result.wait() result.wait()
for msg in result.update['messages']: self.add_messages(chat_id, result.update['messages'])
self.msgs[chat_id].append(msg)
# TODO:
# if len(self.msgs[chat_id]) >= offset + limit:
# break
return sorted(self.msgs[chat_id], key=lambda d: d["id"])[::-1][ return sorted(self.msgs[chat_id], key=lambda d: d["id"])[::-1][
offset:limit offset:limit