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
11 changes: 10 additions & 1 deletion backend/data/follows.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ def get_followed_usernames(follower: 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,
),
)

def get_inverse_followed_usernames(followee: User) -> List[str]:
"""get_followed_usernames returns a list of usernames followed by follower."""
with db_cursor() as cur:
Expand Down
25 changes: 24 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 @@ -149,6 +149,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():
Expand Down
2 changes: 2 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 @@ -54,6 +55,7 @@ def main():
app.add_url_rule("/profile", view_func=self_profile)
app.add_url_rule("/profile/<profile_username>", view_func=other_profile)
app.add_url_rule("/follow", methods=["POST"], view_func=do_follow)
app.add_url_rule("/unfollow", methods=["POST"], view_func=do_unfollow)
app.add_url_rule("/suggested-follows/<limit_str>", view_func=suggested_follows)

app.add_url_rule("/bloom", methods=["POST"], view_func=send_bloom)
Expand Down
20 changes: 18 additions & 2 deletions front-end/components/profile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ 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;
if (profileData.is_self) {
followButtonEl.hidden = true;
} else {
if (profileData.is_following) {
followButtonEl.textContent = "Unfollow";
} else {
followButtonEl.textContent = "Follow";
}
followButtonEl.hidden = false;
}
followButtonEl.addEventListener("click", handleFollow);
if (!isLoggedIn) {
followButtonEl.style.display = "none";
Expand Down Expand Up @@ -62,7 +71,14 @@ async function handleFollow(event) {
const username = button.getAttribute("data-username");
if (!username) return;

await apiService.followUser(username);
const isUnfollowing = button.textContent === "Unfollow";

if (isUnfollowing) {
await apiService.unfollowUser(username);
} else {
await apiService.followUser(username);
}

await apiService.getWhoToFollow();
}

Expand Down
3 changes: 2 additions & 1 deletion front-end/lib/api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down