def add_note_to_db(chat_id, note_name, note_data, msgtype, buttons=None, file=None): if not buttons: buttons = [] with NOTES_INSERTION_LOCK: prev = SESSION.query(Notes).get((str(chat_id), note_name)) if prev: with BUTTONS_INSERTION_LOCK: prev_buttons = (SESSION.query(Buttons).filter( Buttons.chat_id == str(chat_id), Buttons.note_name == note_name, ).all()) for btn in prev_buttons: SESSION.delete(btn) SESSION.delete(prev) note = Notes( str(chat_id), note_name, note_data or "", msgtype=msgtype.value, file=file, ) SESSION.add(note) SESSION.commit() for b_name, url, same_line in buttons: add_note_button_to_db(chat_id, note_name, b_name, url, same_line)
def set_custom_gdbye(chat_id, custom_goodbye, goodbye_type, buttons=None): if buttons is None: buttons = [] with INSERTION_LOCK: welcome_settings = SESSION.query(Welcome).get(str(chat_id)) if not welcome_settings: welcome_settings = Welcome(str(chat_id), True) if custom_goodbye: welcome_settings.custom_leave = custom_goodbye welcome_settings.leave_type = goodbye_type.value else: welcome_settings.custom_leave = DEFAULT_GOODBYE welcome_settings.leave_type = Types.TEXT.value SESSION.add(welcome_settings) with LEAVE_BTN_LOCK: prev_buttons = ( SESSION.query(GoodbyeButtons) .filter(GoodbyeButtons.chat_id == str(chat_id)) .all() ) for btn in prev_buttons: SESSION.delete(btn) for b_name, url, same_line in buttons: button = GoodbyeButtons(chat_id, b_name, url, same_line) SESSION.add(button) SESSION.commit()
def migrate_chat(old_chat_id, new_chat_id): with WARN_INSERTION_LOCK: chat_notes = (SESSION.query(Warns).filter( Warns.chat_id == str(old_chat_id)).all()) for note in chat_notes: note.chat_id = str(new_chat_id) SESSION.commit() with WARN_FILTER_INSERTION_LOCK: chat_filters = (SESSION.query(WarnFilters).filter( WarnFilters.chat_id == str(old_chat_id)).all()) for filt in chat_filters: filt.chat_id = str(new_chat_id) SESSION.commit() old_filt = WARN_FILTERS.get(str(old_chat_id)) if old_filt: WARN_FILTERS[str(new_chat_id)] = old_filt del WARN_FILTERS[str(old_chat_id)] with WARN_SETTINGS_LOCK: chat_settings = (SESSION.query(WarnSettings).filter( WarnSettings.chat_id == str(old_chat_id)).all()) for setting in chat_settings: setting.chat_id = str(new_chat_id) SESSION.commit()
def migrate_chat(old_chat_id, new_chat_id): with INSERTION_LOCK: chat = SESSION.query(Welcome).get(str(old_chat_id)) if chat: chat.chat_id = str(new_chat_id) with WELC_BTN_LOCK: chat_buttons = ( SESSION.query(WelcomeButtons) .filter(WelcomeButtons.chat_id == str(old_chat_id)) .all() ) for btn in chat_buttons: btn.chat_id = str(new_chat_id) with LEAVE_BTN_LOCK: chat_buttons = ( SESSION.query(GoodbyeButtons) .filter(GoodbyeButtons.chat_id == str(old_chat_id)) .all() ) for btn in chat_buttons: btn.chat_id = str(new_chat_id) SESSION.commit()
def remove_filter(chat_id, keyword): global CHAT_FILTERS with CUST_FILT_LOCK: filt = SESSION.query(CustomFilters).get((str(chat_id), keyword)) if filt: if keyword in CHAT_FILTERS.get(str(chat_id), []): # Sanity check CHAT_FILTERS.get(str(chat_id), []).remove(keyword) with BUTTON_LOCK: prev_buttons = ( SESSION.query(Buttons) .filter( Buttons.chat_id == str(chat_id), Buttons.keyword == keyword, ) .all() ) for btn in prev_buttons: SESSION.delete(btn) SESSION.delete(filt) SESSION.commit() return True SESSION.close() return False
def add_filter( chat_id, keyword, reply, is_sticker=False, is_document=False, is_image=False, is_audio=False, is_voice=False, is_video=False, buttons=None, ): global CHAT_FILTERS if buttons is None: buttons = [] with CUST_FILT_LOCK: prev = SESSION.query(CustomFilters).get((str(chat_id), keyword)) if prev: with BUTTON_LOCK: prev_buttons = ( SESSION.query(Buttons) .filter( Buttons.chat_id == str(chat_id), Buttons.keyword == keyword, ) .all() ) for btn in prev_buttons: SESSION.delete(btn) SESSION.delete(prev) filt = CustomFilters( str(chat_id), keyword, reply, is_sticker, is_document, is_image, is_audio, is_voice, is_video, bool(buttons), ) if keyword not in CHAT_FILTERS.get(str(chat_id), []): CHAT_FILTERS[str(chat_id)] = sorted( CHAT_FILTERS.get(str(chat_id), []) + [keyword], key=lambda x: (-len(x), x), ) SESSION.add(filt) SESSION.commit() for b_name, url, same_line in buttons: add_note_button_to_db(chat_id, keyword, b_name, url, same_line)
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 migrate_chat(old_chat_id, new_chat_id): with NOTES_INSERTION_LOCK: chat_notes = (SESSION.query(Notes).filter( Notes.chat_id == str(old_chat_id)).all()) for note in chat_notes: note.chat_id = str(new_chat_id) with BUTTONS_INSERTION_LOCK: chat_buttons = (SESSION.query(Buttons).filter( Buttons.chat_id == str(old_chat_id)).all()) for btn in chat_buttons: btn.chat_id = str(new_chat_id) SESSION.commit()
def warn_user(user_id, chat_id, reason=None): with WARN_INSERTION_LOCK: warned_user = SESSION.query(Warns).get((user_id, str(chat_id))) if not warned_user: warned_user = Warns(user_id, str(chat_id)) warned_user.num_warns += 1 if reason == "": reason = "No reason given." if reason: if warned_user.reasons is None: warned_user.reasons = [reason] else: # TODO:: double check this Daan: Not really wizardry, it adds a # new entry to a list/array which can be done this way, # basically append equivalent warned_user.reasons = warned_user.reasons + [reason] reasons = warned_user.reasons num = warned_user.num_warns SESSION.add(warned_user) SESSION.commit() return num, reasons
def num_chats(): try: return SESSION.query( func.count(distinct(CustomFilters.chat_id)) ).scalar() finally: SESSION.close()
def __migrate_filters(): try: all_filters = SESSION.query(CustomFilters).distinct().all() for x in all_filters: if x.is_document: file_type = Types.DOCUMENT elif x.is_image: file_type = Types.PHOTO elif x.is_video: file_type = Types.VIDEO elif x.is_sticker: file_type = Types.STICKER elif x.is_audio: file_type = Types.AUDIO elif x.is_voice: file_type = Types.VOICE else: file_type = Types.TEXT print(str(x.chat_id), x.keyword, x.reply, file_type.value) if file_type == Types.TEXT: filt = CustomFilters( str(x.chat_id), x.keyword, x.reply, file_type.value, None ) else: filt = CustomFilters( str(x.chat_id), x.keyword, None, file_type.value, x.reply ) SESSION.add(filt) SESSION.commit() finally: SESSION.close()
def get_buttons(chat_id, note_name): try: return (SESSION.query(Buttons).filter( Buttons.chat_id == str(chat_id), Buttons.note_name == note_name).order_by(Buttons.id).all()) finally: SESSION.close()
def welcome_mutes(chat_id): try: welcomemutes = SESSION.query(WelcomeMute).get(str(chat_id)) if welcomemutes: return welcomemutes.welcomemutes return False finally: SESSION.close()
def get_custom_welcome(chat_id): welcome_settings = SESSION.query(Welcome).get(str(chat_id)) ret = DEFAULT_WELCOME if welcome_settings and welcome_settings.custom_welcome: ret = welcome_settings.custom_welcome SESSION.close() return ret
def get_gdbye_pref(chat_id): welc = SESSION.query(Welcome).get(str(chat_id)) SESSION.close() if welc: return welc.should_goodbye, welc.custom_leave, welc.leave_type else: # Welcome by default. return True, DEFAULT_GOODBYE, Types.TEXT
def get_custom_gdbye(chat_id): welcome_settings = SESSION.query(Welcome).get(str(chat_id)) ret = DEFAULT_GOODBYE if welcome_settings and welcome_settings.custom_leave: ret = welcome_settings.custom_leave SESSION.close() return ret
def get_note(chat_id, note_name): try: return (SESSION.query(Notes).filter( func.lower(Notes.name) == note_name, Notes.chat_id == str(chat_id), ).first()) finally: SESSION.close()
def clean_service(chat_id: Union[str, int]) -> bool: try: chat_setting = SESSION.query(CleanServiceSetting).get(str(chat_id)) if chat_setting: return chat_setting.clean_service return False finally: SESSION.close()
def set_welcome_mutes(chat_id, welcomemutes): with WM_LOCK: prev = SESSION.query(WelcomeMute).get((str(chat_id))) if prev: SESSION.delete(prev) welcome_m = WelcomeMute(str(chat_id), welcomemutes) SESSION.add(welcome_m) SESSION.commit()
def get_welc_mutes_pref(chat_id): welcomemutes = SESSION.query(WelcomeMute).get(str(chat_id)) SESSION.close() if welcomemutes: return welcomemutes.welcomemutes return False
def get_clean_pref(chat_id): welc = SESSION.query(Welcome).get(str(chat_id)) SESSION.close() if welc: return welc.clean_welcome return False
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 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 connect(user_id, chat_id): with CONNECTION_INSERTION_LOCK: prev = SESSION.query(Connection).get((int(user_id))) if prev: SESSION.delete(prev) connect_to_chat = Connection(int(user_id), chat_id) SESSION.add(connect_to_chat) SESSION.commit() return True
def allow_connect_to_chat(chat_id: Union[str, int]) -> bool: try: chat_setting = SESSION.query(ChatAccessConnectionSettings).get( str(chat_id)) if chat_setting: return chat_setting.allow_connect_to_chat return False finally: SESSION.close()
def set_clean_service(chat_id: Union[int, str], setting: bool): with CS_LOCK: chat_setting = SESSION.query(CleanServiceSetting).get(str(chat_id)) if not chat_setting: chat_setting = CleanServiceSetting(chat_id) chat_setting.clean_service = setting SESSION.add(chat_setting) SESSION.commit()
def __load_flood_settings(): global CHAT_FLOOD try: all_chats = SESSION.query(FloodControl).all() CHAT_FLOOD = { chat.chat_id: (None, DEF_COUNT, chat.limit) for chat in all_chats } finally: SESSION.close()
def __load_chat_warn_filters(): global WARN_FILTERS try: chats = SESSION.query(WarnFilters.chat_id).distinct().all() for (chat_id, ) in chats: # remove tuple by ( ,) WARN_FILTERS[chat_id] = [] all_filters = SESSION.query(WarnFilters).all() for x in all_filters: WARN_FILTERS[x.chat_id] += [x.keyword] WARN_FILTERS = { x: sorted(set(y), key=lambda i: (-len(i), i)) for x, y in WARN_FILTERS.items() } finally: SESSION.close()
def set_warn_strength(chat_id, soft_warn): with WARN_SETTINGS_LOCK: curr_setting = SESSION.query(WarnSettings).get(str(chat_id)) if not curr_setting: curr_setting = WarnSettings(chat_id, soft_warn=soft_warn) curr_setting.soft_warn = soft_warn SESSION.add(curr_setting) SESSION.commit()
def get_warn_setting(chat_id): try: setting = SESSION.query(WarnSettings).get(str(chat_id)) if setting: return setting.warn_limit, setting.soft_warn else: return 3, False finally: SESSION.close()