diff --git a/app/blueprints/api/endpoints.py b/app/blueprints/api/endpoints.py index f55e4226..b7cfe761 100644 --- a/app/blueprints/api/endpoints.py +++ b/app/blueprints/api/endpoints.py @@ -22,7 +22,6 @@ from typing import List import flask_sqlalchemy from flask import request, jsonify, current_app, Response from flask_babel import gettext -from flask_login import current_user, login_required from sqlalchemy import and_, or_ from sqlalchemy.orm import joinedload from sqlalchemy.sql.expression import func @@ -35,7 +34,7 @@ from app.models import Tag, PackageState, PackageType, Package, db, PackageRelea PackageAlias, Language from app.querybuilder import QueryBuilder from app.utils import is_package_page, get_int_or_abort, url_set_query, abs_url, is_yes, get_request_date -from app.utils.minetest_hypertext import html_to_minetest, package_info_as_hypertext +from app.utils.minetest_hypertext import html_to_minetest, package_info_as_hypertext, package_reviews_as_hypertext from . import bp from .auth import is_api_authd from .support import error, api_create_vcs_release, api_create_zip_release, api_create_screenshot, \ @@ -153,6 +152,18 @@ def package_view_client(package: Package): return resp +@bp.route("/api/packages///for-client/reviews/") +@is_package_page +@cors_allowed +def package_view_client_reviews(package: Package): + formspec_version = get_int_or_abort(request.args["formspec_version"]) + data = package_reviews_as_hypertext(package, formspec_version) + + resp = jsonify(data) + resp.vary = "Accept-Language" + return resp + + @bp.route("/api/packages///hypertext/") @is_package_page @cors_allowed diff --git a/app/utils/minetest_hypertext.py b/app/utils/minetest_hypertext.py index e035bcee..a7a61d0b 100644 --- a/app/utils/minetest_hypertext.py +++ b/app/utils/minetest_hypertext.py @@ -21,7 +21,9 @@ from urllib.parse import urljoin from flask_babel import gettext -from app.models import Package, PackageType +from app.markdown import render_markdown +from app.models import Package, PackageType, PackageReview +from app.utils import abs_url_for def normalize_whitespace(x): @@ -306,3 +308,32 @@ def package_info_as_hypertext(package: Package, formspec_version: int = 7): "images": {}, "image_tooltips": {}, } + + +def package_reviews_as_hypertext(package: Package, formspec_version: int = 7): + link_counter = 0 + links = {} + body = "" + + def make_link(url: str, label: str): + nonlocal link_counter + link_counter += 1 + links[f"link_{link_counter}"] = url + return f"{escape_hypertext(label)}" + + for review in package.reviews: + review: PackageReview + html = render_markdown(review.thread.first_reply.comment) + content = html_to_minetest(html, package.get_url("packages.view", absolute=True), formspec_version)["body"].strip() + author = make_link(abs_url_for("users.profile", username=review.author.username), review.author.display_name) + rating = ["👎", "👎", "-", "👍", "👍"][review.rating - 1] + comments = make_link(abs_url_for("threads.view", id=review.thread.id), "Comments") + body += f"{author} {review.rating}\n{escape_hypertext(review.thread.title)}\n{content}\n{comments}\n\n" + + return { + "head": HEAD, + "body": body, + "links": links, + "images": {}, + "image_tooltips": {}, + }