def kread(chat): if SESSION.query(KRead).get((str(chat))): return False adder = KRead(str(chat)) SESSION.add(adder) SESSION.commit() return True
def gmuted_users(): try: return SESSION.query(GMute).all() except BaseException: return None finally: SESSION.close()
def unmute(chat_id, sender): rem = (SESSION.query(Mute).filter(Mute.chat_id == str(chat_id), Mute.sender == str(sender)).all()) if len(rem): for item in rem: SESSION.delete(item) SESSION.commit()
def gbanned_users(): try: return SESSION.query(GBan).all() except BaseException: return None finally: SESSION.close()
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 unkread(chat): rem = SESSION.query(KRead).get((str(chat))) if rem: SESSION.delete(rem) SESSION.commit() return True return False
def is_kread(): try: return SESSION.query(KRead).all() except BaseException: return None finally: SESSION.close()
def is_gmuted(sender): try: ret = SESSION.query(GMute).filter(GMute.sender == str(sender)).all() return len(ret) > 0 except BaseException: return None finally: SESSION.close()
def is_approved(chat_id): try: return SESSION.query(PMPermit).filter( PMPermit.chat_id == str(chat_id)).one() except BaseException: return None finally: SESSION.close()
def rm_note(chat_id, keyword): to_check = get_note(chat_id, keyword) if not to_check: return False else: SESSION.delete(to_check) SESSION.commit() return True
def remove_snip(keyword): to_check = get_snip(keyword) if not to_check: return False else: SESSION.delete(to_check) SESSION.commit() return True
def is_muted(chat_id, sender): try: ret = (SESSION.query(Mute).filter(Mute.chat_id == str(chat_id), Mute.sender == str(sender)).all()) return len(ret) > 0 except BaseException: return None finally: SESSION.close()
def __load_chat_blacklists(): global CHAT_BLACKLISTS try: chats = SESSION.query(BlackListFilters.chat_id).distinct().all() for (chat_id, ) in chats: # remove tuple by ( ,) 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 rm_from_blacklist(chat_id, trigger): with BLACKLIST_FILTER_INSERTION_LOCK: blacklist_filt = SESSION.query(BlackListFilters).get( (str(chat_id), trigger)) if blacklist_filt: # sanity check 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 SESSION.close() return False
def num_blacklist_chat_filters(chat_id): try: return (SESSION.query(BlackListFilters.chat_id).filter( BlackListFilters.chat_id == str(chat_id)).count()) finally: SESSION.close()
def num_blacklist_filters(): try: return SESSION.query(BlackListFilters).count() finally: SESSION.close()
def add_snip(keyword, reply, f_mesg_id): to_check = get_snip(keyword) if not to_check: adder = Snips(keyword, reply, f_mesg_id) SESSION.add(adder) SESSION.commit() return True else: rem = SESSION.query(Snips).filter(Snips.snip == keyword) rem.delete() SESSION.commit() adder = Snips(keyword, reply, f_mesg_id) SESSION.add(adder) SESSION.commit() return False
def add_note(chat_id, keyword, reply, f_mesg_id): to_check = get_note(chat_id, keyword) if not to_check: adder = Notes(str(chat_id), keyword, reply, f_mesg_id) SESSION.add(adder) SESSION.commit() return True else: rem = SESSION.query(Notes).get((str(chat_id), keyword)) SESSION.delete(rem) SESSION.commit() adder = Notes(str(chat_id), keyword, reply, f_mesg_id) SESSION.add(adder) SESSION.commit() return False
def gban(sender): adder = GBan(str(sender)) SESSION.add(adder) SESSION.commit()
def get_note(chat_id, keyword): try: return SESSION.query(Notes).get((str(chat_id), keyword)) finally: SESSION.close()
def get_filter(chat_id, keyword): try: return SESSION.query(Filters).get((str(chat_id), keyword)) finally: SESSION.close()
def approve(chat_id): adder = PMPermit(str(chat_id)) SESSION.add(adder) SESSION.commit()
def dissprove(chat_id): rem = SESSION.query(PMPermit).get(str(chat_id)) if rem: SESSION.delete(rem) SESSION.commit()
def ungban(sender): rem = SESSION.query(GBan).get((str(sender))) if rem: SESSION.delete(rem) SESSION.commit()
def gmute(sender): adder = GMute(str(sender)) SESSION.add(adder) SESSION.commit()
def ungmute(sender): rem = SESSION.query(GMute).get((str(sender))) if rem: SESSION.delete(rem) SESSION.commit()
def num_blacklist_filter_chats(): try: return SESSION.query(func.count(distinct( BlackListFilters.chat_id))).scalar() finally: SESSION.close()
def get_notes(chat_id): try: return SESSION.query(Notes).filter(Notes.chat_id == str(chat_id)).all() finally: SESSION.close()
def mute(chat_id, sender): adder = Mute(str(chat_id), str(sender)) SESSION.add(adder) SESSION.commit()
def get_filters(chat_id): try: return SESSION.query(Filters).filter( Filters.chat_id == str(chat_id)).all() finally: SESSION.close()