From cd814e41321a74305934f95745acbfb12b0c5937 Mon Sep 17 00:00:00 2001 From: m Date: Mon, 18 Jan 2021 19:07:16 +0100 Subject: [PATCH] refactoring userobj bookkeping --- app/constants.py | 1 + app/event_handlers.py | 34 ++++++++++------------------------ 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/app/constants.py b/app/constants.py index b24f86b..e48c590 100644 --- a/app/constants.py +++ b/app/constants.py @@ -12,6 +12,7 @@ ANY_MENTION = [MessageEntity.MENTION, MessageEntity.TEXT_MENTION] PERIODS = ['day','week','month','year','all'] #TODO put in constants, shared with text_commands USERNAME_TO_USEROBJ_MAP = "username_to_userobj_map" #TODO transfer in config or constants USERS = "userobj_set" +GET_ADMIN_FROM_API_REQUEST_COUNTDOWN = "get_administrators_countdown" # in context/chat_data[] diff --git a/app/event_handlers.py b/app/event_handlers.py index 5645b73..366310b 100644 --- a/app/event_handlers.py +++ b/app/event_handlers.py @@ -7,38 +7,24 @@ import helpers as h from telegram.utils.helpers import escape_markdown -from constants import USERS +from constants import USERS, GET_ADMIN_FROM_API_REQUEST_COUNTDOWN def keep_user_register(update: Update, context: CallbackContext) -> None: """sync username and user object on any action - do not use in large chats as there is no deleting from the register""" + do not use in very large chats as there is no deleting from the register""" message = update.message users = set() - # if hasattr(update, "effective_user"): users |= set(update.effective_user) - # if hasattr(message, "new_chat_members"): users |= set(*message.new_chat_members) - # if hasattr(message, "left_chat_member"): users |= set(message.left_chat_member) - # if hasattr(message, "forward_from"): users |= set(message.forward_from) - # if hasattr(update, "edited_message"): - # if hasattr(update.edited_message, "from_user"): users |= set(message.edited_message.from_user) + # get user from every possible source + if hasattr(update, "effective_user"): users |= set(update.effective_user) + if hasattr(message, "new_chat_members"): users |= set(*message.new_chat_members) + if hasattr(message, "left_chat_member"): users |= set(message.left_chat_member) + if hasattr(message, "forward_from"): users |= set(message.forward_from) + if hasattr(update, "edited_message"): + if hasattr(update.edited_message, "from_user"): users |= set(message.edited_message.from_user) try: - users |= {update.effective_user} - except Exception as e: pass - try: - users |= {*message.new_chat_members} - except Exception as e: pass - try: - users |= {message.left_chat_member} - except Exception as e: pass - try: - users |= {message.forward_from} - except Exception as e: pass - try: - users |= {message.edited_message.from_user} - except Exception as e: pass - try: - record = "get_administrators_countdown" + record = GET_ADMIN_FROM_API_REQUEST_COUNTDOWN get_administrators_frequency = 10 # request this data with low frequency try: if context.chat_data[record] >= get_administrators_frequency: