Merge pull request #6 from paul-nameless/dev

Catch exceptions in update_handler
This commit is contained in:
Alex 2020-05-03 16:23:42 +03:00 committed by GitHub
commit 59dfcd5bbf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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)
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()

View file

@ -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]

View file

@ -296,11 +296,7 @@ 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}]'
return f'[{_type}]'
def get_date(chat):
@ -314,11 +310,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(