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 __load_chat_blacklists(): global CHAT_BLACKLISTS try: chats = SESSION.query(BlackListFilters.chat_id).distinct().all() for (chat_id, ) in chats: CHAT_BLACKLISTS[chat_id] = [] all_filters = SESSION.query(BlackListFilters).all() for x in all_filters: CHAT_BLACKLISTS[x.chat_id] += [x.trigger] CHAT_BLACKLISTS = {x: set(y) for x, y in CHAT_BLACKLISTS.items()} finally: SESSION.close()
def del_user(user_id): with INSERTION_LOCK: curr = SESSION.query(Users).get(user_id) if curr: SESSION.delete(curr) SESSION.commit() return
def all_approved(chat_id): try: return (SESSION.query(Approvals).filter( Approvals.chat_id == str(chat_id)).order_by( Approvals.user_id.asc()).all()) finally: SESSION.close()
def chat_should_report(chat_id: Union[str, int]) -> bool: try: chat_setting = SESSION.query(ReportingChatSettings).get(str(chat_id)) if chat_setting: return chat_setting.should_report return False finally: SESSION.close()
def __load_gbanned_userid_list(): global GBANNED_DICT getall = SESSION.query(GloballyBannedUsers).all() try: for x in getall: GBANNED_DICT[x.user_id] = {"name": x.name, "reason": x.reason} finally: SESSION.close()
def user_should_report(user_id: int) -> bool: try: user_setting = SESSION.query(ReportingUserSettings).get(user_id) if user_setting: return user_setting.should_report return True finally: SESSION.close()
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_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 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 get_userid_by_username(username): try: return ( SESSION.query(Users) .filter(func.lower(Users.username) == username.lower()) .all() ) finally: SESSION.close()
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 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 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 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 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 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 __load_all_notes(): global CHAT_NOTES getall = SESSION.query(Notes).distinct().all() for x in getall: if not CHAT_NOTES.get(x.chat_id): CHAT_NOTES[x.chat_id] = {} CHAT_NOTES[x.chat_id][x.name] = { "value": x.value, "type": x.msgtype, "file": x.file, }
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 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 get_rules(chat_id): with INSERTION_LOCK: try: rules = SESSION.query(Rules).get(str(chat_id)) ret_rules = "" if rules: ret_rules = rules.rules except BaseException: return False finally: SESSION.close() return ret_rules
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 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 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 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 num_chats(): try: return SESSION.query(Chats).count() finally: SESSION.close()
def get_all_users(): try: return SESSION.query(Users).all() finally: SESSION.close()