def migrate_chat(old_chat_id, new_chat_id): with INSERTION_LOCK: chat = SESSION.query(Chats).get(str(old_chat_id)) if chat: chat.chat_id = str(new_chat_id) SESSION.add(chat) SESSION.flush() chat_members = SESSION.query(ChatMembers).filter(ChatMembers.chat == str(old_chat_id)).all() for member in chat_members: member.chat = str(new_chat_id) SESSION.add(member) SESSION.commit()
def migrate_chat(old_chat_id, new_chat_id): with CUST_FILT_LOCK: chat_filters = SESSION.query(CustomFilters).filter( CustomFilters.chat_id == str(old_chat_id)).all() for filt in chat_filters: filt.chat_id = str(new_chat_id) SESSION.commit() CHAT_FILTERS[str(new_chat_id)] = CHAT_FILTERS[str(old_chat_id)] del CHAT_FILTERS[str(old_chat_id)] with BUTTON_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 global_unignore_command(command): command = command.lower() with CLEANER_GLOBAL_LOCK: unignored = SESSION.query(CleanerBlueTextGlobal).get(str(command)) if unignored: if command in GLOBAL_IGNORE_COMMANDS: GLOBAL_IGNORE_COMMANDS.remove(command) SESSION.delete(unignored) SESSION.commit() return True SESSION.close() return False
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 global_ignore_command(command): command = command.lower() with CLEANER_GLOBAL_LOCK: ignored = SESSION.query(CleanerBlueTextGlobal).get(str(command)) if not ignored: GLOBAL_IGNORE_COMMANDS.add(command) ignored = CleanerBlueTextGlobal(str(command)) SESSION.add(ignored) SESSION.commit() return True SESSION.close() return False
def rm_note(chat_id, note_name): with NOTES_INSERTION_LOCK: note = SESSION.query(Notes).get((str(chat_id), note_name)) if note: with BUTTONS_INSERTION_LOCK: buttons = SESSION.query(Buttons).filter(Buttons.chat_id == str(chat_id), Buttons.note_name == note_name).all() for btn in buttons: SESSION.delete(btn) SESSION.delete(note) SESSION.commit() return True else: SESSION.close() return False
def fban_user(fed_id, user_id, reason): 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) r = BansF(str(fed_id), user_id, reason) SESSION.add(r) SESSION.commit() return r
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: warned_user.reasons = warned_user.reasons + [reason] # TODO:: double check this wizardry reasons = warned_user.reasons num = warned_user.num_warns SESSION.add(warned_user) SESSION.commit() return num, reasons
def set_cleanbt(chat_id, is_enable): with CLEANER_CHAT_SETTINGS: curr = SESSION.query(CleanerBlueTextChatSettings).get(str(chat_id)) if not curr: curr = CleanerBlueTextChatSettings(str(chat_id), is_enable) else: curr.is_enabled = is_enable if str(chat_id) not in CLEANER_CHATS: CLEANER_CHATS.setdefault(str(chat_id), {"setting": False, "commands": set()}) CLEANER_CHATS[str(chat_id)]["setting"] = is_enable SESSION.add(curr) SESSION.commit()
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 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 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 chat_unignore_command(chat_id, unignore): unignore = unignore.lower() with CLEANER_CHAT_LOCK: unignored = SESSION.query(CleanerBlueTextChat).get((str(chat_id), unignore)) if unignored: if str(chat_id) not in CLEANER_CHATS: CLEANER_CHATS.setdefault(str(chat_id), {"setting": False, "commands": set()}) if unignore in CLEANER_CHATS.get(str(chat_id)).get("commands"): CLEANER_CHATS[str(chat_id)]["commands"].remove(unignore) SESSION.delete(unignored) SESSION.commit() return True SESSION.close() return False
def chat_ignore_command(chat_id, ignore): ignore = ignore.lower() with CLEANER_CHAT_LOCK: ignored = SESSION.query(CleanerBlueTextChat).get((str(chat_id), ignore)) if not ignored: if str(chat_id) not in CLEANER_CHATS: CLEANER_CHATS.setdefault(str(chat_id), {"setting": False, "commands": set()}) CLEANER_CHATS[str(chat_id)]["commands"].add(ignore) ignored = CleanerBlueTextChat(str(chat_id), ignore) SESSION.add(ignored) SESSION.commit() return True SESSION.close() return False
def update_restriction(chat_id, restr_type, locked): with RESTR_LOCK: curr_restr = SESSION.query(Restrictions).get(str(chat_id)) if not curr_restr: curr_restr = init_restrictions(chat_id) if restr_type == "messages": curr_restr.messages = locked elif restr_type == "media": curr_restr.media = locked elif restr_type == "other": curr_restr.other = locked elif restr_type == "previews": curr_restr.preview = locked elif restr_type == "all": curr_restr.messages = locked curr_restr.media = locked curr_restr.other = locked curr_restr.preview = locked SESSION.add(curr_restr) 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 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() WARN_FILTERS[str(new_chat_id)] = WARN_FILTERS[str(old_chat_id)] 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 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 ensure_bot_in_db(): with INSERTION_LOCK: bot = Users(dispatcher.bot.id, dispatcher.bot.username) SESSION.merge(bot) SESSION.commit()
def new_fed(owner_id, fed_id, fed_name): with FEDS_LOCK: r = Federations(str(owner_id), fed_id, fed_name) SESSION.add(r) SESSION.commit() return r
def migrate_chat(old_chat_id, new_chat_id): with INSERTION_LOCK: chat = SESSION.query(Rules).get(str(old_chat_id)) if chat: chat.chat_id = str(new_chat_id) SESSION.commit()
def user_join_fed(fed_id, user_id): with FEDS_LOCK: r = UserF(user_id, fed_id) SESSION.add(r) SESSION.commit() return r
def add_note_button_to_db(chat_id, keyword, b_name, url, same_line): with BUTTON_LOCK: button = Buttons(chat_id, keyword, b_name, url, same_line) SESSION.add(button) SESSION.commit()
def add_url(tg_chat_id, tg_feed_link, tg_old_entry_link): with INSERTION_LOCK: action = RSS(tg_chat_id, tg_feed_link, tg_old_entry_link) SESSION.add(action) SESSION.commit()
def chat_join_fed(fed_id, chat_id): with FEDS_LOCK: r = ChatF(chat_id, fed_id) SESSION.add(r) SESSION.commit() return r