diff --git a/backend/data/follows.py b/backend/data/follows.py index a4b6314e..3c1758ea 100644 --- a/backend/data/follows.py +++ b/backend/data/follows.py @@ -21,6 +21,17 @@ def follow(follower: User, followee: User): pass +def unfollow(follower: User, followee: User) -> None: + with db_cursor() as cur: + cur.execute( + "DELETE FROM follows WHERE follower = %(follower_id)s AND followee = %(followee_id)s", + dict( + follower_id=follower.id, + followee_id=followee.id, + ), + ) + + def get_followed_usernames(follower: User) -> List[str]: """get_followed_usernames returns a list of usernames followee follows.""" with db_cursor() as cur: diff --git a/backend/endpoints.py b/backend/endpoints.py index 0e177a07..f6d69429 100644 --- a/backend/endpoints.py +++ b/backend/endpoints.py @@ -1,6 +1,6 @@ from typing import Dict, Union from data import blooms -from data.follows import follow, get_followed_usernames, get_inverse_followed_usernames +from data.follows import unfollow, follow, get_followed_usernames, get_inverse_followed_usernames from data.users import ( UserRegistrationError, get_suggested_follows, @@ -150,6 +150,29 @@ def do_follow(): ) +@jwt_required() +def do_unfollow(): + type_check_error = verify_request_fields({"unfollow_username": str}) + if type_check_error is not None: + return type_check_error + + current_user = get_current_user() + + unfollow_username = request.json["unfollow_username"] + unfollow_user = get_user(unfollow_username) + if unfollow_user is None: + return make_response( + (f"Cannot unfollow {unfollow_username} - user does not exist", 404) + ) + + unfollow(current_user, unfollow_user) + return jsonify( + { + "success": True, + } + ) + + @jwt_required() def send_bloom(): type_check_error = verify_request_fields({"content": str}) diff --git a/backend/main.py b/backend/main.py index 7ba155fa..a3c1c8d7 100644 --- a/backend/main.py +++ b/backend/main.py @@ -14,6 +14,7 @@ send_bloom, suggested_follows, user_blooms, + do_unfollow, ) from dotenv import load_dotenv @@ -55,6 +56,7 @@ def main(): app.add_url_rule("/profile/", view_func=other_profile) app.add_url_rule("/follow", methods=["POST"], view_func=do_follow) app.add_url_rule("/suggested-follows/", view_func=suggested_follows) + app.add_url_rule("/unfollow", methods=["POST"], view_func=do_unfollow) app.add_url_rule("/bloom", methods=["POST"], view_func=send_bloom) app.add_url_rule("/bloom/", methods=["GET"], view_func=get_bloom) diff --git a/front-end/components/profile.mjs b/front-end/components/profile.mjs index ec4f2009..46a4e5a1 100644 --- a/front-end/components/profile.mjs +++ b/front-end/components/profile.mjs @@ -27,7 +27,8 @@ function createProfile(template, {profileData, whoToFollow, isLoggedIn}) { followerCountEl.textContent = profileData.followers?.length || 0; followingCountEl.textContent = profileData.follows?.length || 0; followButtonEl.setAttribute("data-username", profileData.username || ""); - followButtonEl.hidden = profileData.is_self || profileData.is_following; + followButtonEl.hidden = profileData.is_self; + followButtonEl.textContent = profileData.is_following ? "Unfollow" : "Follow"; followButtonEl.addEventListener("click", handleFollow); if (!isLoggedIn) { followButtonEl.style.display = "none"; @@ -62,7 +63,11 @@ async function handleFollow(event) { const username = button.getAttribute("data-username"); if (!username) return; - await apiService.followUser(username); + if (button.textContent === "Follow") { + await apiService.followUser(username); + } else { + await apiService.unfollowUser(username); + } await apiService.getWhoToFollow(); } diff --git a/front-end/lib/api.mjs b/front-end/lib/api.mjs index f4b5339b..f60b3529 100644 --- a/front-end/lib/api.mjs +++ b/front-end/lib/api.mjs @@ -261,8 +261,9 @@ async function followUser(username) { async function unfollowUser(username) { try { - const data = await _apiRequest(`/unfollow/${username}`, { + const data = await _apiRequest(`/unfollow`, { method: "POST", + body: JSON.stringify({ unfollow_username: username }), }); if (data.success) {