Add endpoint for getting hypertext of reviews

This commit is contained in:
rubenwardy 2024-05-02 20:32:49 +01:00
parent faddf11f77
commit 4ac0016c0b
2 changed files with 45 additions and 3 deletions

@ -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/<author>/<name>/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/<author>/<name>/hypertext/")
@is_package_page
@cors_allowed

@ -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"<action name=link_{link_counter}>{escape_hypertext(label)}</action>"
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<big>{escape_hypertext(review.thread.title)}</big>\n{content}\n{comments}\n\n"
return {
"head": HEAD,
"body": body,
"links": links,
"images": {},
"image_tooltips": {},
}