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,
),
)
14 changes: 13 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,18 @@ def do_follow():
}
)

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

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

unfollow(current_user, unfollowed_user)
return jsonify({"success": True})



@jwt_required()
def send_bloom():
Expand Down
3 changes: 3 additions & 0 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from custom_json_provider import CustomJsonProvider
from data.users import lookup_user
from endpoints import (
do_unfollow,
do_follow,
get_bloom,
hashtag,
Expand Down Expand Up @@ -54,7 +55,9 @@ 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/<username>", 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)
app.add_url_rule("/bloom/<id_str>", methods=["GET"], view_func=get_bloom)
Expand Down
38 changes: 32 additions & 6 deletions front-end/components/profile.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {apiService} from "../index.mjs";
import { apiService } from "../index.mjs";

/**
* Create a profile component
* @param {string} template - The ID of the template to clone
* @param {Object} profileData - The profile data to display
* @returns {DocumentFragment} - The profile UI
*/
function createProfile(template, {profileData, whoToFollow, isLoggedIn}) {
function createProfile(template, { profileData, whoToFollow, isLoggedIn }) {
if (!template || !profileData) return;
const profileElement = document
.getElementById(template)
Expand All @@ -15,11 +15,18 @@ function createProfile(template, {profileData, whoToFollow, isLoggedIn}) {
const usernameEl = profileElement.querySelector("[data-username]");
const bloomCountEl = profileElement.querySelector("[data-bloom-count]");
const followingCountEl = profileElement.querySelector(
"[data-following-count]"
"[data-following-count]",
);
const followerCountEl = profileElement.querySelector("[data-follower-count]");
const followButtonEl = profileElement.querySelector("[data-action='follow']");
const whoToFollowContainer = profileElement.querySelector(".profile__who-to-follow");
const unfollowButtonEl = profileElement.querySelector(
"[data-action='unfollow']",
);
// console.log("follow btn", followButtonEl);
// console.log("unfollow btn", unfollowButtonEl);
const whoToFollowContainer = profileElement.querySelector(
".profile__who-to-follow",
);
// Populate with data
usernameEl.querySelector("h2").textContent = profileData.username || "";
usernameEl.setAttribute("href", `/profile/${profileData.username}`);
Expand All @@ -32,9 +39,17 @@ function createProfile(template, {profileData, whoToFollow, isLoggedIn}) {
if (!isLoggedIn) {
followButtonEl.style.display = "none";
}
unfollowButtonEl.setAttribute("data-username", profileData.username || "");
unfollowButtonEl.hidden = profileData.is_self || !profileData.is_following;
unfollowButtonEl.addEventListener("click", handleUnfollow);
if (!isLoggedIn) {
followButtonEl.style.display = "none";
}

if (whoToFollow.length > 0) {
const whoToFollowList = whoToFollowContainer.querySelector("[data-who-to-follow]");
const whoToFollowList = whoToFollowContainer.querySelector(
"[data-who-to-follow]",
);
const whoToFollowTemplate = document.querySelector("#who-to-follow-chip");
for (const userToFollow of whoToFollow) {
const wtfElement = whoToFollowTemplate.content.cloneNode(true);
Expand Down Expand Up @@ -63,7 +78,18 @@ async function handleFollow(event) {
if (!username) return;

await apiService.followUser(username);
// await apiService.getProfile(username);
await apiService.getWhoToFollow();
}

async function handleUnfollow(event) {
const button = event.target;
const username = button.getAttribute("data-username");
if (!username) return;

await apiService.unfollowUser(username);
// await apiService.getProfile(username);
await apiService.getWhoToFollow();
}

export {createProfile, handleFollow};
export { createProfile, handleFollow, handleUnfollow };
23 changes: 11 additions & 12 deletions front-end/index.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Purple Forest</title>
<link href="/index.css" rel="stylesheet" />
<link href="./index.css" rel="stylesheet" />
</head>
<body id="app">
<header>
<a href="/"
><h1>
<img
src="/logo.svg"
alt="Purple Forest "
width="50"
height="60"
/>
<img src="./logo.svg" alt="Purple Forest " width="50" height="60" />
PurpleForest
</h1></a
>
Expand Down Expand Up @@ -187,10 +182,12 @@ <h1 id="signup-heading" class="signup__title">Create your account</h1>
<div class="profile__actions">
<button type="button" data-action="follow">Follow</button>
</div>
<div class="profile__actions">
<button type="button" data-action="unfollow">Unfollow</button>
</div>
<div class="profile__who-to-follow">
<h4>Who to follow</h4>
<ul data-who-to-follow>
</ul>
<ul data-who-to-follow></ul>
</div>
</section>
</template>
Expand Down Expand Up @@ -236,7 +233,9 @@ <h2 id="bloom-form-title" class="bloom-form__title">Share a Bloom</h2>
<article class="bloom box" data-bloom data-bloom-id="">
<div class="bloom__header flex">
<a href="#" class="bloom__username" data-username>Username</a>
<a href="#" class="bloom__time"><time class="bloom__time" data-time>2m</time></a>
<a href="#" class="bloom__time"
><time class="bloom__time" data-time>2m</time></a
>
</div>
<div class="bloom__content" data-content></div>
</article>
Expand All @@ -256,6 +255,6 @@ <h2 id="bloom-form-title" class="bloom-form__title">Share a Bloom</h2>
<p>Please enable JavaScript in your browser.</p>
</noscript>

<script type="module" src="/index.mjs"></script>
<script type="module" src="./index.mjs"></script>
</body>
</html>
Loading