def del_user(user_id): with INSERTION_LOCK: curr = SESSION.query(Users).get(user_id) if curr: SESSION.delete(curr) SESSION.commit() return
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 SESSION.commit() SESSION.close() return
def migrate_chat(old_chat_id, new_chat_id): with INSERTION_LOCK: old_chat = SESSION.query(Chats).get(str(old_chat_id)) if old_chat: old_chat.chat_id = str(new_chat_id) SESSION.merge(old_chat) SESSION.commit() SESSION.close()
def set_chat_setting(chat_id: Union[int, str], setting: bool): with CHAT_LOCK: chat_setting = SESSION.query(ReportingChatSettings).get(str(chat_id)) if not chat_setting: chat_setting = ReportingChatSettings(chat_id) chat_setting.should_report = setting SESSION.add(chat_setting) SESSION.commit()
def set_user_setting(user_id: int, setting: bool): with USER_LOCK: user_setting = SESSION.query(ReportingUserSettings).get(user_id) if not user_setting: user_setting = ReportingUserSettings(user_id) user_setting.should_report = setting SESSION.add(user_setting) SESSION.commit()
def migrate_chat(old_chat_id, new_chat_id): with INSERTION_LOCK: try: chat = SESSION.query(Rules).get(str(old_chat_id)) if chat: chat.chat_id = str(new_chat_id) SESSION.commit() finally: SESSION.close()
def disapprove(chat_id, user_id): with INSERTION_LOCK: note = SESSION.query(Approvals).get((str(chat_id), user_id)) if note: SESSION.delete(note) SESSION.commit() return True else: SESSION.close() return False
def add_to_blacklist(chat_id, trigger): with INSERTION_LOCK: try: blacklist_filt = BlackListFilters(str(chat_id), trigger) SESSION.merge(blacklist_filt) SESSION.commit() CHAT_BLACKLISTS.setdefault(str(chat_id), set()).add(trigger) finally: SESSION.close()
def migrate_chat(old_chat_id, new_chat_id): with INSERTION_LOCK: try: chat_filters = (SESSION.query(BlackListFilters).filter( BlackListFilters.chat_id == str(old_chat_id)).all()) for filt in chat_filters: filt.chat_id = str(new_chat_id) SESSION.commit() finally: SESSION.close()
def migrate_chat(old_chat_id, new_chat_id): with CHAT_LOCK: chat_notes = ( SESSION.query(ReportingChatSettings) .filter(ReportingChatSettings.chat_id == str(old_chat_id)) .all() ) for note in chat_notes: note.chat_id = str(new_chat_id) SESSION.commit()
def ungban_user(user_id): with GBAN_LOCK: user = SESSION.query(GloballyBannedUsers).get(user_id) try: if user: SESSION.delete(user) SESSION.commit() finally: SESSION.close() __load_gbanned_userid_list() return
def rem_chat(chat_id): with INSERTION_LOCK: chat = SESSION.query(Chats).get(str(chat_id)) if chat: try: SESSION.delete(chat) SESSION.commit() finally: SESSION.close() else: SESSION.close()
def clear_rules(chat_id): with INSERTION_LOCK: try: rules = SESSION.query(Rules).get(str(chat_id)) SESSION.delete(rules) SESSION.commit() except BaseException: return False finally: SESSION.close() return True
def set_rules(chat_id, rules_text): with INSERTION_LOCK: try: rules = SESSION.query(Rules).get(str(chat_id)) if not rules: rules = Rules(str(chat_id)) rules.rules = rules_text SESSION.add(rules) SESSION.commit() finally: SESSION.close()
def rm_note(chat_id, note_name): global CHAT_NOTES with INSERTION_LOCK: note = SESSION.query(Notes).get((str(chat_id), note_name)) if note: SESSION.delete(note) SESSION.commit() CHAT_NOTES[str(chat_id)].pop(note_name) return True else: SESSION.close() return False
def gban_user(user_id, name, reason=None): with GBAN_LOCK: user = SESSION.query(GloballyBannedUsers).get(user_id) try: if not user: user = GloballyBannedUsers(user_id, name, reason) else: user.reason = reason user.name = name SESSION.merge(user) SESSION.commit() finally: SESSION.close() __load_gbanned_userid_list() return
def rm_from_blacklist(chat_id, trigger): with INSERTION_LOCK: try: blacklist_filt = SESSION.query(BlackListFilters).get( (str(chat_id), trigger)) if blacklist_filt: if trigger in CHAT_BLACKLISTS.get(str(chat_id), set()): CHAT_BLACKLISTS.get(str(chat_id), set()).remove(trigger) SESSION.delete(blacklist_filt) SESSION.commit() return True finally: SESSION.close() return False
def update_gban_reason(user_id, name, reason=None): with GBAN_LOCK: try: user = SESSION.query(GloballyBannedUsers).get(user_id) if not user: user = GloballyBannedUsers(user_id, name, reason) old_reason = "" else: old_reason = user.reason user.name = name user.reason = reason SESSION.merge(user) SESSION.commit() finally: SESSION.close() return old_reason
def rm_all_note(chat_id): global CHAT_NOTES with INSERTION_LOCK: all_notes = get_all_notes(chat_id) for note_name in all_notes: note = SESSION.query(Notes).get((str(chat_id), note_name)) if note: try: SESSION.delete(note) SESSION.commit() CHAT_NOTES[str(chat_id)].pop(note_name) except: pass SESSION.close() del CHAT_NOTES[str(chat_id)] return True
def save_note(chat_id, note_name, note_data, msgtype, file=None): global CHAT_NOTES with INSERTION_LOCK: prev = SESSION.query(Notes).get((str(chat_id), note_name)) if prev: SESSION.delete(prev) note = Notes(str(chat_id), note_name, note_data, msgtype, file) SESSION.add(note) SESSION.commit() if not CHAT_NOTES.get(chat_id): CHAT_NOTES[chat_id] = {} CHAT_NOTES[chat_id][note_name] = { "value": note_data, "type": msgtype, "file": file, }
def disapprove_all(chat_id): users_list = [] try: users = (SESSION.query(Approvals).filter( Approvals.chat_id == str(chat_id)).order_by( Approvals.user_id.asc()).all()) for i in users: users_list.append(int(i.user_id)) with INSERTION_LOCK: for user_id in users_list: note = SESSION.query(Approvals).get((str(chat_id), user_id)) if note: SESSION.delete(note) SESSION.commit() else: SESSION.close() return True except: return False
def approve(chat_id, user_id): with INSERTION_LOCK: note = Approvals(str(chat_id), user_id) SESSION.add(note) SESSION.commit()