def set_nsfw(chat_id): with INSERTION_LOCK: nsfwchat = SESSION.query(NSFWChats).get(str(chat_id)) if not nsfwchat: nsfwchat = NSFWChats(str(chat_id)) SESSION.add(nsfwchat) 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 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 del_user(user_id): with INSERTION_LOCK: curr = SESSION.query(Users).get(user_id) if curr: SESSION.delete(curr) SESSION.commit() return True ChatMembers.query.filter(ChatMembers.user == user_id).delete() SESSION.commit() SESSION.close() return False
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 ensure_bot_in_db(): with INSERTION_LOCK: bot = Users(dispatcher.bot.id, dispatcher.bot.username) SESSION.merge(bot) SESSION.commit()
def rem_nsfw(chat_id): with INSERTION_LOCK: nsfwchat = SESSION.query(NSFWChats).get(str(chat_id)) if nsfwchat: SESSION.delete(nsfwchat) SESSION.commit()