def switch_to_locale(chat_id, locale_name): with LOCALES_INSERTION_LOCK: prev = SESSION.query(Locales).get((str(chat_id))) if prev: SESSION.delete(prev) switch_locale = Locales(str(chat_id), locale_name) SESSION.add(switch_locale) SESSION.commit()
def chat_leave_fed(chat_id): with FEDS_LOCK: H = False curr = SESSION.query(ChatF).all() for U in curr: if int(U.chat_id) == int(chat_id): H = True SESSION.delete(U) SESSION.commit() return H
def remove_warn_filter(chat_id, keyword): with WARN_FILTER_INSERTION_LOCK: warn_filt = SESSION.query(WarnFilters).get((str(chat_id), keyword)) if warn_filt: if keyword in WARN_FILTERS.get(str(chat_id), []): # sanity check WARN_FILTERS.get(str(chat_id), []).remove(keyword) SESSION.delete(warn_filt) 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 del_fed(fed_id, chat_id): with FEDS_LOCK: curr = SESSION.query(Federations).get(fed_id) print(curr) if curr: SESSION.delete(curr) SESSION.commit() curr = SESSION.query(ChatF).get(str(chat_id)) if curr: SESSION.delete(curr) SESSION.commit() curr = SESSION.query(UserF).all() for I in curr: if I.fed_id == fed_id: SESSION.delete(I) SESSION.commit() curr = SESSION.query(RulesF).get(fed_id) if curr: SESSION.delete(curr) SESSION.commit() return
def user_demote_fed(fed_id, user_id): with FEDS_LOCK: curr = SESSION.query(UserF).all() result = False for r in curr: print(r.user_id, user_id, r.fed_id, fed_id) if int(r.user_id) == int(user_id): print("yes1 - ", r.user_id, user_id) if r.fed_id == fed_id: print("yes2 - ", r.fed_id, fed_id) SESSION.delete(r) SESSION.commit() result = True SESSION.close() return result
def set_frules(fed_id, rules): with FEDS_LOCK: r = SESSION.query(RulesF).get(fed_id) if r: print("fund prev") SESSION.delete(r) r = RulesF(str(fed_id), rules) SESSION.add(r) try: SESSION.commit() return r except: SESSION.rollback() return False finally: SESSION.commit()
def un_fban_user(fed_id, user_id): with FEDS_LOCK: r = SESSION.query(BansF).all() for I in r: print("1") if I.fed_id == fed_id: print("2") print(I.user_id, user_id) if int(I.user_id) == int(user_id): print("fund prev") SESSION.delete(I) try: SESSION.commit() return I except: SESSION.rollback() return False finally: 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)