-
-
Notifications
You must be signed in to change notification settings - Fork 69
London | 26-SDC-March | Zobeir Rigi | Legacy-Code | Sprint 1 | Add ability to unfollow users #253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
| ), | ||
| ) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you add newline in the end of the file please? |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| from data.users import lookup_user | ||
| from endpoints import ( | ||
| do_follow, | ||
| do_unfollow, | ||
| get_bloom, | ||
| hashtag, | ||
| home_timeline, | ||
|
|
@@ -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( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you reformat it to match the surrounding coding style? (overall, even if you want to keep it multiline, the arguments should be indented additionally, like this: |
||
| "/unfollow/<profile_username>", | ||
| methods=["POST"], | ||
| view_func=do_unfollow, | ||
| ) | ||
|
|
||
| app.run(host="0.0.0.0", port="3000", debug=True) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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"; | ||
|
|
@@ -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") { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you decide whether to follow or unfollow by checking button.textContent === "Unfollow". That works right now, but have a think: textContent is the text a human reads on the screen. What happens to this logic the day someone renames the button to "Stop following", or the app gets translated into another language? You're already storing a piece of state on this button so the handler can read it later — see the data-username attribute a few lines up. Could the follow/unfollow state travel the same way, instead of being inferred from the label? Have a look at how data-username is set and then read back, and see if you can apply the same idea here. |
||
| await apiService.unfollowUser(username); | ||
| } else { | ||
| await apiService.followUser(username); | ||
| } | ||
|
|
||
| await apiService.getWhoToFollow(); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parameterized DELETE — correct and injection-safe, and consistent with follow(). Good instinct.