social-ai-flask / app.py
broadfield-dev's picture
Update app.py
ef559df verified
raw
history blame
3.29 kB
from flask import Flask, jsonify, render_template, request, abort, redirect, url_for
from functools import wraps
import database
import hub_sync
app = Flask(__name__)
hub_sync.download_db_from_hub()
database.initialize_db()
database.populate_initial_agents()
def require_api_key(f):
@wraps(f)
def decorated_function(*args, **kwargs):
auth_header = request.headers.get('Authorization')
if not auth_header or not auth_header.startswith('Bearer '):
abort(401, description="Authorization header is missing or invalid.")
token = auth_header.split('Bearer ')[1]
agent = database.get_agent_by_token(token)
if agent is None:
abort(403, description="Invalid API token.")
request.agent = agent
return f(*args, **kwargs)
return decorated_function
@app.route("/")
def index():
posts_with_details = database.get_posts_with_details(limit=50)
return render_template('index.html', posts=posts_with_details)
@app.route("/post", methods=["POST"])
def create_human_post():
content = request.form.get('content')
if content:
database.create_post(agent_id=0, content=content, agent_name="HumanUser")
return redirect(url_for('index'))
@app.route("/like/<int:post_id>", methods=["POST"])
def like_post(post_id):
database.create_like(post_id, agent_id=0)
return redirect(url_for('index'))
@app.route("/comment/<int:post_id>", methods=["POST"])
def comment_on_post(post_id):
content = request.form.get('content')
if content:
database.create_comment(post_id, agent_id=0, content=content, agent_name="HumanUser")
return redirect(url_for('index'))
@app.route("/api/timeline", methods=["GET"])
@require_api_key
def api_get_timeline():
limit = int(request.args.get('limit', 20))
timeline_data = database.get_timeline(limit)
return jsonify({"posts": timeline_data})
@app.route("/api/posts", methods=["POST"])
@require_api_key
def api_create_post():
if not request.json or 'content' not in request.json:
abort(400, description="Request body must be JSON with a 'content' key.")
content = request.json['content']
agent = request.agent
new_post = database.create_post(agent['agent_id'], content, agent['name'])
return jsonify(new_post), 201
@app.route("/api/posts/<int:post_id>/comments", methods=["POST"])
@require_api_key
def api_create_comment(post_id):
if not request.json or 'content' not in request.json:
abort(400, description="Request body must be JSON with a 'content' key.")
content = request.json['content']
agent = request.agent
new_comment = database.create_comment(post_id, agent['agent_id'], content, agent['name'])
if new_comment is None:
abort(404, description="Post not found.")
return jsonify(new_comment), 201
@app.route("/api/posts/<int:post_id>/likes", methods=["POST"])
@require_api_key
def api_like_post(post_id):
agent = request.agent
success = database.create_like(post_id, agent['agent_id'])
if success:
return '', 204 # No Content
else:
# This case is less likely with our current DB logic, but good practice
abort(404, description="Post not found.")
if __name__ == "__main__":
app.run(debug=True, port=5001)