diff --git a/backend/data/blooms.py b/backend/data/blooms.py index 7e280cf3..fc487d93 100644 --- a/backend/data/blooms.py +++ b/backend/data/blooms.py @@ -13,7 +13,18 @@ class Bloom: sender: User content: str sent_timestamp: datetime.datetime + activity_timestamp: datetime.datetime + rebloom_count: int +@dataclass +class RebloomView: + id: int + sender: str + content: str + sent_timestamp: datetime.datetime + rebloomer: str + activity_timestamp: datetime.datetime + rebloom_count: int def add_bloom(*, sender: User, content: str) -> Bloom: hashtags = [word[1:] for word in content.split(" ") if word.startswith("#")] @@ -36,6 +47,17 @@ def add_bloom(*, sender: User, content: str) -> Bloom: dict(hashtag=hashtag, bloom_id=bloom_id), ) +def add_rebloom(*, user_id: int, bloom_id: int) -> bool: + with db_cursor() as cur: + try: + cur.execute( + "INSERT INTO reblooms (user_id, bloom_id) VALUES (%(user_id)s, %(bloom_id)s) ON CONFLICT (user_id, bloom_id) DO NOTHING", dict(user_id=user_id, bloom_id=bloom_id) + ) + return True + except Exception as e: + print(f"cannot rebloom bloom: {bloom_id}") + return False + def get_blooms_for_user( username: str, *, before: Optional[int] = None, limit: Optional[int] = None @@ -54,7 +76,8 @@ def get_blooms_for_user( cur.execute( f"""SELECT - blooms.id, users.username, content, send_timestamp + blooms.id, users.username, content, send_timestamp, + (SELECT COUNT(*) FROM reblooms WHERE reblooms.bloom_id = blooms.id) AS rebloom_count FROM blooms INNER JOIN users ON users.id = blooms.sender_id WHERE @@ -68,33 +91,90 @@ def get_blooms_for_user( rows = cur.fetchall() blooms = [] for row in rows: - bloom_id, sender_username, content, timestamp = row + bloom_id, sender_username, content, timestamp, rebloom_count = row blooms.append( Bloom( id=bloom_id, sender=sender_username, content=content, sent_timestamp=timestamp, + activity_timestamp=timestamp, + rebloom_count=rebloom_count ) ) return blooms +def get_reblooms_for_user(username: str, *, before: Optional[int] = None, limit: Optional[int] = None) -> List[Bloom]: + with db_cursor() as cur: + kwargs = { + "username": username, + } + if before is not None: + before_clause = "AND send_timestamp < %(before_limit)s" + kwargs["before_limit"] = before + else: + before_clause = "" + + limit_clause = make_limit_clause(limit, kwargs) + + cur.execute( + f""" + SELECT + blooms.id, users.username AS sender, blooms.content, blooms.send_timestamp, rebloomer.username AS rebloomer, reblooms.rebloom_timestamp AS activity_time, + (SELECT COUNT(*) FROM reblooms WHERE reblooms.bloom_id = blooms.id) AS rebloom_count + FROM + reblooms + INNER JOIN blooms ON blooms.id = reblooms.bloom_id + INNER JOIN users ON users.id = blooms.sender_id + INNER JOIN users AS rebloomer ON rebloomer.id = reblooms.user_id + WHERE + rebloomer.username = %(username)s + {before_clause} + ORDER BY reblooms.rebloom_timestamp DESC + {limit_clause} + """, + kwargs + ) + rows = cur.fetchall() + + reblooms = [] + for row in rows: + bloom_id, send_username, content, timestamp, rebloomer_name, activity_time, rebloom_count = row + reblooms.append( + RebloomView( + id=bloom_id, + sender=send_username, + content=content, + sent_timestamp=timestamp, + rebloomer=rebloomer_name, + activity_timestamp=activity_time, + rebloom_count=rebloom_count + ) + ) + return reblooms + + def get_bloom(bloom_id: int) -> Optional[Bloom]: with db_cursor() as cur: cur.execute( - "SELECT blooms.id, users.username, content, send_timestamp FROM blooms INNER JOIN users ON users.id = blooms.sender_id WHERE blooms.id = %s", + """SELECT + blooms.id, users.username, content, send_timestamp, + (SELECT COUNT(*) FROM reblooms WHERE reblooms.bloom_id = blooms.id) AS rebloom_count + FROM blooms INNER JOIN users ON users.id = blooms.sender_id WHERE blooms.id = %s""", (bloom_id,), ) row = cur.fetchone() if row is None: return None - bloom_id, sender_username, content, timestamp = row + bloom_id, sender_username, content, timestamp, rebloom_count = row return Bloom( id=bloom_id, sender=sender_username, content=content, sent_timestamp=timestamp, + activity_timestamp=timestamp, + rebloom_count=rebloom_count ) @@ -108,7 +188,8 @@ def get_blooms_with_hashtag( with db_cursor() as cur: cur.execute( f"""SELECT - blooms.id, users.username, content, send_timestamp + blooms.id, users.username, content, send_timestamp, + (SELECT COUNT(*) FROM reblooms WHERE reblooms.bloom_id = blooms.id) AS rebloom_count FROM blooms INNER JOIN hashtags ON blooms.id = hashtags.bloom_id INNER JOIN users ON blooms.sender_id = users.id WHERE @@ -121,13 +202,15 @@ def get_blooms_with_hashtag( rows = cur.fetchall() blooms = [] for row in rows: - bloom_id, sender_username, content, timestamp = row + bloom_id, sender_username, content, timestamp, rebloom_count = row blooms.append( Bloom( id=bloom_id, sender=sender_username, content=content, sent_timestamp=timestamp, + activity_timestamp=timestamp, + rebloom_count=rebloom_count ) ) return blooms diff --git a/backend/endpoints.py b/backend/endpoints.py index 0e177a07..1de2cb2d 100644 --- a/backend/endpoints.py +++ b/backend/endpoints.py @@ -110,12 +110,16 @@ def other_profile(profile_username): current_user = get_current_user() followers = get_inverse_followed_usernames(profile_user) - all_blooms = blooms.get_blooms_for_user(profile_username) - all_blooms.reverse() + own_blooms = blooms.get_blooms_for_user(profile_username) + shared_blooms = blooms.get_reblooms_for_user(profile_username) + all_blooms = own_blooms + shared_blooms + + sorted_blooms = list(sorted(all_blooms, key=lambda bloom: bloom.activity_timestamp, reverse=True)) + return jsonify( { "username": profile_username, - "recent_blooms": all_blooms[:10], + "recent_blooms": sorted_blooms[:10], "follows": get_followed_usernames(profile_user), "followers": list(followers), "is_following": current_user is not None @@ -177,6 +181,21 @@ def get_bloom(id_str): return make_response((f"Bloom not found", 404)) return jsonify(bloom) +@jwt_required() +def rebloom(bloom_id): + try: + id_int = int(bloom_id) + except ValueError: + return make_response(jsonify({"error": "Invalid bloom id"}), 400) + + current_user = get_current_user() + + success = blooms.add_rebloom(user_id=current_user.id, bloom_id=id_int) + + if success: + return make_response(jsonify({"success": True, "message": "Rebloom successfully"}), 200) + else: + return make_response(jsonify({"success": False, "message": "Failed to rebloom"}), 500) @jwt_required() def home_timeline(): @@ -195,22 +214,29 @@ def home_timeline(): # Get the current user's own blooms own_blooms = blooms.get_blooms_for_user(current_user.username, limit=50) + # Get the current user's shared blooms + shared_blooms = blooms.get_reblooms_for_user(current_user.username, limit=50) + # Combine own blooms with followed blooms - all_blooms = followed_blooms + own_blooms + all_blooms = followed_blooms + own_blooms + shared_blooms # Sort by timestamp (newest first) sorted_blooms = list( - sorted(all_blooms, key=lambda bloom: bloom.sent_timestamp, reverse=True) + sorted(all_blooms, key=lambda bloom: bloom.activity_timestamp, reverse=True) ) return jsonify(sorted_blooms) +def user_timeline(profile_username): + own_blooms = blooms.get_blooms_for_user(profile_username) + + shared_blooms = blooms.get_reblooms_for_user(profile_username) + + combined_timeline = own_blooms + shared_blooms -def user_blooms(profile_username): - user_blooms = blooms.get_blooms_for_user(profile_username) - user_blooms.reverse() - return jsonify(user_blooms) + sorted_timeline = list(sorted(combined_timeline, key=lambda bloom: bloom.activity_timestamp, reverse=True)) + return jsonify(sorted_timeline) @jwt_required() def suggested_follows(limit_str): diff --git a/backend/main.py b/backend/main.py index 7ba155fa..bc4c80ad 100644 --- a/backend/main.py +++ b/backend/main.py @@ -5,6 +5,7 @@ from endpoints import ( do_follow, get_bloom, + rebloom, hashtag, home_timeline, login, @@ -13,7 +14,7 @@ self_profile, send_bloom, suggested_follows, - user_blooms, + user_timeline, ) from dotenv import load_dotenv @@ -58,7 +59,8 @@ def main(): app.add_url_rule("/bloom", methods=["POST"], view_func=send_bloom) app.add_url_rule("/bloom/", methods=["GET"], view_func=get_bloom) - app.add_url_rule("/blooms/", view_func=user_blooms) + app.add_url_rule("/blooms//rebloom", methods=["POST"], view_func=rebloom) + app.add_url_rule("/blooms/", view_func=user_timeline) app.add_url_rule("/hashtag/", view_func=hashtag) app.run(host="0.0.0.0", port="3000", debug=True) diff --git a/db/schema.sql b/db/schema.sql index 61e7580c..c6029a65 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -26,3 +26,11 @@ CREATE TABLE hashtags ( bloom_id BIGINT NOT NULL REFERENCES blooms(id), UNIQUE(hashtag, bloom_id) ); + +CREATE TABLE reblooms ( + id SERIAL PRIMARY KEY, + user_id INT NOT NULL REFERENCES users(id) ON DELETE CASCADE, + bloom_id BIGINT NOT NULL REFERENCES blooms(id) ON DELETE CASCADE, + rebloom_timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE(user_id, bloom_id) +); diff --git a/front-end/components/bloom.mjs b/front-end/components/bloom.mjs index 0b4166c3..b970f44e 100644 --- a/front-end/components/bloom.mjs +++ b/front-end/components/bloom.mjs @@ -1,3 +1,6 @@ +import { apiService } from "../index.mjs"; +import { state } from "../lib/state.mjs"; + /** * Create a bloom component * @param {string} template - The ID of the template to clone @@ -20,8 +23,36 @@ const createBloom = (template, bloom) => { const bloomTime = bloomFrag.querySelector("[data-time]"); const bloomTimeLink = bloomFrag.querySelector("a:has(> [data-time])"); const bloomContent = bloomFrag.querySelector("[data-content]"); + const rebloomBtn = bloomFrag.querySelector("[data-action='rebloom']"); + const rebloomHeader = bloomFrag.querySelector("[data-rebloom-header]"); + const rebloomBy = bloomFrag.querySelector("[data-rebloom-by]"); + const rebloomCountLabel = bloomFrag.querySelector("[data-rebloom-count]"); bloomArticle.setAttribute("data-bloom-id", bloom.id); + + bloomArticle.removeAttribute("data-is-rebloom"); + if (rebloomHeader) rebloomHeader.classList.add("is-hidden"); + + if (bloom.rebloomer) { + bloomArticle.setAttribute("data-is-rebloom", "true"); + + if (rebloomHeader && rebloomBy) { + rebloomHeader.classList.remove("is-hidden"); + rebloomBy.setAttribute("href", `/profile/${bloom.rebloomer}`); + rebloomBy.textContent = bloom.rebloomer; + + } + } + + if (rebloomCountLabel) { + if (bloom.rebloom_count > 0) { + rebloomCountLabel.textContent = `(${bloom.rebloom_count})`; + rebloomCountLabel.classList.remove("is-hidden"); + } else { + rebloomCountLabel.classList.add("is-hidden"); + } + } + bloomUsername.setAttribute("href", `/profile/${bloom.sender}`); bloomUsername.textContent = bloom.sender; bloomTime.textContent = _formatTimestamp(bloom.sent_timestamp); @@ -31,6 +62,17 @@ const createBloom = (template, bloom) => { .body.childNodes ); + if (rebloomBtn) { + rebloomBtn.addEventListener("click", async (e) => { + e.preventDefault(); + rebloomBtn.disabled = true; + console.log(bloom) + const response = await apiService.postRebloom(bloom.id); + console.log("API service responded with", response); + rebloomBtn.disabled = false; + }) + } + return bloomFrag; }; diff --git a/front-end/index.css b/front-end/index.css index 65c7fb4c..8b604b59 100644 --- a/front-end/index.css +++ b/front-end/index.css @@ -194,6 +194,34 @@ dialog { grid-area: char-count; } +/* REBLOOM */ + +.bloom.box[data-is-rebloom="true"] { + border-left: 4px solid #7149c6 !important; + background-color: #f9f6ff; + box-shadow: inset 0 0 8px rgba(113, 73, 198, 0.02); +} + +.bloom__rebloom-header { + font-size: 0.85rem; + font-weight: 500; + color: #657786; + margin-bottom: 8px; + display: flex; + align-items: center; + gap: 4px; +} + +.bloom__rebloom-author { + font-weight: 700; + color: #7149c6; + text-decoration: none; +} + +.bloom__rebloom-author:hover { + text-decoration: underline; +} + /* LOGIN */ .login__container { margin: 1em 0 auto auto; @@ -264,6 +292,6 @@ dialog { } /* We always want attribute hidden to sync with display:none, no matter what */ -[hidden] { +.is-hidden, [hidden] { display: none !important; } diff --git a/front-end/index.html b/front-end/index.html index 89d6b130..ee3dff3e 100644 --- a/front-end/index.html +++ b/front-end/index.html @@ -234,11 +234,19 @@

Share a Bloom

diff --git a/front-end/lib/api.mjs b/front-end/lib/api.mjs index f4b5339b..0ae8fb9f 100644 --- a/front-end/lib/api.mjs +++ b/front-end/lib/api.mjs @@ -212,6 +212,28 @@ async function postBloom(content) { } } +async function postRebloom(bloomId) { + try { + const data = await _apiRequest(`/blooms/${bloomId}/rebloom`, { + method: "POST" + }); + console.log("Network response received:", data) + if (data.success) { + const reFetchingForHome = await getBlooms(); + console.log("Refetching for home:", reFetchingForHome) + if (state.currentUser) { + const updateUserProfile = await getProfile(state.currentUser); + const updateUserBlooms = await getBlooms(state.currentUser); + console.log("update user profile", updateUserProfile); + console.log("update user blooms", updateUserBlooms); + } + } + return data; + } catch (error) { + return {success: false} + } +} + // ======= USER methods async function getProfile(username) { const endpoint = username ? `/profile/${username}` : "/profile"; @@ -292,6 +314,7 @@ const apiService = { getBlooms, postBloom, getBloomsByHashtag, + postRebloom, // User methods getProfile,