Skip to content

Commit

Permalink
Add endpoint for getting hypertext of reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenwardy committed May 2, 2024
1 parent faddf11 commit 4ac0016
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
15 changes: 13 additions & 2 deletions app/blueprints/api/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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
Expand All @@ -35,7 +34,7 @@
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, \
Expand Down Expand Up @@ -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
Expand Down
33 changes: 32 additions & 1 deletion app/utils/minetest_hypertext.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

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):
Expand Down Expand Up @@ -306,3 +308,32 @@ def make_game_link(game):
"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": {},
}

0 comments on commit 4ac0016

Please sign in to comment.