def users_load(): users = User.objects() list_users = [] for user in users: list_users.append(user.to_dict()) return list_users
def authenticate_user(self, username: str, password: str): user = User.objects(username=username).first() if not user: return None if not self.verify_password(password, user.password): return None return user
def get_public_user_info(user_id: str): user = User.objects(id=ObjectId(user_id)).first() if user: return { "username": user.username, "name": user.name, "user_id": str(user.id), "avatar": user.avatar, } return None
def get_members(room_id: str): members_id = RoomMember.objects(room_id=room_id) list_members = [] for member in members_id: user = User.objects(id=ObjectId(member.member_id)).first() _user = user.to_dict() _user["is_owner"] = member.is_owner list_members.append(_user) return list_members
def user_delete(user_id: str): user = User.objects(id=user_id) members = RoomMember.objects(member_id=str(user_id)) if not members: user.delete() return True for member in members: if member.is_owner: return False user.delete() member.delete() return True
def update_current_user( user_id: str, username: str, avatar: Optional[str] = None, name: Optional[str] = None, ): user = User.objects(id=ObjectId(user_id)) fileds = { "username": username, "avatar": avatar, "name": name, } user.update(**fileds) return True
def rooms_load(): rooms = Room.objects() list_rooms = [] for room in rooms: members = RoomMember.objects(room_id=str(room.id)) messages_count = Messages.objects(room_id=str(room.id)).count() _room = room.to_dict() last_message = mess_logic.get_last_message(str(room.id)) try: last_message['timestamp'] = parse( last_message['created_at']).strftime('%d %b,%Y %H:%M') except: pass _room['last_message'] = last_message _room["member"] = [] _room["messages_count"] = messages_count for member in members: user = User.objects(id=ObjectId(member.member_id)).first() _room["member"].append(user.to_dict()) list_rooms.append(_room) return list_rooms
def user_disable(user_id: str): user = User.objects(id=ObjectId(user_id)).first() active = not user.active user.update(active=active) return True
def get_user_id(username: str): user_id = User.objects(username=username).first().id if user_id: return user_id return False
def check_user_active(username: str): active = User.objects(username=username).first().active if active: return True return False
def disabe_user(username: str): user = User.objects(username=username).first() user.update(active=False) return True
def check_user_exist(user_id: str): user = User.objects(id=ObjectId(user_id)).first() if user: return True return False
def get_user_profile(username: str): user = User.objects(username=username).first() return user.to_dict()
def get_user(username: str): return User.objects(username=username).first()
def get_user_by_id(user_id: str): return User.objects(id=ObjectId(user_id)).first()