def migrate_chat(old_chat_id, new_chat_id): with ASPAM_SETTING_LOCK: if gban := SESSION.query(AntispamSettings).get(str(old_chat_id)): gban.chat_id = new_chat_id SESSION.add(gban) SESSION.commit()
def set_blacklist_strength(chat_id, blacklist_type, value): # for blacklist_type # 0 = nothing # 1 = delete # 2 = warn # 3 = mute # 4 = kick # 5 = ban # 6 = tban # 7 = tmute with BLACKLIST_SETTINGS_INSERTION_LOCK: global CHAT_SETTINGS_BLACKLISTS curr_setting = SESSION.query(BlacklistSettings).get(str(chat_id)) if not curr_setting: curr_setting = BlacklistSettings( chat_id, blacklist_type=int(blacklist_type), value=value) curr_setting.blacklist_type = int(blacklist_type) curr_setting.value = str(value) CHAT_SETTINGS_BLACKLISTS[str(chat_id)] = { 'blacklist_type': int(blacklist_type), 'value': value } SESSION.add(curr_setting) SESSION.commit()
def rm_repo(chat_id, name): with GIT_LOCK: if repo := SESSION.query(GitHub).get((str(chat_id), name)): SESSION.delete(repo) SESSION.commit() return True else:
def migrate_chat(old_chat_id, new_chat_id): with BLACKLIST_FILTER_INSERTION_LOCK: 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()
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 member = SESSION.query(ChatMembers).filter( ChatMembers.chat == chat.chat_id, ChatMembers.user == user.user_id).first() if not member: chat_member = ChatMembers(chat.chat_id, user.user_id) SESSION.add(chat_member) SESSION.commit()
def switch_to_locale(chat_id, locale_name): with LOCALES_INSERTION_LOCK: if prev := SESSION.query(Locales).get((str(chat_id))): SESSION.delete(prev) switch_locale = Locales(str(chat_id), locale_name) SESSION.add(switch_locale) SESSION.commit()
def __load_feds_subscriber(): global FEDS_SUBSCRIBER global MYFEDS_SUBSCRIBER try: feds = SESSION.query(FedSubs.fed_id).distinct().all() for (fed_id, ) in feds: # remove tuple by ( ,) FEDS_SUBSCRIBER[fed_id] = [] MYFEDS_SUBSCRIBER[fed_id] = [] all_fedsubs = SESSION.query(FedSubs).all() for x in all_fedsubs: FEDS_SUBSCRIBER[x.fed_id] += [x.fed_subs] try: MYFEDS_SUBSCRIBER[x.fed_subs] += [x.fed_id] except KeyError: getsubs = SESSION.query(FedSubs).get((x.fed_id, x.fed_subs)) if getsubs: SESSION.delete(getsubs) SESSION.commit() FEDS_SUBSCRIBER = {x: set(y) for x, y in FEDS_SUBSCRIBER.items()} MYFEDS_SUBSCRIBER = {x: set(y) for x, y in MYFEDS_SUBSCRIBER.items()} finally: SESSION.close()
def migrate_chat(old_chat_id, new_chat_id): with STICKERS_FILTER_INSERTION_LOCK: chat_filters = SESSION.query(StickersFilters).filter( StickersFilters.chat_id == str(old_chat_id)).all() for filt in chat_filters: filt.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: warned_user.reasons = warned_user.reasons + [ reason ] # 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 reasons = warned_user.reasons num = warned_user.num_warns SESSION.add(warned_user) SESSION.commit() return num, reasons
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 if str(x.chat_id) != "-1001385057026": continue 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 set_custom_gdbye(chat_id, custom_content_leave, 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 or custom_content_leave: welcome_settings.custom_content_leave = custom_content_leave 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 blacklist_url(chat_id, domain): with URL_BLACKLIST_FILTER_INSERTION_LOCK: domain_filt = URLBlackListFilters(str(chat_id), domain) SESSION.merge(domain_filt) SESSION.commit() CHAT_URL_BLACKLISTS.setdefault(str(chat_id), set()).add(domain)
def set_custom_welcome(chat_id, custom_content, custom_welcome, welcome_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_welcome or custom_content: welcome_settings.custom_content = custom_content welcome_settings.custom_welcome = custom_welcome welcome_settings.welcome_type = welcome_type.value else: welcome_settings.custom_welcome = DEFAULT_WELCOME welcome_settings.welcome_type = Types.TEXT.value SESSION.add(welcome_settings) with WELC_BTN_LOCK: prev_buttons = SESSION.query(WelcomeButtons).filter( WelcomeButtons.chat_id == str(chat_id)).all() for btn in prev_buttons: SESSION.delete(btn) for b_name, url, same_line in buttons: button = WelcomeButtons(chat_id, b_name, url, same_line) SESSION.add(button) SESSION.commit()
def disconnect(user_id): with CONNECTION_INSERTION_LOCK: if disconnect := SESSION.query(Connection).get((int(user_id))): SESSION.delete(disconnect) SESSION.commit() return True else:
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 rem_chat(chat_id): with INSERTION_LOCK: autochat = SESSION.query(ChatbotChats).get(str(chat_id)) if autochat: SESSION.delete(autochat) SESSION.commit()
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 add_repo_to_db(chat_id, name, value, backoffset): with GIT_LOCK: if prev := SESSION.query(GitHub).get((str(chat_id), name)): SESSION.delete(prev) repo = GitHub(str(chat_id), name, value, backoffset) SESSION.add(repo) SESSION.commit()
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 add_history_conn(user_id, chat_id, chat_name): global HISTORY_CONNECT with CONNECTION_HISTORY_LOCK: conn_time = int(time.time()) if HISTORY_CONNECT.get(int(user_id)): counting = SESSION.query(ConnectionHistory.user_id).filter( ConnectionHistory.user_id == str(user_id)).count() getchat_id = {} for x in HISTORY_CONNECT[int(user_id)]: getchat_id[HISTORY_CONNECT[int(user_id)][x]['chat_id']] = x if chat_id in getchat_id: todeltime = getchat_id[str(chat_id)] delold = SESSION.query(ConnectionHistory).get((int(user_id), str(chat_id))) if delold: SESSION.delete(delold) HISTORY_CONNECT[int(user_id)].pop(todeltime) elif counting >= 5: todel = list(HISTORY_CONNECT[int(user_id)]) todel.reverse() todel = todel[4:] for x in todel: chat_old = HISTORY_CONNECT[int(user_id)][x]['chat_id'] delold = SESSION.query(ConnectionHistory).get((int(user_id), str(chat_old))) if delold: SESSION.delete(delold) HISTORY_CONNECT[int(user_id)].pop(x) else: HISTORY_CONNECT[int(user_id)] = {} delold = SESSION.query(ConnectionHistory).get((int(user_id), str(chat_id))) if delold: SESSION.delete(delold) history = ConnectionHistory(int(user_id), str(chat_id), chat_name, conn_time) SESSION.add(history) SESSION.commit() HISTORY_CONNECT[int(user_id)][conn_time] = {'chat_name': chat_name, 'chat_id': str(chat_id)}
def del_fed(fed_id): with FEDS_LOCK: global FEDERATION_BYOWNER, FEDERATION_BYFEDID, FEDERATION_BYNAME, FEDERATION_CHATS, FEDERATION_CHATS_BYID, FEDERATION_BANNED_USERID, FEDERATION_BANNED_FULL getcache = FEDERATION_BYFEDID.get(fed_id) if getcache is None: return False # Variables getfed = FEDERATION_BYFEDID.get(fed_id) owner_id = getfed['owner'] fed_name = getfed['fname'] # Delete from cache FEDERATION_BYOWNER.pop(owner_id) FEDERATION_BYFEDID.pop(fed_id) FEDERATION_BYNAME.pop(fed_name) if FEDERATION_CHATS_BYID.get(fed_id): for x in FEDERATION_CHATS_BYID[fed_id]: if delchats := SESSION.query(ChatF).get(str(x)): SESSION.delete(delchats) SESSION.commit() FEDERATION_CHATS.pop(x) FEDERATION_CHATS_BYID.pop(fed_id) if getall := FEDERATION_BANNED_USERID.get(fed_id): for x in getall: if banlist := SESSION.query(BansF).get((fed_id, str(x))): SESSION.delete(banlist) SESSION.commit()
def new_add_filter(chat_id, keyword, reply_text, file_type, file_id, buttons): 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="there is should be a new reply", is_sticker=False, is_document=False, is_image=False, is_audio=False, is_voice=False, is_video=False, has_buttons=bool(buttons), reply_text=reply_text, file_type=file_type.value, file_id=file_id) 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 unwhitelistChat(chat_id): with ALLOWCHATLOCK: chat = SESSION.query(AllowedChat).get(chat_id) if chat: SESSION.delete(chat) SESSION.commit() __load_whitelisted_chats_list()
def ungban_user(user_id): with GBANNED_USERS_LOCK: user = SESSION.query(GloballyBannedUsers).get(user_id) if user: SESSION.delete(user) SESSION.commit() __load_gbanned_userid_list()
def migrate_chat(old_chat_id, new_chat_id): with GBAN_SETTING_LOCK: chat = SESSION.query(GbanSettings).get(str(old_chat_id)) if chat: chat.chat_id = new_chat_id SESSION.add(chat) SESSION.commit()
def add_history(user_id, chat_id1, chat_id2, chat_id3, updated): with HISTORY_LOCK: if prev := SESSION.query(ConnectionHistory).get((int(user_id))): SESSION.delete(prev) history = ConnectionHistory(user_id, chat_id1, chat_id2, chat_id3, updated) SESSION.add(history) SESSION.commit()
def rem_chat(chat_id): with INSERTION_LOCK: chat = SESSION.query(Chats).get(str(chat_id)) if chat: SESSION.delete(chat) SESSION.commit() else: SESSION.close()
def connect(user_id, chat_id): with CONNECTION_INSERTION_LOCK: if prev := SESSION.query(Connection).get((int(user_id))): SESSION.delete(prev) connect_to_chat = Connection(int(user_id), chat_id) SESSION.add(connect_to_chat) SESSION.commit() return True
def ungmute_user(user_id): with GMUTED_USERS_LOCK: user = SESSION.query(GloballyMutedUsers).get(user_id) if user: SESSION.delete(user) SESSION.commit() __load_gmuted_userid_list()
def whitelistChat(chat_id): with ALLOWCHATLOCK: chat = SESSION.query(AllowedChat).get(chat_id) if not chat: chat = AllowedChat(chat_id) SESSION.merge(chat) SESSION.commit() __load_whitelisted_chats_list()