"""Page route registration for the legacy Jinja UI.""" import json import logging from flask import current_app, redirect, render_template, url_for from flask_login import current_user, login_required from services import get_display_name_for_source, load_platform_config, post_service logger = logging.getLogger(__name__) def _load_user_settings(): if not current_user.is_authenticated: return {} try: return json.loads(current_user.settings) if current_user.settings else {} except (json.JSONDecodeError, TypeError): return {} def register_page_routes(app): """Register legacy page routes while preserving endpoint names.""" @app.route("/") def index(): """Serve the main feed page.""" quick_stats = post_service.quick_stats() if current_user.is_authenticated: return render_template( "dashboard.html", user_settings=_load_user_settings(), quick_stats=quick_stats, ) if current_app.config.get("ALLOW_ANONYMOUS_ACCESS", False): user_settings = { "filter_set": "no_filter", "communities": [], "experience": { "infinite_scroll": False, "auto_refresh": False, "push_notifications": False, "dark_patterns_opt_in": False, "time_filter_enabled": False, "time_filter_days": 7, }, } return render_template( "dashboard.html", user_settings=user_settings, anonymous=True, quick_stats=quick_stats, ) return redirect(url_for("login")) @app.route("/bookmarks") @login_required def bookmarks(): """Bookmarks page.""" return render_template("bookmarks.html", user=current_user) @app.route("/post/") def post_detail(post_id): """Serve individual post detail page with modern theme.""" try: platform_config = load_platform_config() cached_posts, cached_comments = post_service.load() post_data = cached_posts.get(post_id) if not post_data: return render_template("404.html"), 404 post = dict(post_data) post["source_display"] = get_display_name_for_source( post.get("platform", ""), post.get("source", ""), platform_config, ) comments_flat = cached_comments.get(post_id, []) logger.info(f"Loading post {post_id}: found {len(comments_flat)} comments") comments = post_service.build_comment_tree(comments_flat) return render_template( "post_detail.html", post=post, comments=comments, user_settings=_load_user_settings(), ) except Exception as e: logger.error(f"Error loading post {post_id}: {e}") return render_template("404.html"), 404