async def get_profile_by_username( self, *, username: str, requested_user: Optional[UserLike]) -> Profile: user = await self._users_repo.get_user_by_username(username=username) profile = Profile(username=user.username, bio=user.bio, image=user.image) if requested_user: profile.following = await self.is_user_following_for_another_user( target_user=user, requested_user=requested_user) return profile
async def follow_for_user( profile: Profile = Depends(get_profile_by_username_from_path), user: User = Depends(get_current_user_authorizer()), profiles_repo: ProfilesRepository = Depends( get_repository(ProfilesRepository)), ) -> ProfileInResponse: if user.username == profile.username: raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail=strings.UNABLE_TO_FOLLOW_YOURSELF) if profile.following: raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail=strings.USER_IS_ALREADY_FOLLOWED) await profiles_repo.add_user_into_followers(target_user=profile, requested_user=user) return ProfileInResponse(profile=profile.copy(update={"following": True}))
async def unsubscribe_from_user( profile: Profile = Depends(get_profile_by_username_from_path), user: User = Depends(get_current_user_authorizer()), profiles_repo: ProfilesRepository = Depends( get_repository(ProfilesRepository)), ) -> ProfileInResponse: if user.username == profile.username: raise HTTPException( status_code=HTTP_400_BAD_REQUEST, detail=strings.UNABLE_TO_UNSUBSCRIBE_FROM_YOURSELF, ) if not profile.following: raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail=strings.USER_IS_NOT_FOLLOWED) await profiles_repo.remove_user_from_followers(target_user=profile, requested_user=user) return ProfileInResponse(profile=profile.copy(update={"following": False}))