def __pre__hook(self, fn, u, c, **kwargs): b = c.bot m = u.message or u.channel_post if not m: return age = (datetime.utcnow() - m.date.replace(tzinfo=None)).total_seconds() if age > config.get_config_prop("app")["antiflood"]["age_threshold"]: return chat_id = get_chat_id(u) antiflood.on_chat_msg_received(chat_id) if chat_id in self.floods and self.floods[chat_id] is True: return if not TBDB.get_chat_entry(chat_id): # happens when welcome/joined message is not received TBDB.create_default_chat_entry(chat_id, 'en-US') if chat_id in self.mqbot.active_chats_cache and self.mqbot.active_chats_cache[chat_id] == 0: logger.debug("Marking chat {} as active".format(chat_id)) self.mqbot.active_chats_cache[chat_id] = 1 TBDB.set_chat_active(chat_id, self.mqbot.active_chats_cache[chat_id]) return fn(b, u, **kwargs)
def left_chat_member(bot, update): chat_id = get_chat_id(update) message = update.message or update.channel_post if message.left_chat_member.id == bot.get_me().id: logger.info('Marking chat {} as inactive'.format(chat_id)) bot.active_chats_cache[chat_id] = 0 TBDB.set_chat_active(chat_id, bot.active_chats_cache[chat_id])
def test_db(): id = 1234 TBDB.create_default_chat_entry(id, 'en-US') assert TBDB.get_chat_lang(id) == 'en-US' assert TBDB.get_chat_active(id) == 1 TBDB.set_chat_lang(id, 'lang') TBDB.set_chat_voice_enabled(id, 2) TBDB.set_chat_photos_enabled(id, 1) TBDB.set_chat_qr_enabled(id, 1) TBDB.set_chat_active(id, 0) TBDB.set_chat_ban(id, 1) assert TBDB.get_chat_lang(id) == 'lang' assert TBDB.get_chat_voice_enabled(id) == 2 assert TBDB.get_chat_photos_enabled(id) == 1 assert TBDB.get_chat_qr_enabled(id) == 1 assert TBDB.get_chat_active(id) == 0 assert TBDB.get_chat_ban(id) == 1
def active_check(self, fn, *args, **kwargs): err = None res = None try: res = fn(*args, **kwargs) except Unauthorized as e: pprint.pprint(e) logger.error(e) err = e if err is not None: chat_id = kwargs['chat_id'] if chat_id not in self.active_chats_cache or self.active_chats_cache[chat_id] == 1: logger.debug("Marking chat {} as inactive".format(chat_id)) self.active_chats_cache[chat_id] = 0 TBDB.set_chat_active(chat_id, self.active_chats_cache[chat_id]) raise err return res