⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.DS_Store
.venv
node_modules
19 changes: 15 additions & 4 deletions backend/data/blooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,38 @@ class Bloom:


def add_bloom(*, sender: User, content: str) -> Bloom:
# Enforce 280-character limit
content = content.strip()
if len(content) > 280:
content = content[:280] # truncate to 280 characters
Comment on lines +21 to +22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automatically truncating works (and you have endpoint-layer checks to make sure we shouldn't get here), but it's not clear whether at the DB layer we should truncate or reject - what do you think the trade-offs are here?


hashtags = [word[1:] for word in content.split(" ") if word.startswith("#")]

now = datetime.datetime.now(tz=datetime.UTC)
bloom_id = int(now.timestamp() * 1000000)

with db_cursor() as cur:
cur.execute(
"INSERT INTO blooms (id, sender_id, content, send_timestamp) VALUES (%(bloom_id)s, %(sender_id)s, %(content)s, %(timestamp)s)",
"""
INSERT INTO blooms (id, sender_id, content, send_timestamp)
VALUES (%(bloom_id)s, %(sender_id)s, %(content)s, %(timestamp)s)
""",
dict(
bloom_id=bloom_id,
sender_id=sender.id,
content=content,
timestamp=datetime.datetime.now(datetime.UTC),
timestamp=now,
),
)
for hashtag in hashtags:
cur.execute(
"INSERT INTO hashtags (hashtag, bloom_id) VALUES (%(hashtag)s, %(bloom_id)s)",
"""
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]:
Expand Down
40 changes: 28 additions & 12 deletions backend/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,35 @@ def do_follow():

@jwt_required()
def send_bloom():
type_check_error = verify_request_fields({"content": str})
if type_check_error is not None:
return type_check_error

"""Handles POST requests to create a new bloom (post)."""

# 1. Input Validation: Check for required fields and types
validation_error = verify_request_fields({"content": str})
if validation_error:
return validation_error

# 2. Data Extraction and Cleaning
content = request.json.get("content", "").strip()

# 3. Application-Specific Validation (Length and Empty Check)
if not content:
return jsonify({
"success": False,
"error": "Content cannot be empty."
}), 400

if len(content) > 280:
return jsonify({
"success": False,
"error": f"Content too long. Maximum 280 characters (current: {len(content)})."
}), 400

# 4. Processing: Get user and save the resource
user = get_current_user()

blooms.add_bloom(sender=user, content=request.json["content"])

return jsonify(
{
"success": True,
}
)
blooms.add_bloom(sender=user, content=content)

# 5. Success Response
return jsonify({"success": True}), 201


def get_bloom(id_str):
Expand Down