Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions backend/data/follows.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@ def get_inverse_followed_usernames(followee: User) -> List[str]:
)
rows = cur.fetchall()
return [row[0] for row in rows]

def unfollow(follower: User, followee: User):
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,
),
)
20 changes: 19 additions & 1 deletion backend/endpoints.py
Original file line number Diff line number Diff line change
@@ -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 follow, unfollow, get_followed_usernames, get_inverse_followed_usernames
from data.users import (
UserRegistrationError,
get_suggested_follows,
Expand Down Expand Up @@ -245,3 +245,21 @@ def verify_request_fields(names_to_types: Dict[str, type]) -> Union[Response, No
)
)
return None

@jwt_required()
def do_unfollow(profile_username):
current_user = get_current_user()

follow_user = get_user(profile_username)
if follow_user is None:
return make_response(
(f"Cannot unfollow {profile_username} - user does not exist", 404)
)

unfollow(current_user, follow_user)

return jsonify(
{
"success": True,
}
)
6 changes: 6 additions & 0 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from data.users import lookup_user
from endpoints import (
do_follow,
do_unfollow,
get_bloom,
hashtag,
home_timeline,
Expand Down Expand Up @@ -60,6 +61,11 @@ def main():
app.add_url_rule("/bloom/<id_str>", methods=["GET"], view_func=get_bloom)
app.add_url_rule("/blooms/<profile_username>", view_func=user_blooms)
app.add_url_rule("/hashtag/<hashtag>", view_func=hashtag)
app.add_url_rule(
"/unfollow/<profile_username>",
methods=["POST"],
view_func=do_unfollow,
)

app.run(host="0.0.0.0", port="3000", debug=True)

Expand Down
12 changes: 10 additions & 2 deletions front-end/components/profile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ 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";
Expand Down Expand Up @@ -62,7 +65,12 @@ async function handleFollow(event) {
const username = button.getAttribute("data-username");
if (!username) return;

await apiService.followUser(username);
if (button.textContent === "Unfollow") {
await apiService.unfollowUser(username);
} else {
await apiService.followUser(username);
}

await apiService.getWhoToFollow();
}

Expand Down