From 4c5c7801e46dccd2755626d49304eeaeccb926a0 Mon Sep 17 00:00:00 2001 From: Paul Nameless Date: Sun, 3 May 2020 21:06:40 +0800 Subject: [PATCH] Catch exceptions in update_handler Fix index error when going over last chat Remove loops that might cause msg duplicationn --- tg/controllers/__init__.py | 30 ++++++------ tg/models/__init__.py | 94 +++++++++++++++++++------------------- tg/views/__init__.py | 16 +++---- 3 files changed, 69 insertions(+), 71 deletions(-) diff --git a/tg/controllers/__init__.py b/tg/controllers/__init__.py index 5fd0741..34298cb 100644 --- a/tg/controllers/__init__.py +++ b/tg/controllers/__init__.py @@ -135,21 +135,21 @@ class Controller: self.view.draw_msgs(self.model.get_current_msg(), msgs) def update_handler(self, update): - log.debug('===============Received: %s', update) - _type = update['@type'] - if _type == 'updateNewMessage': - log.debug('Updating... new message') - # with self.lock: - chat_id = update['message']['chat_id'] - self.model.msgs.msgs[chat_id].append(update['message']) - # msgs = self.model.get_current_msgs() - self.refresh_msgs() - if not update['disable_notification']: - try: - notify(update['message']['content']['text']['text']) - except Exception: - log.exception('Error happened on notify: %s', update) - # message_content = update['message']['content'].get('text', {}) + try: + log.info('===Received: %s', update) + _type = update['@type'] + if _type == 'updateNewMessage': + # with self.lock: + chat_id = update['message']['chat_id'] + self.model.msgs.msgs[chat_id].append(update['message']) + # msgs = self.model.get_current_msgs() + self.refresh_msgs() + if not update.get('disable_notification'): + if update['message']['content'] == 'text': + notify(update['message']['content']['text']['text']) + except Exception: + log.exception("Error happened in update_handler") + # message_content = update['message']['content'].get('text', {}) # we need this because of different message types: photos, files, etc. # message_text = message_content.get('text', '').lower() diff --git a/tg/models/__init__.py b/tg/models/__init__.py index 5019a12..371e989 100644 --- a/tg/models/__init__.py +++ b/tg/models/__init__.py @@ -19,9 +19,14 @@ class Model: return self.users.get_user(user_id) def get_current_chat_id(self): + if self.current_chat >= len(self.chats.chat_ids): + return None return self.chats.chat_ids[self.current_chat] def get_current_msg(self): + chat_id = self.get_current_chat_id() + if chat_id is None: + return [] return self.msgs.current_msgs[self.get_current_chat_id()] def jump_bottom(self): @@ -60,6 +65,8 @@ class Model: return self.chats.get_chats(offset=offset, limit=limit) def get_current_msgs(self, offset=0, limit=10): + if self.current_chat >= len(self.chats.chat_ids): + return [] chat_id = self.chats.chat_ids[self.current_chat] return self.msgs.get_msgs( chat_id, offset=offset, limit=limit @@ -94,41 +101,36 @@ class ChatModel: offset=len(self.chats), limit=len(self.chats) + limit ) - for i in range(3): - for chat_id in self.chat_ids: - chat = self.get_chat(chat_id) - self.chats.append(chat) - log.debug( - '#### %s: %s, %s', chat_id, chat, i) - if len(self.chats) >= offset + limit: - break + for chat_id in self.chat_ids: + chat = self.get_chat(chat_id) + self.chats.append(chat) return self.chats[offset:limit] def get_chat_ids(self, offset=0, limit=10): - for i in range(3): - if len(self.chats): - result = self.tg.get_chats( - offset_chat_id=self.chats[-1]['id'], - limit=limit - ) - else: - result = self.tg.get_chats( - offset_order=2 ** 63 - 1, - offset_chat_id=offset, - limit=limit - ) + if len(self.chats): + result = self.tg.get_chats( + offset_chat_id=self.chats[-1]['id'], + limit=limit + ) + else: + result = self.tg.get_chats( + offset_order=2 ** 63 - 1, + offset_chat_id=offset, + limit=limit + ) - result.wait() - if result.error: - log.error(f'get chat ids error: {result.error_info}') - return {} + result.wait() + if result.error: + log.error(f'get chat ids error: {result.error_info}') + return {} - for chat_id in result.update['chat_ids']: - self.chat_ids.append(chat_id) + for chat_id in result.update['chat_ids']: + self.chat_ids.append(chat_id) - if len(self.chat_ids) >= offset + limit: - break + # TODO: + # if len(self.chat_ids) >= offset + limit: + # break return self.chat_ids[offset:limit] @@ -184,25 +186,25 @@ class MsgModel: if offset + limit < len(self.msgs[chat_id]): return sorted(self.msgs[chat_id], key=lambda d: d['id'])[::-1][offset:limit] - for i in range(3): - if len(self.msgs[chat_id]): - result = self.tg.get_chat_history( - chat_id, - from_message_id=self.msgs[chat_id][-1]['id'], - limit=len(self.msgs[chat_id]) + limit - ) - else: - result = self.tg.get_chat_history( - chat_id, - offset=len(self.msgs[chat_id]), - limit=len(self.msgs[chat_id]) + limit - ) + if len(self.msgs[chat_id]): + result = self.tg.get_chat_history( + chat_id, + from_message_id=self.msgs[chat_id][-1]['id'], + limit=len(self.msgs[chat_id]) + limit + ) + else: + result = self.tg.get_chat_history( + chat_id, + offset=len(self.msgs[chat_id]), + limit=len(self.msgs[chat_id]) + limit + ) - result.wait() - for msg in result.update['messages']: - self.msgs[chat_id].append(msg) - if len(self.msgs[chat_id]) >= offset + limit: - break + result.wait() + for msg in 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][offset:limit] diff --git a/tg/views/__init__.py b/tg/views/__init__.py index 156fa00..c82cace 100644 --- a/tg/views/__init__.py +++ b/tg/views/__init__.py @@ -296,11 +296,11 @@ def get_last_msg(chat): _type = content['@type'] if _type == 'messageText': return content['text']['text'] - elif _type == 'messageVoiceNote': - return '[voice msg]' - else: - log.error(chat) - return f'[unknown type {_type}]' + # elif _type == 'messageVoiceNote': + # return '[voice msg]' + # else: + # log.warning('error in get last msg: %s', chat) + return f'[{_type}]' def get_date(chat): @@ -314,11 +314,7 @@ def parse_content(content): _type = content['@type'] if _type == 'messageText': return content['text']['text'] - elif _type == 'messageVoiceNote': - return '[voice msg]' - else: - log.debug('Unknown content: %s', content) - return f'[unknown content type {_type}]' + return f'[{_type}]' emoji_pattern = re.compile(