From 25f968ab625d4d773e533acc324d6d14201e9a3b Mon Sep 17 00:00:00 2001 From: Gabriel Date: Wed, 5 Nov 2025 14:48:44 +0000 Subject: [PATCH] Added the functionality to filter out and validate extra long blooms --- backend/data/blooms.py | 101 +++++++++++++++++++++------- backend/endpoints.py | 14 ++-- front-end/components/bloom-form.mjs | 8 +++ 3 files changed, 93 insertions(+), 30 deletions(-) diff --git a/backend/data/blooms.py b/backend/data/blooms.py index 7e280cf..d0de026 100644 --- a/backend/data/blooms.py +++ b/backend/data/blooms.py @@ -16,6 +16,12 @@ class Bloom: def add_bloom(*, sender: User, content: str) -> Bloom: + bloom_length = len(content) + max_bloom_length = 280 + + if bloom_length > max_bloom_length: + raise ValueError(f"Bloom length {bloom_length} exceeds the maximum limit of {max_bloom_length}") + hashtags = [word[1:] for word in content.split(" ") if word.startswith("#")] now = datetime.datetime.now(tz=datetime.UTC) @@ -35,41 +41,86 @@ def add_bloom(*, sender: User, content: str) -> Bloom: "INSERT INTO hashtags (hashtag, bloom_id) VALUES (%(hashtag)s, %(bloom_id)s)", dict(hashtag=hashtag, bloom_id=bloom_id), ) - - -def get_blooms_for_user( - username: str, *, before: Optional[int] = None, limit: Optional[int] = None -) -> List[Bloom]: + return Bloom( + id=bloom_id, + sender=sender, + content=content, + sent_timestamp=now + ) + + +# def get_blooms_for_user( +# username: str, *, before: Optional[int] = None, limit: Optional[int] = None +# ) -> List[Bloom]: +# with db_cursor() as cur: +# kwargs = { +# "sender_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, content, send_timestamp +# FROM +# blooms INNER JOIN users ON users.id = blooms.sender_id +# WHERE +# username = %(sender_username)s +# {before_clause} +# ORDER BY send_timestamp DESC +# {limit_clause} +# """, +# kwargs, +# ) +# rows = cur.fetchall() +# blooms = [] +# for row in rows: +# bloom_id, sender_username, content, timestamp = row +# blooms.append( +# Bloom( +# id=bloom_id, +# sender=sender_username, +# content=content, +# sent_timestamp=timestamp, +# ) +# ) +# return blooms + +def get_blooms_for_user(username: str, *, before: Optional[int] = None, limit: Optional[int] = None) -> List[Bloom]: with db_cursor() as cur: - kwargs = { - "sender_username": username, - } - if before is not None: - before_clause = "AND send_timestamp < %(before_limit)s" - kwargs["before_limit"] = before - else: - before_clause = "" - + kwargs = {"sender_username": username} + before_clause = "AND send_timestamp < %(before_limit)s" if before else "" + if before: kwargs["before_limit"] = before limit_clause = make_limit_clause(limit, kwargs) cur.execute( - f"""SELECT - blooms.id, users.username, content, send_timestamp - FROM - blooms INNER JOIN users ON users.id = blooms.sender_id - WHERE - username = %(sender_username)s - {before_clause} + f""" + SELECT blooms.id, users.username, content, send_timestamp + FROM blooms + INNER JOIN users ON users.id = blooms.sender_id + WHERE username = %(sender_username)s + {before_clause} ORDER BY send_timestamp DESC {limit_clause} """, kwargs, ) + rows = cur.fetchall() - blooms = [] + blooms_list = [] for row in rows: bloom_id, sender_username, content, timestamp = row - blooms.append( + + # skip blooms longer than max length + if len(content) > 280: + continue + + blooms_list.append( Bloom( id=bloom_id, sender=sender_username, @@ -77,7 +128,9 @@ def get_blooms_for_user( sent_timestamp=timestamp, ) ) - return blooms + + return blooms_list + def get_bloom(bloom_id: int) -> Optional[Bloom]: diff --git a/backend/endpoints.py b/backend/endpoints.py index 0e177a0..e227735 100644 --- a/backend/endpoints.py +++ b/backend/endpoints.py @@ -158,13 +158,15 @@ def send_bloom(): user = get_current_user() - blooms.add_bloom(sender=user, content=request.json["content"]) + try: + blooms.add_bloom(sender=user, content=request.json["content"]) + except ValueError as error: + return make_response( + jsonify({"success": False, "message": str(error)}), + 400, + ) - return jsonify( - { - "success": True, - } - ) + return jsonify({"success": True}) def get_bloom(id_str): diff --git a/front-end/components/bloom-form.mjs b/front-end/components/bloom-form.mjs index e047f9a..828c937 100644 --- a/front-end/components/bloom-form.mjs +++ b/front-end/components/bloom-form.mjs @@ -27,6 +27,14 @@ async function handleBloomSubmit(event) { const textarea = form.querySelector("textarea"); const content = textarea.value.trim(); + const bloom_length = content.length + const max_bloom_length = 280 + + if (bloom_length > max_bloom_length) { + alert(`Bloom length ${bloom_length} exceeds the maximum limit of ${max_bloom_length}`); + return; + } + try { // Make form inert while we call the back end form.inert = true;