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
4 changes: 2 additions & 2 deletions backend/data/blooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Bloom:
def add_bloom(*, sender: User, content: str) -> Bloom:
hashtags = [word[1:] for word in content.split(" ") if word.startswith("#")]

now = datetime.datetime.now(tz=datetime.UTC)
now = datetime.datetime.now(tz=datetime.timezone.utc)
bloom_id = int(now.timestamp() * 1000000)
with db_cursor() as cur:
cur.execute(
Expand All @@ -27,7 +27,7 @@ def add_bloom(*, sender: User, content: str) -> Bloom:
bloom_id=bloom_id,
sender_id=sender.id,
content=content,
timestamp=datetime.datetime.now(datetime.UTC),
timestamp=datetime.datetime.now(datetime.timezone.utc),
),
)
for hashtag in hashtags:
Expand Down
16 changes: 14 additions & 2 deletions backend/data/users.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from dataclasses import dataclass
from hashlib import scrypt
import hashlib
import random
import string
from typing import List, Optional

try:
from hashlib import scrypt as _scrypt_impl
USE_HASHLIB_SCRYPT = True
except ImportError:
# Python 3.9 doesn't have scrypt in hashlib, use the scrypt package
import scrypt as _scrypt_module
USE_HASHLIB_SCRYPT = False

from data.connection import db_cursor
from psycopg2.errors import UniqueViolation

Expand Down Expand Up @@ -91,7 +98,12 @@ def register_user(username: str, password_plaintext: str) -> User:


def scrypt(password_plaintext: bytes, password_salt: bytes) -> bytes:
return hashlib.scrypt(password_plaintext, salt=password_salt, n=8, r=8, p=1)
if USE_HASHLIB_SCRYPT:
# Python 3.11+ hashlib.scrypt
return _scrypt_impl(password_plaintext, salt=password_salt, n=8, r=8, p=1)
else:
# Python 3.9 scrypt package
return _scrypt_module.hash(password_plaintext, password_salt, N=8, r=8, p=1)


SALT_CHARACTERS = string.ascii_uppercase + string.ascii_lowercase + string.digits
Expand Down
14 changes: 13 additions & 1 deletion backend/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from datetime import timedelta

MINIMUM_PASSWORD_LENGTH = 5
MAXIMUM_BLOOM_LENGTH = 280


def login():
Expand Down Expand Up @@ -156,9 +157,20 @@ def send_bloom():
if type_check_error is not None:
return type_check_error

content = request.json["content"]

if len(content) > MAXIMUM_BLOOM_LENGTH:
return make_response(
jsonify({
"success": False,
"message": f"Bloom content exceeds maximum length of {MAXIMUM_BLOOM_LENGTH} characters"
}),
400
)

user = get_current_user()

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

return jsonify(
{
Expand Down
2 changes: 1 addition & 1 deletion backend/populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def main():
writer_access_token = create_user("AS", "neverSt0pTalking")
send_bloom(
writer_access_token,
"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.",
"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 toast them over a fire.",
)

justsomeguy_access_token = create_user("JustSomeGuy", "mysterious")
Expand Down