""" MIT License Copyright (c) 2022 Aʙɪsʜɴᴏɪ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ import threading from sqlalchemy import Column, String, UnicodeText, distinct, func from Database.sql import BASE, SESSION class Disable(BASE): __tablename__ = "disabled_commands" chat_id = Column(String(14), primary_key=True) command = Column(UnicodeText, primary_key=True) def __init__(self, chat_id, command): self.chat_id = chat_id self.command = command def __repr__(self): return "ᴅɪsᴀʙʟᴇᴅ ᴄᴍᴅ {} in {}".format(self.command, self.chat_id) Disable.__table__.create(checkfirst=True) DISABLE_INSERTION_LOCK = threading.RLock() DISABLED = {} def disable_command(chat_id, disable): with DISABLE_INSERTION_LOCK: disabled = SESSION.query(Disable).get((str(chat_id), disable)) if not disabled: DISABLED.setdefault(str(chat_id), set()).add(disable) disabled = Disable(str(chat_id), disable) SESSION.add(disabled) SESSION.commit() return True SESSION.close() return False def enable_command(chat_id, enable): with DISABLE_INSERTION_LOCK: disabled = SESSION.query(Disable).get((str(chat_id), enable)) if disabled: if enable in DISABLED.get(str(chat_id)): # sanity check DISABLED.setdefault(str(chat_id), set()).remove(enable) SESSION.delete(disabled) SESSION.commit() return True SESSION.close() return False def is_command_disabled(chat_id, cmd): return str(cmd).lower() in DISABLED.get(str(chat_id), set()) def get_all_disabled(chat_id): return DISABLED.get(str(chat_id), set()) def num_chats(): try: return SESSION.query(func.count(distinct(Disable.chat_id))).scalar() finally: SESSION.close() def num_disabled(): try: return SESSION.query(Disable).count() finally: SESSION.close() def migrate_chat(old_chat_id, new_chat_id): with DISABLE_INSERTION_LOCK: chats = SESSION.query(Disable).filter(Disable.chat_id == str(old_chat_id)).all() for chat in chats: chat.chat_id = str(new_chat_id) SESSION.add(chat) if str(old_chat_id) in DISABLED: DISABLED[str(new_chat_id)] = DISABLED.get(str(old_chat_id), set()) SESSION.commit() def __load_disabled_commands(): global DISABLED try: all_chats = SESSION.query(Disable).all() for chat in all_chats: DISABLED.setdefault(chat.chat_id, set()).add(chat.command) finally: SESSION.close() __load_disabled_commands()