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: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"liveServer.settings.root": "front-end/",
"python.defaultInterpreterPath": "backend/.venv/bin/python"
"python.defaultInterpreterPath": "backend/.venv/bin/python",
"python-envs.defaultEnvManager": "ms-python.python:system",
"python-envs.pythonProjects": []
}
8 changes: 7 additions & 1 deletion backend/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,15 @@ def send_bloom():
if type_check_error is not None:
return type_check_error

content = request.json["content"]
if len(content) > 280:
return make_response(
({"success": False, "message": "Bloom cannot exceed 280 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
41 changes: 27 additions & 14 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)

from dotenv import load_dotenv
from flask import Flask
from flask import Flask, jsonify
from flask_cors import CORS
from flask_jwt_extended import JWTManager

Expand All @@ -32,34 +32,47 @@ def main():
# Configure CORS to handle preflight requests
CORS(
app,
resources={r"/*": {"origins": "*"}},
supports_credentials=True,
resources={
r"/*": {
"origins": "*",
"allow_headers": ["Content-Type", "Authorization"],
"methods": ["GET", "POST", "OPTIONS"],
}
},
allow_headers=["Content-Type", "Authorization"],
methods=["GET", "POST", "OPTIONS"]
)

app.config["JWT_SECRET_KEY"] = os.environ["JWT_SECRET_KEY"]
jwt = JWTManager(app)
jwt.user_lookup_loader(lookup_user)

# JWT error handlers
@app.errorhandler(422)
def handle_unprocessable_entity(e):
return jsonify({"success": False, "message": "Invalid or missing authentication token"}), 401

@jwt.expired_token_loader
def expired_token_callback(jwt_header, jwt_payload):
return jsonify({"success": False, "message": "Token has expired"}), 401

@jwt.invalid_token_loader
def invalid_token_callback(error):
return jsonify({"success": False, "message": "Invalid token"}), 401

@jwt.unauthorized_loader
def missing_authorization_callback(error):
return jsonify({"success": False, "message": "Missing authorization token"}), 401

app.add_url_rule("/register", methods=["POST"], view_func=register)
app.add_url_rule("/login", methods=["POST"], view_func=login)

app.add_url_rule("/home", view_func=home_timeline)
app.add_url_rule("/home", methods=["GET"], view_func=home_timeline)

app.add_url_rule("/profile", view_func=self_profile)
app.add_url_rule("/profile/<profile_username>", view_func=other_profile)
app.add_url_rule("/profile", methods=["GET"], view_func=self_profile)
app.add_url_rule("/profile/<profile_username>", methods=["GET"], view_func=other_profile)
app.add_url_rule("/follow", methods=["POST"], view_func=do_follow)
app.add_url_rule("/suggested-follows/<limit_str>", view_func=suggested_follows)
app.add_url_rule("/suggested-follows/<limit_str>", methods=["GET"], view_func=suggested_follows)

app.add_url_rule("/bloom", methods=["POST"], view_func=send_bloom)
app.add_url_rule("/bloom/<id_str>", methods=["GET"], view_func=get_bloom)
app.add_url_rule("/blooms/<profile_username>", view_func=user_blooms)
app.add_url_rule("/hashtag/<hashtag>", view_func=hashtag)
app.add_url_rule("/blooms/<profile_username>", methods=["GET"], view_func=user_blooms)
app.add_url_rule("/hashtag/<hashtag>", methods=["GET"], view_func=hashtag)

app.run(host="0.0.0.0", port="3000", debug=True)

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.",
"Marshmallows are magnificent! They have great squish and taste amazing. Toast them until they're golden and slightly melted. Simply delicious!",
)

justsomeguy_access_token = create_user("JustSomeGuy", "mysterious")
Expand Down
1 change: 1 addition & 0 deletions db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CREATE TABLE blooms (
sender_id INT NOT NULL REFERENCES users(id),
content TEXT NOT NULL,
send_timestamp TIMESTAMP NOT NULL
,CHECK (char_length(content) <= 280)
);

CREATE TABLE follows (
Expand Down
16 changes: 14 additions & 2 deletions front-end/lib/api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ async function _apiRequest(endpoint, options = {}) {
if (!endpoint.includes("/login") && !endpoint.includes("/register")) {
state.destroyState();
}
if (!endpoint.includes("/profile")) {
handleErrorDialog(error);
}
} else {
handleErrorDialog(error);
}

// Pass all errors forward to a dialog on the screen
handleErrorDialog(error);
throw error;
}

Expand Down Expand Up @@ -195,6 +198,15 @@ async function getBloomsByHashtag(hashtag) {

async function postBloom(content) {
try {
if (content.length === 0) {
handleErrorDialog(new Error("Bloom cannot be empty"));
return {success: false};
}
if (content.length > 280) {
handleErrorDialog(new Error("Bloom cannot exceed 280 characters"));
return {success: false};
}

const data = await _apiRequest("/bloom", {
method: "POST",
body: JSON.stringify({content}),
Expand Down