def add_to_blacklist(chat_id, trigger): with BLACKLIST_FILTER_INSERTION_LOCK: blacklist_filt = BlackListFilters(str(chat_id), trigger) SESSION.merge(blacklist_filt) # merge to avoid duplicate key issues SESSION.commit() CHAT_BLACKLISTS.setdefault(str(chat_id), set()).add(trigger)
def update_user(user_id, username, chat_id=None, chat_name=None): with INSERTION_LOCK: user = SESSION.query(Users).get(user_id) if not user: user = Users(user_id, username) SESSION.add(user) SESSION.flush() else: user.username = username if not chat_id or not chat_name: SESSION.commit() return chat = SESSION.query(Chats).get(str(chat_id)) if not chat: chat = Chats(str(chat_id), chat_name) SESSION.add(chat) SESSION.flush() else: chat.chat_name = chat_name member = SESSION.query(ChatMembers).filter( ChatMembers.chat == chat.chat_id, ChatMembers.user == user.user_id).first() if not member: chat_member = ChatMembers(chat.chat_id, user.user_id) SESSION.add(chat_member) SESSION.commit()
def init_restrictions(chat_id, reset=False): curr_restr = SESSION.query(Restrictions).get(str(chat_id)) if reset: SESSION.delete(curr_restr) SESSION.flush() restr = Restrictions(str(chat_id)) SESSION.add(restr) SESSION.commit() return restr
def init_permissions(chat_id, reset=False): curr_perm = SESSION.query(Permissions).get(str(chat_id)) if reset: SESSION.delete(curr_perm) SESSION.flush() perm = Permissions(str(chat_id)) SESSION.add(perm) SESSION.commit() return perm
def set_gp_exit_preference(chat_id, should_goodbye): with INSERTION_LOCK: curr = SESSION.query(Welcome).get(str(chat_id)) if not curr: curr = Welcome(str(chat_id), should_goodbye=should_goodbye) else: curr.should_goodbye = should_goodbye SESSION.add(curr) SESSION.commit()
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 migrate_chat(old_chat_id, new_chat_id): with PERM_LOCK: perms = SESSION.query(Permissions).get(str(old_chat_id)) if perms: perms.chat_id = str(new_chat_id) SESSION.commit() with RESTR_LOCK: rest = SESSION.query(Restrictions).get(str(old_chat_id)) if rest: rest.chat_id = str(new_chat_id) SESSION.commit()
def del_user(user_id): with INSERTION_LOCK: curr = SESSION.query(Users).get(user_id) if curr: SESSION.delete(curr) SESSION.commit() return True ChatMembers.query.filter(ChatMembers.user == user_id).delete() SESSION.commit() 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 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 rm_from_blacklist(chat_id, trigger): with BLACKLIST_FILTER_INSERTION_LOCK: blacklist_filt = SESSION.query(BlackListFilters).get( (str(chat_id), trigger)) if blacklist_filt: if trigger in CHAT_BLACKLISTS.get(str(chat_id), set()): # sanity check CHAT_BLACKLISTS.get(str(chat_id), set()).remove(trigger) SESSION.delete(blacklist_filt) SESSION.commit() return True SESSION.close() return False
def migrate_chat(old_chat_id, new_chat_id): with INSERTION_LOCK: chat = SESSION.query(Chats).get(str(old_chat_id)) if chat: chat.chat_id = str(new_chat_id) SESSION.add(chat) SESSION.flush() chat_members = SESSION.query(ChatMembers).filter( ChatMembers.chat == str(old_chat_id)).all() for member in chat_members: member.chat = str(new_chat_id) SESSION.add(member) SESSION.commit()
def update_restriction(chat_id, restr_type, locked): with RESTR_LOCK: curr_restr = SESSION.query(Restrictions).get(str(chat_id)) if not curr_restr: curr_restr = init_restrictions(chat_id) if restr_type == "messages": curr_restr.messages = locked elif restr_type == "media": curr_restr.media = locked elif restr_type == "other": curr_restr.other = locked elif restr_type == "previews": curr_restr.preview = locked elif restr_type == "all": curr_restr.messages = locked curr_restr.media = locked curr_restr.other = locked curr_restr.preview = locked SESSION.add(curr_restr) SESSION.commit()
def update_lock(chat_id, lock_type, locked): with PERM_LOCK: curr_perm = SESSION.query(Permissions).get(str(chat_id)) if not curr_perm: curr_perm = init_permissions(chat_id) if lock_type == "audio": curr_perm.audio = locked elif lock_type == "voice": curr_perm.voice = locked elif lock_type == "contact": curr_perm.contact = locked elif lock_type == "video": curr_perm.video = locked elif lock_type == "videonote": curr_perm.videonote = locked elif lock_type == "document": curr_perm.document = locked elif lock_type == "photo": curr_perm.photo = locked elif lock_type == "sticker": curr_perm.sticker = locked elif lock_type == "gif": curr_perm.gif = locked elif lock_type == 'url': curr_perm.url = locked elif lock_type == 'bots': curr_perm.bots = locked elif lock_type == 'forward': curr_perm.forward = locked elif lock_type == 'game': curr_perm.game = locked elif lock_type == 'location': curr_perm.location = locked SESSION.add(curr_perm) SESSION.commit()
def ensure_bot_in_db(): with INSERTION_LOCK: bot = Users(dispatcher.bot.id, dispatcher.bot.username) SESSION.merge(bot) SESSION.commit()