Catch exceptions in update_handler

Fix index error when going over last chat
Remove loops that might cause msg duplicationn
This commit is contained in:
Paul Nameless 2020-05-03 21:06:40 +08:00
parent db02c44596
commit a18481202e
3 changed files with 65 additions and 71 deletions

View file

@ -135,21 +135,21 @@ class Controller:
self.view.draw_msgs(self.model.get_current_msg(), msgs) self.view.draw_msgs(self.model.get_current_msg(), msgs)
def update_handler(self, update): def update_handler(self, update):
log.debug('===============Received: %s', update) try:
_type = update['@type'] log.info('===Received: %s', update)
if _type == 'updateNewMessage': _type = update['@type']
log.debug('Updating... new message') 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.msgs[chat_id].append(update['message'])
# msgs = self.model.get_current_msgs() # msgs = self.model.get_current_msgs()
self.refresh_msgs() self.refresh_msgs()
if not update['disable_notification']: if not update.get('disable_notification'):
try: if update['message']['content'] == 'text':
notify(update['message']['content']['text']['text']) notify(update['message']['content']['text']['text'])
except Exception: except Exception:
log.exception('Error happened on notify: %s', update) log.exception("Error happened in update_handler")
# message_content = update['message']['content'].get('text', {}) # message_content = update['message']['content'].get('text', {})
# we need this because of different message types: photos, files, etc. # we need this because of different message types: photos, files, etc.
# message_text = message_content.get('text', '').lower() # message_text = message_content.get('text', '').lower()

View file

@ -19,9 +19,14 @@ class Model:
return self.users.get_user(user_id) return self.users.get_user(user_id)
def get_current_chat_id(self): 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] return self.chats.chat_ids[self.current_chat]
def get_current_msg(self): 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()] return self.msgs.current_msgs[self.get_current_chat_id()]
def jump_bottom(self): def jump_bottom(self):
@ -60,6 +65,8 @@ class Model:
return self.chats.get_chats(offset=offset, limit=limit) return self.chats.get_chats(offset=offset, limit=limit)
def get_current_msgs(self, offset=0, limit=10): 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] chat_id = self.chats.chat_ids[self.current_chat]
return self.msgs.get_msgs( return self.msgs.get_msgs(
chat_id, offset=offset, limit=limit chat_id, offset=offset, limit=limit
@ -94,41 +101,36 @@ class ChatModel:
offset=len(self.chats), offset=len(self.chats),
limit=len(self.chats) + limit limit=len(self.chats) + limit
) )
for i in range(3): for chat_id in self.chat_ids:
for chat_id in self.chat_ids: chat = self.get_chat(chat_id)
chat = self.get_chat(chat_id) self.chats.append(chat)
self.chats.append(chat)
log.debug(
'#### %s: %s, %s', chat_id, chat, i)
if len(self.chats) >= offset + limit:
break
return self.chats[offset:limit] return self.chats[offset:limit]
def get_chat_ids(self, offset=0, limit=10): def get_chat_ids(self, offset=0, limit=10):
for i in range(3): if len(self.chats):
if len(self.chats): result = self.tg.get_chats(
result = self.tg.get_chats( offset_chat_id=self.chats[-1]['id'],
offset_chat_id=self.chats[-1]['id'], limit=limit
limit=limit )
) else:
else: result = self.tg.get_chats(
result = self.tg.get_chats( offset_order=2 ** 63 - 1,
offset_order=2 ** 63 - 1, offset_chat_id=offset,
offset_chat_id=offset, limit=limit
limit=limit )
)
result.wait() result.wait()
if result.error: if result.error:
log.error(f'get chat ids error: {result.error_info}') log.error(f'get chat ids error: {result.error_info}')
return {} return {}
for chat_id in result.update['chat_ids']: for chat_id in result.update['chat_ids']:
self.chat_ids.append(chat_id) self.chat_ids.append(chat_id)
if len(self.chat_ids) >= offset + limit: # TODO:
break # if len(self.chat_ids) >= offset + limit:
# break
return self.chat_ids[offset:limit] return self.chat_ids[offset:limit]
@ -184,25 +186,25 @@ class MsgModel:
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]
for i in range(3): if len(self.msgs[chat_id]):
if len(self.msgs[chat_id]): result = self.tg.get_chat_history(
result = self.tg.get_chat_history( chat_id,
chat_id, from_message_id=self.msgs[chat_id][-1]['id'],
from_message_id=self.msgs[chat_id][-1]['id'], limit=len(self.msgs[chat_id]) + limit
limit=len(self.msgs[chat_id]) + limit )
) else:
else: result = self.tg.get_chat_history(
result = self.tg.get_chat_history( chat_id,
chat_id, offset=len(self.msgs[chat_id]),
offset=len(self.msgs[chat_id]), limit=len(self.msgs[chat_id]) + limit
limit=len(self.msgs[chat_id]) + limit )
)
result.wait() result.wait()
for msg in result.update['messages']: for msg in result.update['messages']:
self.msgs[chat_id].append(msg) self.msgs[chat_id].append(msg)
if len(self.msgs[chat_id]) >= offset + limit: # TODO:
break # if len(self.msgs[chat_id]) >= offset + limit:
# break
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]

View file

@ -296,11 +296,7 @@ def get_last_msg(chat):
_type = content['@type'] _type = content['@type']
if _type == 'messageText': if _type == 'messageText':
return content['text']['text'] return content['text']['text']
elif _type == 'messageVoiceNote': return f'[{_type}]'
return '[voice msg]'
else:
log.error(chat)
return f'[unknown type {_type}]'
def get_date(chat): def get_date(chat):
@ -314,11 +310,7 @@ def parse_content(content):
_type = content['@type'] _type = content['@type']
if _type == 'messageText': if _type == 'messageText':
return content['text']['text'] return content['text']['text']
elif _type == 'messageVoiceNote': return f'[{_type}]'
return '[voice msg]'
else:
log.debug('Unknown content: %s', content)
return f'[unknown content type {_type}]'
emoji_pattern = re.compile( emoji_pattern = re.compile(