Skip to content

Commit 1275097

Browse files
enforce 280-character limit for blooms in backend, DB, and frontend
1 parent c0add7d commit 1275097

File tree

5 files changed

+20
-5
lines changed

5 files changed

+20
-5
lines changed

backend/data/blooms.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ class Bloom:
1616

1717

1818
def add_bloom(*, sender: User, content: str) -> Bloom:
19-
hashtags = [word[1:] for word in content.split(" ") if word.startswith("#")]
19+
if len(content) > 280:
20+
raise ValueError("Bloom content exceeds the 280 character limit.")
2021

22+
hashtags = [word[1:] for word in content.split(" ") if word.startswith("#")]
2123
now = datetime.datetime.now(tz=datetime.UTC)
2224
bloom_id = int(now.timestamp() * 1000000)
25+
2326
with db_cursor() as cur:
2427
cur.execute(
2528
"INSERT INTO blooms (id, sender_id, content, send_timestamp) VALUES (%(bloom_id)s, %(sender_id)s, %(content)s, %(timestamp)s)",

backend/populate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ def main():
6464
writer_access_token = create_user("AS", "neverSt0pTalking")
6565
send_bloom(
6666
writer_access_token,
67-
"In this essay I will convince you that my views are correct in ways you have never imagined. If it doesn't change your life, read it again. Marshmallows are magnificent. They have great squish, tasty good, and you can even toast them over a fire. Toast them just right until they have a tiny bit of crunch when you bite into them, and have just started melting in the middle.",
68-
)
67+
"In this essay I will convince you my views are correct in ways you never imagined. If it doesn't change your life, read it again. Marshmallows are magnificentgreat squish, tasty, and perfect toasted with a crisp outside and melty middle."
68+
)
6969

7070
justsomeguy_access_token = create_user("JustSomeGuy", "mysterious")
7171
send_bloom(justsomeguy_access_token, "Hello.")

db/schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ CREATE TABLE users (
99
CREATE TABLE blooms (
1010
id BIGSERIAL NOT NULL PRIMARY KEY,
1111
sender_id INT NOT NULL REFERENCES users(id),
12-
content TEXT NOT NULL,
12+
content TEXT NOT NULL CHECK (char_length(content) <= 280),
1313
send_timestamp TIMESTAMP NOT NULL
1414
);
1515

front-end/components/bloom-form.mjs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,18 @@ function handleTyping(event) {
5252
.closest("[data-form]")
5353
?.querySelector("[data-counter]");
5454
const maxLength = parseInt(textarea.getAttribute("maxlength"), 10);
55-
counter.textContent = `${textarea.value.length} / ${maxLength}`;
55+
const currentLength = textarea.value.length;
56+
57+
counter.textContent = `${currentLength} / ${maxLength}`;
58+
59+
if (currentLength >= maxLength) {
60+
counter.style.color = "red";
61+
counter.setAttribute("aria-live", "polite");
62+
counter.textContent += " — character limit reached!";
63+
} else {
64+
counter.style.color = "";
65+
}
5666
}
5767

68+
5869
export {createBloomForm, handleBloomSubmit, handleTyping};

front-end/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ <h2 id="bloom-form-title" class="bloom-form__title">Share a Bloom</h2>
219219
placeholder="What's happening?"
220220
maxlength="280"
221221
spellcheck="true"
222+
maxlength="280"
222223
required
223224
></textarea>
224225
<div class="bloom-form__counter" data-counter>0/280</div>

0 commit comments

Comments
 (0)