2020-07-12 17:34:25 +02:00
|
|
|
# ContentDB
|
2021-01-30 17:59:42 +01:00
|
|
|
# Copyright (C) 2018-21 rubenwardy
|
2019-11-22 15:33:22 +01:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2021-01-30 17:59:42 +01:00
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
2019-11-22 15:33:22 +01:00
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2021-01-30 17:59:42 +01:00
|
|
|
# GNU Affero General Public License for more details.
|
2019-11-22 15:33:22 +01:00
|
|
|
#
|
2021-01-30 17:59:42 +01:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2019-11-22 15:33:22 +01:00
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-01-30 04:35:02 +01:00
|
|
|
|
2021-11-26 15:56:01 +01:00
|
|
|
import math
|
2024-01-18 19:28:36 +01:00
|
|
|
import os
|
2021-11-25 11:35:31 +01:00
|
|
|
from typing import List
|
2019-11-22 15:33:22 +01:00
|
|
|
|
2021-11-26 15:33:17 +01:00
|
|
|
import flask_sqlalchemy
|
2024-07-03 18:58:42 +02:00
|
|
|
from flask import request, jsonify, current_app
|
2024-02-25 19:43:27 +01:00
|
|
|
from flask_babel import gettext
|
2023-09-12 22:15:55 +02:00
|
|
|
from sqlalchemy import and_, or_
|
2022-01-30 04:35:02 +01:00
|
|
|
from sqlalchemy.orm import joinedload
|
2021-02-02 18:09:23 +01:00
|
|
|
from sqlalchemy.sql.expression import func
|
|
|
|
|
2020-01-23 00:10:02 +01:00
|
|
|
from app import csrf
|
2022-11-15 02:51:21 +01:00
|
|
|
from app.logic.graphs import get_package_stats, get_package_stats_for_user, get_all_package_stats
|
2021-08-21 06:40:20 +02:00
|
|
|
from app.markdown import render_markdown
|
2024-07-02 22:36:42 +02:00
|
|
|
from app.models import Tag, PackageState, PackageType, Package, db, PackageRelease, Permission, \
|
2023-09-14 21:41:48 +02:00
|
|
|
MinetestRelease, APIToken, PackageScreenshot, License, ContentWarning, User, PackageReview, Thread, Collection, \
|
2024-02-25 19:43:27 +01:00
|
|
|
PackageAlias, Language
|
2019-11-22 15:33:22 +01:00
|
|
|
from app.querybuilder import QueryBuilder
|
2024-07-03 18:58:42 +02:00
|
|
|
from app.utils import is_package_page, get_int_or_abort, url_set_query, abs_url, is_yes, get_request_date, cached, \
|
|
|
|
cors_allowed
|
2024-05-02 21:32:49 +02:00
|
|
|
from app.utils.minetest_hypertext import html_to_minetest, package_info_as_hypertext, package_reviews_as_hypertext
|
2021-02-02 18:09:23 +01:00
|
|
|
from . import bp
|
|
|
|
from .auth import is_api_authd
|
2022-02-02 02:08:01 +01:00
|
|
|
from .support import error, api_create_vcs_release, api_create_zip_release, api_create_screenshot, \
|
|
|
|
api_order_screenshots, api_edit_package, api_set_cover_image
|
2021-11-24 16:39:50 +01:00
|
|
|
|
|
|
|
|
2019-11-22 15:33:22 +01:00
|
|
|
@bp.route("/api/packages/")
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2022-11-18 22:36:15 +01:00
|
|
|
@cached(300)
|
2019-11-22 15:33:22 +01:00
|
|
|
def packages():
|
2024-02-25 19:43:27 +01:00
|
|
|
allowed_languages = set([x[0] for x in db.session.query(Language.id).all()])
|
|
|
|
lang = request.accept_languages.best_match(allowed_languages)
|
|
|
|
|
|
|
|
qb = QueryBuilder(request.args, lang=lang)
|
2023-06-19 22:27:49 +02:00
|
|
|
query = qb.build_package_query()
|
2019-11-22 15:33:22 +01:00
|
|
|
|
2024-06-22 18:14:52 +02:00
|
|
|
fmt = request.args.get("fmt")
|
|
|
|
if fmt == "keys":
|
2023-06-19 20:32:36 +02:00
|
|
|
return jsonify([pkg.as_key_dict() for pkg in query.all()])
|
2021-01-16 01:34:09 +01:00
|
|
|
|
2024-06-22 18:14:52 +02:00
|
|
|
include_vcs = fmt == "vcs"
|
|
|
|
pkgs = qb.convert_to_dictionary(query.all(), include_vcs)
|
2021-01-16 01:34:09 +01:00
|
|
|
if "engine_version" in request.args or "protocol_version" in request.args:
|
2023-06-19 20:32:36 +02:00
|
|
|
pkgs = [pkg for pkg in pkgs if pkg.get("release")]
|
2023-05-31 18:29:20 +02:00
|
|
|
|
|
|
|
# Promote featured packages
|
2023-09-02 22:52:14 +02:00
|
|
|
if "sort" not in request.args and \
|
|
|
|
"order" not in request.args and \
|
|
|
|
"q" not in request.args and \
|
|
|
|
"limit" not in request.args:
|
2023-05-31 18:29:20 +02:00
|
|
|
featured_lut = set()
|
2023-08-20 23:25:18 +02:00
|
|
|
featured = qb.convert_to_dictionary(query.filter(
|
2024-06-22 18:14:52 +02:00
|
|
|
Package.collections.any(and_(Collection.name == "featured", Collection.author.has(username="ContentDB")))).all(),
|
|
|
|
include_vcs)
|
2023-05-31 18:29:20 +02:00
|
|
|
for pkg in featured:
|
|
|
|
featured_lut.add(f"{pkg['author']}/{pkg['name']}")
|
2024-02-25 19:43:27 +01:00
|
|
|
pkg["short_description"] = gettext("Featured") + ". " + pkg["short_description"]
|
2024-04-05 19:25:41 +02:00
|
|
|
pkg["featured"] = True
|
2023-05-31 18:29:20 +02:00
|
|
|
|
|
|
|
not_featured = [pkg for pkg in pkgs if f"{pkg['author']}/{pkg['name']}" not in featured_lut]
|
|
|
|
pkgs = featured + not_featured
|
|
|
|
|
2024-03-03 02:53:12 +01:00
|
|
|
resp = jsonify(pkgs)
|
|
|
|
resp.vary = "Accept-Language"
|
|
|
|
return resp
|
2019-11-22 15:33:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/api/packages/<author>/<name>/")
|
|
|
|
@is_package_page
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2023-06-19 20:32:36 +02:00
|
|
|
def package_view(package):
|
2024-02-25 19:43:27 +01:00
|
|
|
allowed_languages = set([x[0] for x in db.session.query(Language.id).all()])
|
|
|
|
lang = request.accept_languages.best_match(allowed_languages)
|
|
|
|
|
|
|
|
data = package.as_dict(current_app.config["BASE_URL"], lang=lang)
|
2024-04-01 18:32:12 +02:00
|
|
|
resp = jsonify(data)
|
|
|
|
resp.vary = "Accept-Language"
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/api/packages/<author>/<name>/for-client/")
|
|
|
|
@is_package_page
|
|
|
|
@cors_allowed
|
|
|
|
def package_view_client(package: Package):
|
|
|
|
protocol_version = request.args.get("protocol_version")
|
|
|
|
engine_version = request.args.get("engine_version")
|
|
|
|
if protocol_version or engine_version:
|
|
|
|
version = MinetestRelease.get(engine_version, get_int_or_abort(protocol_version))
|
|
|
|
else:
|
|
|
|
version = None
|
|
|
|
|
|
|
|
allowed_languages = set([x[0] for x in db.session.query(Language.id).all()])
|
|
|
|
lang = request.accept_languages.best_match(allowed_languages)
|
|
|
|
|
2024-04-01 18:43:16 +02:00
|
|
|
data = package.as_dict(current_app.config["BASE_URL"], version, lang=lang, screenshots_dict=True)
|
2024-04-01 18:32:12 +02:00
|
|
|
|
|
|
|
formspec_version = get_int_or_abort(request.args["formspec_version"])
|
|
|
|
include_images = is_yes(request.args.get("include_images", "true"))
|
|
|
|
html = render_markdown(data["long_description"])
|
2024-04-05 19:17:07 +02:00
|
|
|
page_url = package.get_url("packages.view", absolute=True)
|
|
|
|
data["long_description"] = html_to_minetest(html, page_url, formspec_version, include_images)
|
2024-04-01 18:32:12 +02:00
|
|
|
|
|
|
|
data["info_hypertext"] = package_info_as_hypertext(package, formspec_version)
|
|
|
|
|
|
|
|
data["download_size"] = package.get_download_release(version).file_size
|
|
|
|
|
|
|
|
data["reviews"] = {
|
|
|
|
"positive": package.reviews.filter(PackageReview.rating > 3).count(),
|
|
|
|
"neutral": package.reviews.filter(PackageReview.rating == 3).count(),
|
|
|
|
"negative": package.reviews.filter(PackageReview.rating < 3).count(),
|
|
|
|
}
|
2023-12-16 01:46:11 +01:00
|
|
|
|
2024-03-03 02:53:12 +01:00
|
|
|
resp = jsonify(data)
|
|
|
|
resp.vary = "Accept-Language"
|
|
|
|
return resp
|
2019-11-22 15:33:22 +01:00
|
|
|
|
|
|
|
|
2024-05-02 21:32:49 +02:00
|
|
|
@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
|
|
|
|
|
|
|
|
|
2023-04-19 19:27:28 +02:00
|
|
|
@bp.route("/api/packages/<author>/<name>/hypertext/")
|
|
|
|
@is_package_page
|
|
|
|
@cors_allowed
|
|
|
|
def package_hypertext(package):
|
2024-04-05 19:17:07 +02:00
|
|
|
formspec_version = get_int_or_abort(request.args["formspec_version"])
|
2023-06-19 22:27:49 +02:00
|
|
|
include_images = is_yes(request.args.get("include_images", "true"))
|
2023-04-19 19:27:28 +02:00
|
|
|
html = render_markdown(package.desc)
|
2024-04-05 19:17:07 +02:00
|
|
|
page_url = package.get_url("packages.view", absolute=True)
|
|
|
|
return jsonify(html_to_minetest(html, page_url, formspec_version, include_images))
|
2023-04-19 19:27:28 +02:00
|
|
|
|
|
|
|
|
2021-02-02 22:35:29 +01:00
|
|
|
@bp.route("/api/packages/<author>/<name>/", methods=["PUT"])
|
|
|
|
@csrf.exempt
|
|
|
|
@is_package_page
|
|
|
|
@is_api_authd
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2021-02-02 22:35:29 +01:00
|
|
|
def edit_package(token, package):
|
|
|
|
if not token:
|
|
|
|
error(401, "Authentication needed")
|
|
|
|
|
|
|
|
return api_edit_package(token, package, request.json)
|
|
|
|
|
|
|
|
|
2021-07-20 22:23:13 +02:00
|
|
|
def resolve_package_deps(out, package, only_hard, depth=1):
|
2023-06-19 20:32:36 +02:00
|
|
|
id_ = package.get_id()
|
|
|
|
if id_ in out:
|
2020-06-05 17:09:27 +02:00
|
|
|
return
|
2019-11-22 15:33:22 +01:00
|
|
|
|
2020-06-05 17:09:27 +02:00
|
|
|
ret = []
|
2023-06-19 20:32:36 +02:00
|
|
|
out[id_] = ret
|
2020-06-05 05:44:39 +02:00
|
|
|
|
2021-07-20 22:23:13 +02:00
|
|
|
if package.type != PackageType.MOD:
|
|
|
|
return
|
|
|
|
|
2019-11-22 15:33:22 +01:00
|
|
|
for dep in package.dependencies:
|
2020-06-05 05:46:46 +02:00
|
|
|
if only_hard and dep.optional:
|
2020-06-05 05:44:39 +02:00
|
|
|
continue
|
|
|
|
|
2019-11-22 15:33:22 +01:00
|
|
|
if dep.package:
|
|
|
|
name = dep.package.name
|
2023-06-18 22:56:19 +02:00
|
|
|
fulfilled_by = [ dep.package.get_id() ]
|
2021-07-20 22:23:13 +02:00
|
|
|
resolve_package_deps(out, dep.package, only_hard, depth)
|
2019-11-22 15:33:22 +01:00
|
|
|
|
|
|
|
elif dep.meta_package:
|
|
|
|
name = dep.meta_package.name
|
2023-06-18 22:56:19 +02:00
|
|
|
fulfilled_by = [ pkg.get_id() for pkg in dep.meta_package.packages if pkg.state == PackageState.APPROVED]
|
2021-07-20 22:23:13 +02:00
|
|
|
|
2022-01-08 23:29:02 +01:00
|
|
|
if depth == 1 and not dep.optional:
|
2022-06-01 18:02:49 +02:00
|
|
|
most_likely = next((pkg for pkg in dep.meta_package.packages \
|
|
|
|
if pkg.type == PackageType.MOD and pkg.state == PackageState.APPROVED), None)
|
2021-07-20 22:23:13 +02:00
|
|
|
if most_likely:
|
|
|
|
resolve_package_deps(out, most_likely, only_hard, depth + 1)
|
2019-11-22 15:33:22 +01:00
|
|
|
|
|
|
|
else:
|
2020-12-04 03:23:04 +01:00
|
|
|
raise Exception("Malformed dependency")
|
2019-11-22 15:33:22 +01:00
|
|
|
|
|
|
|
ret.append({
|
|
|
|
"name": name,
|
|
|
|
"is_optional": dep.optional,
|
|
|
|
"packages": fulfilled_by
|
|
|
|
})
|
|
|
|
|
2020-06-05 17:09:27 +02:00
|
|
|
|
|
|
|
@bp.route("/api/packages/<author>/<name>/dependencies/")
|
|
|
|
@is_package_page
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2024-01-01 18:35:11 +01:00
|
|
|
@cached(300)
|
2020-06-05 17:09:27 +02:00
|
|
|
def package_dependencies(package):
|
|
|
|
only_hard = request.args.get("only_hard")
|
|
|
|
|
|
|
|
out = {}
|
|
|
|
resolve_package_deps(out, package, only_hard)
|
|
|
|
|
|
|
|
return jsonify(out)
|
2019-11-22 15:33:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/api/topics/")
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2019-11-22 15:33:22 +01:00
|
|
|
def topics():
|
2023-06-19 20:32:36 +02:00
|
|
|
qb = QueryBuilder(request.args)
|
2023-06-19 22:27:49 +02:00
|
|
|
query = qb.build_topic_query(show_added=True)
|
2023-06-18 22:56:19 +02:00
|
|
|
return jsonify([t.as_dict() for t in query.all()])
|
2019-11-22 15:33:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/api/whoami/")
|
|
|
|
@is_api_authd
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2019-11-22 15:33:22 +01:00
|
|
|
def whoami(token):
|
|
|
|
if token is None:
|
|
|
|
return jsonify({ "is_authenticated": False, "username": None })
|
|
|
|
else:
|
|
|
|
return jsonify({ "is_authenticated": True, "username": token.owner.username })
|
2020-01-23 00:10:02 +01:00
|
|
|
|
|
|
|
|
2023-10-31 19:46:34 +01:00
|
|
|
@bp.route("/api/delete-token/", methods=["DELETE"])
|
|
|
|
@csrf.exempt
|
|
|
|
@is_api_authd
|
|
|
|
@cors_allowed
|
|
|
|
def api_delete_token(token):
|
|
|
|
if token is None:
|
|
|
|
error(404, "Token not found")
|
|
|
|
|
|
|
|
db.session.delete(token)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return jsonify({"success": True})
|
|
|
|
|
|
|
|
|
2020-01-23 00:10:02 +01:00
|
|
|
@bp.route("/api/markdown/", methods=["POST"])
|
|
|
|
@csrf.exempt
|
2020-01-24 21:21:40 +01:00
|
|
|
def markdown():
|
2020-01-23 00:10:02 +01:00
|
|
|
return render_markdown(request.data.decode("utf-8"))
|
2020-01-24 21:21:40 +01:00
|
|
|
|
|
|
|
|
2021-03-05 13:55:21 +01:00
|
|
|
@bp.route("/api/releases/")
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2021-03-05 13:55:21 +01:00
|
|
|
def list_all_releases():
|
|
|
|
query = PackageRelease.query.filter_by(approved=True) \
|
|
|
|
.filter(PackageRelease.package.has(state=PackageState.APPROVED)) \
|
2024-06-22 16:18:58 +02:00
|
|
|
.order_by(db.desc(PackageRelease.created_at))
|
2021-03-05 13:55:21 +01:00
|
|
|
|
|
|
|
if "author" in request.args:
|
|
|
|
author = User.query.filter_by(username=request.args["author"]).first()
|
|
|
|
if author is None:
|
2021-04-10 17:30:19 +02:00
|
|
|
error(404, "Author not found")
|
2021-03-05 13:55:21 +01:00
|
|
|
query = query.filter(PackageRelease.package.has(author=author))
|
|
|
|
|
|
|
|
if "maintainer" in request.args:
|
|
|
|
maintainer = User.query.filter_by(username=request.args["maintainer"]).first()
|
|
|
|
if maintainer is None:
|
2021-04-10 17:30:19 +02:00
|
|
|
error(404, "Maintainer not found")
|
2021-03-05 13:55:21 +01:00
|
|
|
query = query.join(Package)
|
2023-06-18 22:11:17 +02:00
|
|
|
query = query.filter(Package.maintainers.contains(maintainer))
|
2021-03-05 13:55:21 +01:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
return jsonify([ rel.as_long_dict() for rel in query.limit(30).all() ])
|
2021-03-05 13:55:21 +01:00
|
|
|
|
|
|
|
|
2021-02-02 18:09:25 +01:00
|
|
|
@bp.route("/api/packages/<author>/<name>/releases/")
|
|
|
|
@is_package_page
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2021-02-02 18:09:25 +01:00
|
|
|
def list_releases(package):
|
2023-06-18 22:56:19 +02:00
|
|
|
return jsonify([ rel.as_dict() for rel in package.releases.all() ])
|
2021-02-02 18:09:25 +01:00
|
|
|
|
|
|
|
|
2020-01-24 21:21:40 +01:00
|
|
|
@bp.route("/api/packages/<author>/<name>/releases/new/", methods=["POST"])
|
|
|
|
@csrf.exempt
|
|
|
|
@is_package_page
|
|
|
|
@is_api_authd
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2020-01-24 21:21:40 +01:00
|
|
|
def create_release(token, package):
|
2020-05-19 18:24:57 +02:00
|
|
|
if not token:
|
|
|
|
error(401, "Authentication needed")
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
if not package.check_perm(token.owner, Permission.APPROVE_RELEASE):
|
2020-05-19 18:24:57 +02:00
|
|
|
error(403, "You do not have the permission to approve releases")
|
2020-04-21 20:27:34 +02:00
|
|
|
|
2023-05-02 11:24:52 +02:00
|
|
|
if request.headers.get("Content-Type") == "application/json":
|
|
|
|
data = request.json
|
|
|
|
else:
|
|
|
|
data = request.form
|
|
|
|
|
2024-06-22 16:33:49 +02:00
|
|
|
if not ("title" in data or "name" in data):
|
|
|
|
error(400, "name is required in the POST data")
|
|
|
|
|
|
|
|
name = data.get("name")
|
|
|
|
title = data.get("title") or name
|
|
|
|
name = name or title
|
2020-01-24 21:21:40 +01:00
|
|
|
|
2021-02-02 18:09:28 +01:00
|
|
|
if data.get("method") == "git":
|
2021-02-02 01:07:41 +01:00
|
|
|
for option in ["method", "ref"]:
|
|
|
|
if option not in data:
|
|
|
|
error(400, option + " is required in the POST data")
|
2020-01-24 21:21:40 +01:00
|
|
|
|
2024-06-22 16:33:49 +02:00
|
|
|
return api_create_vcs_release(token, package, name, title, data.get("release_notes"), data["ref"])
|
2021-02-02 01:07:41 +01:00
|
|
|
|
|
|
|
elif request.files:
|
|
|
|
file = request.files.get("file")
|
|
|
|
if file is None:
|
|
|
|
error(400, "Missing 'file' in multipart body")
|
|
|
|
|
2021-02-27 19:31:56 +01:00
|
|
|
commit_hash = data.get("commit")
|
|
|
|
|
2024-06-22 16:33:49 +02:00
|
|
|
return api_create_zip_release(token, package, name, title, data.get("release_notes"), file, None, None, "API", commit_hash)
|
2021-02-02 18:09:23 +01:00
|
|
|
|
2021-02-02 18:09:28 +01:00
|
|
|
else:
|
|
|
|
error(400, "Unknown release-creation method. Specify the method or provide a file.")
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/api/packages/<author>/<name>/releases/<int:id>/")
|
|
|
|
@is_package_page
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2023-06-19 23:10:21 +02:00
|
|
|
def release_view(package: Package, id: int):
|
|
|
|
release = PackageRelease.query.get(id)
|
2021-02-02 18:09:28 +01:00
|
|
|
if release is None or release.package != package:
|
|
|
|
error(404, "Release not found")
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
return jsonify(release.as_dict())
|
2021-02-02 18:09:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/api/packages/<author>/<name>/releases/<int:id>/", methods=["DELETE"])
|
|
|
|
@csrf.exempt
|
|
|
|
@is_package_page
|
|
|
|
@is_api_authd
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2023-06-19 23:10:21 +02:00
|
|
|
def delete_release(token: APIToken, package: Package, id: int):
|
|
|
|
release = PackageRelease.query.get(id)
|
2021-02-02 18:09:28 +01:00
|
|
|
if release is None or release.package != package:
|
|
|
|
error(404, "Release not found")
|
|
|
|
|
|
|
|
if not token:
|
|
|
|
error(401, "Authentication needed")
|
|
|
|
|
2023-06-19 20:32:36 +02:00
|
|
|
if not token.can_operate_on_package(package):
|
2021-02-02 18:09:28 +01:00
|
|
|
error(403, "API token does not have access to the package")
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
if not release.check_perm(token.owner, Permission.DELETE_RELEASE):
|
2021-02-02 18:09:28 +01:00
|
|
|
error(403, "Unable to delete the release, make sure there's a newer release available")
|
|
|
|
|
|
|
|
db.session.delete(release)
|
|
|
|
db.session.commit()
|
|
|
|
|
2024-03-03 02:34:22 +01:00
|
|
|
if release.file_path and os.path.isfile(release.file_path):
|
|
|
|
os.remove(release.file_path)
|
2024-01-18 19:28:36 +01:00
|
|
|
|
2021-02-02 18:09:28 +01:00
|
|
|
return jsonify({"success": True})
|
|
|
|
|
2021-02-02 18:09:23 +01:00
|
|
|
|
2021-02-02 18:09:25 +01:00
|
|
|
@bp.route("/api/packages/<author>/<name>/screenshots/")
|
|
|
|
@is_package_page
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2021-02-02 18:09:25 +01:00
|
|
|
def list_screenshots(package):
|
|
|
|
screenshots = package.screenshots.all()
|
2023-06-18 22:56:19 +02:00
|
|
|
return jsonify([ss.as_dict(current_app.config["BASE_URL"]) for ss in screenshots])
|
2021-02-02 18:09:25 +01:00
|
|
|
|
|
|
|
|
2021-02-02 18:09:23 +01:00
|
|
|
@bp.route("/api/packages/<author>/<name>/screenshots/new/", methods=["POST"])
|
|
|
|
@csrf.exempt
|
|
|
|
@is_package_page
|
|
|
|
@is_api_authd
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2021-02-02 18:09:23 +01:00
|
|
|
def create_screenshot(token: APIToken, package: Package):
|
|
|
|
if not token:
|
|
|
|
error(401, "Authentication needed")
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
if not package.check_perm(token.owner, Permission.ADD_SCREENSHOTS):
|
2021-02-02 18:09:23 +01:00
|
|
|
error(403, "You do not have the permission to create screenshots")
|
|
|
|
|
|
|
|
data = request.form
|
|
|
|
if "title" not in data:
|
|
|
|
error(400, "Title is required in the POST data")
|
|
|
|
|
|
|
|
file = request.files.get("file")
|
|
|
|
if file is None:
|
|
|
|
error(400, "Missing 'file' in multipart body")
|
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
return api_create_screenshot(token, package, data["title"], file, is_yes(data.get("is_cover_image")))
|
2021-02-02 18:09:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/api/packages/<author>/<name>/screenshots/<int:id>/")
|
|
|
|
@is_package_page
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2023-06-19 23:10:21 +02:00
|
|
|
def screenshot(package, id):
|
|
|
|
ss = PackageScreenshot.query.get(id)
|
2021-02-02 18:09:25 +01:00
|
|
|
if ss is None or ss.package != package:
|
|
|
|
error(404, "Screenshot not found")
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
return jsonify(ss.as_dict(current_app.config["BASE_URL"]))
|
2021-02-02 18:09:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/api/packages/<author>/<name>/screenshots/<int:id>/", methods=["DELETE"])
|
|
|
|
@csrf.exempt
|
|
|
|
@is_package_page
|
|
|
|
@is_api_authd
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2023-06-19 23:10:21 +02:00
|
|
|
def delete_screenshot(token: APIToken, package: Package, id: int):
|
|
|
|
ss = PackageScreenshot.query.get(id)
|
2021-02-02 18:09:25 +01:00
|
|
|
if ss is None or ss.package != package:
|
|
|
|
error(404, "Screenshot not found")
|
|
|
|
|
|
|
|
if not token:
|
|
|
|
error(401, "Authentication needed")
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
if not package.check_perm(token.owner, Permission.ADD_SCREENSHOTS):
|
2021-02-02 18:09:25 +01:00
|
|
|
error(403, "You do not have the permission to delete screenshots")
|
|
|
|
|
2023-06-19 20:32:36 +02:00
|
|
|
if not token.can_operate_on_package(package):
|
2021-02-02 18:09:25 +01:00
|
|
|
error(403, "API token does not have access to the package")
|
|
|
|
|
|
|
|
if package.cover_image == ss:
|
|
|
|
package.cover_image = None
|
|
|
|
db.session.merge(package)
|
|
|
|
|
|
|
|
db.session.delete(ss)
|
|
|
|
db.session.commit()
|
|
|
|
|
2024-01-18 19:28:36 +01:00
|
|
|
os.remove(ss.file_path)
|
|
|
|
|
2021-02-02 18:09:25 +01:00
|
|
|
return jsonify({ "success": True })
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/api/packages/<author>/<name>/screenshots/order/", methods=["POST"])
|
|
|
|
@csrf.exempt
|
|
|
|
@is_package_page
|
|
|
|
@is_api_authd
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2021-02-02 18:09:25 +01:00
|
|
|
def order_screenshots(token: APIToken, package: Package):
|
|
|
|
if not token:
|
|
|
|
error(401, "Authentication needed")
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
if not package.check_perm(token.owner, Permission.ADD_SCREENSHOTS):
|
2022-02-02 02:08:01 +01:00
|
|
|
error(403, "You do not have the permission to change screenshots")
|
2021-02-02 18:09:25 +01:00
|
|
|
|
2023-06-19 20:32:36 +02:00
|
|
|
if not token.can_operate_on_package(package):
|
2021-02-02 18:09:25 +01:00
|
|
|
error(403, "API token does not have access to the package")
|
|
|
|
|
|
|
|
json = request.json
|
|
|
|
if json is None or not isinstance(json, list):
|
|
|
|
error(400, "Expected order body to be array")
|
|
|
|
|
|
|
|
return api_order_screenshots(token, package, request.json)
|
2021-02-02 23:41:48 +01:00
|
|
|
|
|
|
|
|
2022-02-02 02:08:01 +01:00
|
|
|
@bp.route("/api/packages/<author>/<name>/screenshots/cover-image/", methods=["POST"])
|
|
|
|
@csrf.exempt
|
|
|
|
@is_package_page
|
|
|
|
@is_api_authd
|
|
|
|
@cors_allowed
|
|
|
|
def set_cover_image(token: APIToken, package: Package):
|
|
|
|
if not token:
|
|
|
|
error(401, "Authentication needed")
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
if not package.check_perm(token.owner, Permission.ADD_SCREENSHOTS):
|
2022-02-02 02:08:01 +01:00
|
|
|
error(403, "You do not have the permission to change screenshots")
|
|
|
|
|
2023-06-19 20:32:36 +02:00
|
|
|
if not token.can_operate_on_package(package):
|
2022-02-02 02:08:01 +01:00
|
|
|
error(403, "API token does not have access to the package")
|
|
|
|
|
|
|
|
json = request.json
|
|
|
|
if json is None or not isinstance(json, dict) or "cover_image" not in json:
|
|
|
|
error(400, "Expected body to be an object with cover_image as a key")
|
|
|
|
|
|
|
|
return api_set_cover_image(token, package, request.json["cover_image"])
|
|
|
|
|
|
|
|
|
2021-11-24 17:33:37 +01:00
|
|
|
@bp.route("/api/packages/<author>/<name>/reviews/")
|
|
|
|
@is_package_page
|
|
|
|
@cors_allowed
|
|
|
|
def list_reviews(package):
|
|
|
|
reviews = package.reviews
|
2023-06-18 22:56:19 +02:00
|
|
|
return jsonify([review.as_dict() for review in reviews])
|
2021-11-24 17:33:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/api/reviews/")
|
|
|
|
@cors_allowed
|
|
|
|
def list_all_reviews():
|
2021-11-26 15:33:17 +01:00
|
|
|
page = get_int_or_abort(request.args.get("page"), 1)
|
2023-07-09 17:48:46 +02:00
|
|
|
num = min(get_int_or_abort(request.args.get("n"), 100), 200)
|
2021-11-26 15:33:17 +01:00
|
|
|
|
|
|
|
query = PackageReview.query
|
|
|
|
query = query.options(joinedload(PackageReview.author), joinedload(PackageReview.package))
|
|
|
|
|
2023-07-09 17:48:46 +02:00
|
|
|
if "for_user" in request.args:
|
|
|
|
query = query.filter(PackageReview.package.has(Package.author.has(username=request.args["for_user"])))
|
|
|
|
|
|
|
|
if "author" in request.args:
|
2021-12-20 22:07:12 +01:00
|
|
|
query = query.filter(PackageReview.author.has(User.username == request.args.get("author")))
|
2021-11-26 15:33:17 +01:00
|
|
|
|
2023-07-09 17:48:46 +02:00
|
|
|
if "is_positive" in request.args:
|
2023-06-19 22:27:49 +02:00
|
|
|
if is_yes(request.args.get("is_positive")):
|
2023-04-15 21:06:24 +02:00
|
|
|
query = query.filter(PackageReview.rating > 3)
|
2023-04-15 03:37:58 +02:00
|
|
|
else:
|
2023-04-15 21:06:24 +02:00
|
|
|
query = query.filter(PackageReview.rating <= 3)
|
2021-11-26 15:33:17 +01:00
|
|
|
|
|
|
|
q = request.args.get("q")
|
|
|
|
if q:
|
|
|
|
query = query.filter(PackageReview.thread.has(Thread.title.ilike(f"%{q}%")))
|
|
|
|
|
2023-07-09 17:48:46 +02:00
|
|
|
query = query.order_by(db.desc(PackageReview.created_at))
|
|
|
|
|
2023-04-23 22:49:53 +02:00
|
|
|
pagination: flask_sqlalchemy.Pagination = query.paginate(page=page, per_page=num)
|
2021-11-26 15:33:17 +01:00
|
|
|
return jsonify({
|
|
|
|
"page": pagination.page,
|
2021-11-26 15:56:01 +01:00
|
|
|
"per_page": pagination.per_page,
|
|
|
|
"page_count": math.ceil(pagination.total / pagination.per_page),
|
|
|
|
"total": pagination.total,
|
2021-11-26 15:33:17 +01:00
|
|
|
"urls": {
|
2021-11-26 15:56:01 +01:00
|
|
|
"previous": abs_url(url_set_query(page=page - 1)) if pagination.has_prev else None,
|
|
|
|
"next": abs_url(url_set_query(page=page + 1)) if pagination.has_next else None,
|
2021-11-26 15:33:17 +01:00
|
|
|
},
|
2023-06-18 22:56:19 +02:00
|
|
|
"items": [review.as_dict(True) for review in pagination.items],
|
2021-11-26 15:33:17 +01:00
|
|
|
})
|
2021-11-24 17:33:37 +01:00
|
|
|
|
|
|
|
|
2022-11-06 11:32:46 +01:00
|
|
|
@bp.route("/api/packages/<author>/<name>/stats/")
|
|
|
|
@is_package_page
|
|
|
|
@cors_allowed
|
2022-11-18 22:36:15 +01:00
|
|
|
@cached(300)
|
2022-11-06 11:32:46 +01:00
|
|
|
def package_stats(package: Package):
|
2023-06-14 23:47:08 +02:00
|
|
|
start = get_request_date("start")
|
|
|
|
end = get_request_date("end")
|
|
|
|
return jsonify(get_package_stats(package, start, end))
|
2022-11-06 11:32:46 +01:00
|
|
|
|
|
|
|
|
2022-11-15 02:51:21 +01:00
|
|
|
@bp.route("/api/package_stats/")
|
|
|
|
@cors_allowed
|
2022-11-18 22:36:15 +01:00
|
|
|
@cached(900)
|
2022-11-15 02:51:21 +01:00
|
|
|
def all_package_stats():
|
|
|
|
return jsonify(get_all_package_stats())
|
|
|
|
|
|
|
|
|
2021-02-02 23:41:48 +01:00
|
|
|
@bp.route("/api/scores/")
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2024-06-01 01:06:25 +02:00
|
|
|
@cached(900)
|
2021-02-02 23:41:48 +01:00
|
|
|
def package_scores():
|
2023-06-19 20:32:36 +02:00
|
|
|
qb = QueryBuilder(request.args)
|
2023-06-19 22:27:49 +02:00
|
|
|
query = qb.build_package_query()
|
2021-02-02 23:41:48 +01:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
pkgs = [package.as_score_dict() for package in query.all()]
|
2021-02-02 23:41:48 +01:00
|
|
|
return jsonify(pkgs)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/api/tags/")
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2024-01-01 18:35:11 +01:00
|
|
|
@cached(60*60)
|
2021-02-02 23:41:48 +01:00
|
|
|
def tags():
|
2023-06-18 22:56:19 +02:00
|
|
|
return jsonify([tag.as_dict() for tag in Tag.query.all() ])
|
2021-02-02 23:41:48 +01:00
|
|
|
|
|
|
|
|
2021-02-03 00:58:59 +01:00
|
|
|
@bp.route("/api/content_warnings/")
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2024-01-01 18:35:11 +01:00
|
|
|
@cached(60*60)
|
2021-02-03 00:58:59 +01:00
|
|
|
def content_warnings():
|
2023-06-18 22:56:19 +02:00
|
|
|
return jsonify([warning.as_dict() for warning in ContentWarning.query.all() ])
|
2021-02-03 00:58:59 +01:00
|
|
|
|
|
|
|
|
2021-02-02 23:41:48 +01:00
|
|
|
@bp.route("/api/licenses/")
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2024-01-01 18:35:11 +01:00
|
|
|
@cached(60*60)
|
2021-02-02 23:41:48 +01:00
|
|
|
def licenses():
|
2023-06-19 20:32:36 +02:00
|
|
|
all_licenses = License.query.order_by(db.asc(License.name)).all()
|
|
|
|
return jsonify([{"name": license.name, "is_foss": license.is_foss} for license in all_licenses])
|
2021-02-02 23:41:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/api/homepage/")
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2024-01-01 18:35:11 +01:00
|
|
|
@cached(300)
|
2021-02-02 23:41:48 +01:00
|
|
|
def homepage():
|
2023-06-19 20:32:36 +02:00
|
|
|
query = Package.query.filter_by(state=PackageState.APPROVED)
|
|
|
|
count = query.count()
|
2021-02-02 23:41:48 +01:00
|
|
|
|
2023-08-20 23:25:18 +02:00
|
|
|
spotlight = query.filter(
|
2023-08-22 01:16:32 +02:00
|
|
|
Package.collections.any(and_(Collection.name == "spotlight", Collection.author.has(username="ContentDB")))) \
|
2023-08-20 23:25:18 +02:00
|
|
|
.order_by(func.random()).limit(6).all()
|
2023-06-19 20:32:36 +02:00
|
|
|
new = query.order_by(db.desc(Package.approved_at)).limit(4).all()
|
2021-02-02 23:41:48 +01:00
|
|
|
pop_mod = query.filter_by(type=PackageType.MOD).order_by(db.desc(Package.score)).limit(8).all()
|
|
|
|
pop_gam = query.filter_by(type=PackageType.GAME).order_by(db.desc(Package.score)).limit(8).all()
|
|
|
|
pop_txp = query.filter_by(type=PackageType.TXP).order_by(db.desc(Package.score)).limit(8).all()
|
|
|
|
high_reviewed = query.order_by(db.desc(Package.score - Package.score_downloads)) \
|
|
|
|
.filter(Package.reviews.any()).limit(4).all()
|
|
|
|
|
|
|
|
updated = db.session.query(Package).select_from(PackageRelease).join(Package) \
|
|
|
|
.filter_by(state=PackageState.APPROVED) \
|
2024-06-22 16:18:58 +02:00
|
|
|
.order_by(db.desc(PackageRelease.created_at)) \
|
2021-02-02 23:41:48 +01:00
|
|
|
.limit(20).all()
|
|
|
|
updated = updated[:4]
|
|
|
|
|
|
|
|
downloads_result = db.session.query(func.sum(Package.downloads)).one_or_none()
|
|
|
|
downloads = 0 if not downloads_result or not downloads_result[0] else downloads_result[0]
|
|
|
|
|
2023-06-19 20:32:36 +02:00
|
|
|
def map_packages(packages: List[Package]):
|
2023-06-18 22:56:19 +02:00
|
|
|
return [pkg.as_short_dict(current_app.config["BASE_URL"]) for pkg in packages]
|
2021-02-02 23:41:48 +01:00
|
|
|
|
2021-11-25 11:28:15 +01:00
|
|
|
return jsonify({
|
2021-02-02 23:41:48 +01:00
|
|
|
"count": count,
|
|
|
|
"downloads": downloads,
|
2023-06-19 20:32:36 +02:00
|
|
|
"spotlight": map_packages(spotlight),
|
|
|
|
"new": map_packages(new),
|
|
|
|
"updated": map_packages(updated),
|
|
|
|
"pop_mod": map_packages(pop_mod),
|
|
|
|
"pop_txp": map_packages(pop_txp),
|
|
|
|
"pop_game": map_packages(pop_gam),
|
|
|
|
"high_reviewed": map_packages(high_reviewed)
|
2021-11-25 11:28:15 +01:00
|
|
|
})
|
2021-02-02 23:41:48 +01:00
|
|
|
|
|
|
|
|
2022-01-30 04:35:02 +01:00
|
|
|
@bp.route("/api/welcome/v1/")
|
|
|
|
@cors_allowed
|
|
|
|
def welcome_v1():
|
|
|
|
featured = Package.query \
|
|
|
|
.filter(Package.type == PackageType.GAME, Package.state == PackageState.APPROVED,
|
2023-08-20 23:25:18 +02:00
|
|
|
Package.collections.any(
|
|
|
|
and_(Collection.name == "featured", Collection.author.has(username="ContentDB")))) \
|
2022-01-30 04:35:02 +01:00
|
|
|
.order_by(func.random()) \
|
|
|
|
.limit(5).all()
|
|
|
|
|
|
|
|
def map_packages(packages: List[Package]):
|
2023-06-18 22:56:19 +02:00
|
|
|
return [pkg.as_short_dict(current_app.config["BASE_URL"]) for pkg in packages]
|
2022-01-30 04:35:02 +01:00
|
|
|
|
|
|
|
return jsonify({
|
|
|
|
"featured": map_packages(featured),
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2021-02-02 23:41:48 +01:00
|
|
|
@bp.route("/api/minetest_versions/")
|
2021-11-24 16:39:50 +01:00
|
|
|
@cors_allowed
|
2021-02-02 23:41:48 +01:00
|
|
|
def versions():
|
2021-05-03 19:27:20 +02:00
|
|
|
protocol_version = request.args.get("protocol_version")
|
|
|
|
engine_version = request.args.get("engine_version")
|
|
|
|
if protocol_version or engine_version:
|
|
|
|
rel = MinetestRelease.get(engine_version, get_int_or_abort(protocol_version))
|
|
|
|
if rel is None:
|
|
|
|
error(404, "No releases found")
|
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
return jsonify(rel.as_dict())
|
2021-05-03 19:27:20 +02:00
|
|
|
|
2023-06-18 22:56:19 +02:00
|
|
|
return jsonify([rel.as_dict() \
|
|
|
|
for rel in MinetestRelease.query.all() if rel.get_actual() is not None])
|
2022-01-12 18:08:18 +01:00
|
|
|
|
|
|
|
|
2024-02-26 02:50:15 +01:00
|
|
|
@bp.route("/api/languages/")
|
|
|
|
@cors_allowed
|
|
|
|
def languages():
|
|
|
|
return jsonify([x.as_dict() for x in Language.query.all()])
|
|
|
|
|
|
|
|
|
2022-01-12 18:08:18 +01:00
|
|
|
@bp.route("/api/dependencies/")
|
|
|
|
@cors_allowed
|
|
|
|
def all_deps():
|
|
|
|
qb = QueryBuilder(request.args)
|
2023-06-19 22:27:49 +02:00
|
|
|
query = qb.build_package_query()
|
2022-01-12 18:08:18 +01:00
|
|
|
|
|
|
|
def format_pkg(pkg: Package):
|
|
|
|
return {
|
2023-06-18 22:56:19 +02:00
|
|
|
"type": pkg.type.to_name(),
|
2022-01-12 18:08:18 +01:00
|
|
|
"author": pkg.author.username,
|
|
|
|
"name": pkg.name,
|
|
|
|
"provides": [x.name for x in pkg.provides],
|
|
|
|
"depends": [str(x) for x in pkg.dependencies if not x.optional],
|
|
|
|
"optional_depends": [str(x) for x in pkg.dependencies if x.optional],
|
|
|
|
}
|
|
|
|
|
|
|
|
page = get_int_or_abort(request.args.get("page"), 1)
|
2022-01-12 21:50:01 +01:00
|
|
|
num = min(get_int_or_abort(request.args.get("n"), 100), 300)
|
2023-04-23 22:49:53 +02:00
|
|
|
pagination: flask_sqlalchemy.Pagination = query.paginate(page=page, per_page=num)
|
2022-01-12 18:08:18 +01:00
|
|
|
return jsonify({
|
|
|
|
"page": pagination.page,
|
|
|
|
"per_page": pagination.per_page,
|
|
|
|
"page_count": math.ceil(pagination.total / pagination.per_page),
|
|
|
|
"total": pagination.total,
|
|
|
|
"urls": {
|
|
|
|
"previous": abs_url(url_set_query(page=page - 1)) if pagination.has_prev else None,
|
|
|
|
"next": abs_url(url_set_query(page=page + 1)) if pagination.has_next else None,
|
|
|
|
},
|
|
|
|
"items": [format_pkg(pkg) for pkg in pagination.items],
|
|
|
|
})
|
2022-11-09 19:46:11 +01:00
|
|
|
|
|
|
|
|
2022-11-09 21:43:31 +01:00
|
|
|
@bp.route("/api/users/<username>/")
|
|
|
|
@cors_allowed
|
|
|
|
def user_view(username: str):
|
|
|
|
user = User.query.filter_by(username=username).first()
|
|
|
|
if user is None:
|
|
|
|
error(404, "User not found")
|
|
|
|
|
|
|
|
return jsonify(user.get_dict())
|
|
|
|
|
|
|
|
|
2022-11-09 19:46:11 +01:00
|
|
|
@bp.route("/api/users/<username>/stats/")
|
|
|
|
@cors_allowed
|
2024-01-01 18:35:11 +01:00
|
|
|
@cached(300)
|
2022-11-09 19:46:11 +01:00
|
|
|
def user_stats(username: str):
|
|
|
|
user = User.query.filter_by(username=username).first()
|
|
|
|
if user is None:
|
|
|
|
error(404, "User not found")
|
|
|
|
|
2023-06-14 23:47:08 +02:00
|
|
|
start = get_request_date("start")
|
|
|
|
end = get_request_date("end")
|
|
|
|
return jsonify(get_package_stats_for_user(user, start, end))
|
2023-01-02 20:26:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/api/cdb_schema/")
|
|
|
|
@cors_allowed
|
2024-01-01 18:35:11 +01:00
|
|
|
@cached(60*60)
|
2023-01-02 20:26:10 +01:00
|
|
|
def json_schema():
|
|
|
|
tags = Tag.query.all()
|
|
|
|
warnings = ContentWarning.query.all()
|
|
|
|
licenses = License.query.order_by(db.asc(License.name)).all()
|
|
|
|
return jsonify({
|
|
|
|
"title": "CDB Config",
|
|
|
|
"description": "Package Configuration",
|
|
|
|
"type": "object",
|
|
|
|
"$defs": {
|
|
|
|
"license": {
|
|
|
|
"enum": [license.name for license in licenses],
|
|
|
|
"enumDescriptions": [license.is_foss and "FOSS" or "NON-FOSS" for license in licenses]
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"properties": {
|
|
|
|
"type": {
|
|
|
|
"description": "Package Type",
|
|
|
|
"enum": ["MOD", "GAME", "TXP"],
|
|
|
|
"enumDescriptions": ["Mod", "Game", "Texture Pack"]
|
|
|
|
},
|
|
|
|
"title": {
|
|
|
|
"description": "Human-readable title",
|
|
|
|
"type": "string"
|
|
|
|
},
|
|
|
|
"name": {
|
|
|
|
"description": "Technical name (needs permission if already approved).",
|
|
|
|
"type": "string",
|
|
|
|
"pattern": "^[a-z_]+$"
|
|
|
|
},
|
|
|
|
"short_description": {
|
|
|
|
"description": "Package Short Description",
|
|
|
|
"type": ["string", "null"]
|
|
|
|
},
|
|
|
|
"dev_state": {
|
|
|
|
"description": "Development State",
|
|
|
|
"enum": [
|
|
|
|
"WIP",
|
|
|
|
"BETA",
|
|
|
|
"ACTIVELY_DEVELOPED",
|
|
|
|
"MAINTENANCE_ONLY",
|
|
|
|
"AS_IS",
|
|
|
|
"DEPRECATED",
|
|
|
|
"LOOKING_FOR_MAINTAINER"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"tags": {
|
|
|
|
"description": "Package Tags",
|
|
|
|
"type": "array",
|
|
|
|
"items": {
|
|
|
|
"enum": [tag.name for tag in tags],
|
|
|
|
"enumDescriptions": [tag.title for tag in tags]
|
|
|
|
},
|
|
|
|
"uniqueItems": True,
|
|
|
|
},
|
|
|
|
"content_warnings": {
|
|
|
|
"description": "Package Content Warnings",
|
|
|
|
"type": "array",
|
|
|
|
"items": {
|
|
|
|
"enum": [warning.name for warning in warnings],
|
|
|
|
"enumDescriptions": [warning.title for warning in warnings]
|
|
|
|
},
|
|
|
|
"uniqueItems": True,
|
|
|
|
},
|
|
|
|
"license": {
|
|
|
|
"description": "Package License",
|
|
|
|
"$ref": "#/$defs/license"
|
|
|
|
},
|
|
|
|
"media_license": {
|
|
|
|
"description": "Package Media License",
|
|
|
|
"$ref": "#/$defs/license"
|
|
|
|
},
|
|
|
|
"long_description": {
|
|
|
|
"description": "Package Long Description",
|
|
|
|
"type": ["string", "null"]
|
|
|
|
},
|
|
|
|
"repo": {
|
|
|
|
"description": "Git Repository URL",
|
|
|
|
"type": "string",
|
|
|
|
"format": "uri"
|
|
|
|
},
|
|
|
|
"website": {
|
|
|
|
"description": "Website URL",
|
|
|
|
"type": ["string", "null"],
|
|
|
|
"format": "uri"
|
|
|
|
},
|
|
|
|
"issue_tracker": {
|
|
|
|
"description": "Issue Tracker URL",
|
|
|
|
"type": ["string", "null"],
|
|
|
|
"format": "uri"
|
|
|
|
},
|
|
|
|
"forums": {
|
|
|
|
"description": "Forum Topic ID",
|
|
|
|
"type": ["integer", "null"],
|
|
|
|
"minimum": 0
|
|
|
|
},
|
|
|
|
"video_url": {
|
|
|
|
"description": "URL to a Video",
|
|
|
|
"type": ["string", "null"],
|
|
|
|
"format": "uri"
|
|
|
|
},
|
2023-03-05 19:13:07 +01:00
|
|
|
"donate_url": {
|
|
|
|
"description": "URL to a donation page",
|
|
|
|
"type": ["string", "null"],
|
|
|
|
"format": "uri"
|
|
|
|
},
|
2024-02-25 19:05:29 +01:00
|
|
|
"translation_url": {
|
|
|
|
"description": "URL to send users interested in translating your package",
|
|
|
|
"type": ["string", "null"],
|
|
|
|
"format": "uri"
|
|
|
|
}
|
2023-01-02 20:26:10 +01:00
|
|
|
},
|
|
|
|
})
|
2023-08-13 14:50:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/api/hypertext/", methods=["POST"])
|
|
|
|
@csrf.exempt
|
|
|
|
@cors_allowed
|
|
|
|
def hypertext():
|
2024-04-05 19:17:07 +02:00
|
|
|
formspec_version = get_int_or_abort(request.args["formspec_version"])
|
2023-08-13 14:50:02 +02:00
|
|
|
include_images = is_yes(request.args.get("include_images", "true"))
|
|
|
|
|
|
|
|
html = request.data.decode("utf-8")
|
|
|
|
if request.content_type == "text/markdown":
|
|
|
|
html = render_markdown(html)
|
|
|
|
|
2024-04-05 19:17:07 +02:00
|
|
|
return jsonify(html_to_minetest(html, "", formspec_version, include_images))
|
2023-08-20 22:47:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/api/collections/")
|
|
|
|
@cors_allowed
|
|
|
|
def collection_list():
|
|
|
|
if "author" in request.args:
|
|
|
|
user = User.query.filter_by(username=request.args["author"]).one_or_404()
|
|
|
|
query = user.collections
|
|
|
|
else:
|
|
|
|
query = Collection.query.order_by(db.asc(Collection.title))
|
|
|
|
|
|
|
|
if "package" in request.args:
|
|
|
|
id_ = request.args["package"]
|
|
|
|
package = Package.get_by_key(id_)
|
|
|
|
if package is None:
|
|
|
|
error(404, f"Package {id_} not found")
|
|
|
|
|
|
|
|
query = query.filter(Collection.packages.contains(package))
|
|
|
|
|
|
|
|
collections = [x.as_short_dict() for x in query.all() if not x.private]
|
|
|
|
return jsonify(collections)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/api/collections/<author>/<name>/")
|
2024-03-30 18:46:54 +01:00
|
|
|
@is_api_authd
|
2023-08-20 22:47:20 +02:00
|
|
|
@cors_allowed
|
2024-03-30 18:46:54 +01:00
|
|
|
def collection_view(token, author, name):
|
|
|
|
user = token.owner if token else None
|
|
|
|
|
2023-08-20 22:47:20 +02:00
|
|
|
collection = Collection.query \
|
|
|
|
.filter(Collection.name == name, Collection.author.has(username=author)) \
|
|
|
|
.one_or_404()
|
|
|
|
|
2024-03-30 18:46:54 +01:00
|
|
|
if not collection.check_perm(user, Permission.VIEW_COLLECTION):
|
2023-08-20 22:47:20 +02:00
|
|
|
error(404, "Collection not found")
|
|
|
|
|
|
|
|
items = collection.items
|
2024-03-30 18:46:54 +01:00
|
|
|
if not collection.check_perm(user, Permission.EDIT_COLLECTION):
|
|
|
|
items = [x for x in items if x.package.check_perm(user, Permission.VIEW_PACKAGE)]
|
2023-08-20 22:47:20 +02:00
|
|
|
|
|
|
|
ret = collection.as_dict()
|
|
|
|
ret["items"] = [x.as_dict() for x in items]
|
|
|
|
return jsonify(ret)
|
2023-09-12 22:15:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/api/updates/")
|
2024-01-01 18:35:11 +01:00
|
|
|
@cors_allowed
|
|
|
|
@cached(300)
|
2023-09-12 22:15:55 +02:00
|
|
|
def updates():
|
|
|
|
protocol_version = get_int_or_abort(request.args.get("protocol_version"))
|
|
|
|
minetest_version = request.args.get("engine_version")
|
|
|
|
if protocol_version or minetest_version:
|
|
|
|
version = MinetestRelease.get(minetest_version, protocol_version)
|
|
|
|
else:
|
|
|
|
version = None
|
|
|
|
|
|
|
|
# Subquery to get the latest release for each package
|
|
|
|
latest_release_query = (db.session.query(
|
|
|
|
PackageRelease.package_id,
|
|
|
|
func.max(PackageRelease.id).label('max_release_id'))
|
|
|
|
.select_from(PackageRelease)
|
|
|
|
.filter(PackageRelease.approved == True))
|
|
|
|
|
|
|
|
if version:
|
|
|
|
latest_release_query = (latest_release_query
|
|
|
|
.filter(or_(PackageRelease.min_rel_id == None,
|
|
|
|
PackageRelease.min_rel_id <= version.id))
|
|
|
|
.filter(or_(PackageRelease.max_rel_id == None,
|
|
|
|
PackageRelease.max_rel_id >= version.id)))
|
|
|
|
|
|
|
|
latest_release_subquery = (
|
|
|
|
latest_release_query
|
|
|
|
.group_by(PackageRelease.package_id)
|
|
|
|
.subquery()
|
|
|
|
)
|
|
|
|
|
|
|
|
# Get package id and latest release
|
|
|
|
query = (db.session.query(User.username, Package.name, latest_release_subquery.c.max_release_id)
|
|
|
|
.select_from(Package)
|
|
|
|
.join(User, Package.author)
|
|
|
|
.join(latest_release_subquery, Package.id == latest_release_subquery.c.package_id)
|
2023-09-14 21:41:48 +02:00
|
|
|
.filter(Package.state == PackageState.APPROVED)
|
|
|
|
.all())
|
2023-09-12 22:15:55 +02:00
|
|
|
|
|
|
|
ret = {}
|
|
|
|
for author_username, package_name, release_id in query:
|
|
|
|
ret[f"{author_username}/{package_name}"] = release_id
|
|
|
|
|
2023-09-14 21:41:48 +02:00
|
|
|
# Get aliases
|
|
|
|
aliases = (db.session.query(PackageAlias.author, PackageAlias.name, User.username, Package.name)
|
|
|
|
.select_from(PackageAlias)
|
|
|
|
.join(Package, PackageAlias.package)
|
|
|
|
.join(User, Package.author)
|
|
|
|
.filter(Package.state == PackageState.APPROVED)
|
|
|
|
.all())
|
|
|
|
|
|
|
|
for old_author, old_name, new_author, new_name in aliases:
|
|
|
|
new_release = ret.get(f"{new_author}/{new_name}")
|
|
|
|
if new_release is not None:
|
|
|
|
ret[f"{old_author}/{old_name}"] = new_release
|
|
|
|
|
2023-09-12 22:15:55 +02:00
|
|
|
return jsonify(ret)
|