2023-06-19 20:32:36 +02:00
|
|
|
# ContentDB
|
|
|
|
# Copyright (C) rubenwardy
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# 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
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2024-06-05 20:27:09 +02:00
|
|
|
from typing import Optional, List
|
|
|
|
from flask import abort, current_app, request, make_response
|
2024-02-25 22:53:55 +01:00
|
|
|
from flask_babel import lazy_gettext, gettext, get_locale
|
2019-01-28 22:31:08 +01:00
|
|
|
from sqlalchemy import or_
|
2021-01-30 20:32:04 +01:00
|
|
|
from sqlalchemy.orm import subqueryload
|
2020-12-04 03:23:04 +01:00
|
|
|
from sqlalchemy.sql.expression import func
|
2022-08-18 17:44:40 +02:00
|
|
|
from sqlalchemy_searchable import search
|
2020-12-04 03:23:04 +01:00
|
|
|
|
2021-12-20 22:07:12 +01:00
|
|
|
from .models import db, PackageType, Package, ForumTopic, License, MinetestRelease, PackageRelease, User, Tag, \
|
|
|
|
ContentWarning, PackageState, PackageDevState
|
2023-06-19 22:27:49 +02:00
|
|
|
from .utils import is_yes, get_int_or_abort
|
2020-12-04 03:23:04 +01:00
|
|
|
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
class QueryBuilder:
|
2024-06-05 20:27:09 +02:00
|
|
|
limit: Optional[int]
|
2024-02-25 19:43:27 +01:00
|
|
|
lang: str = "en"
|
2024-06-05 20:27:09 +02:00
|
|
|
types: List[PackageType]
|
|
|
|
search: Optional[str] = None
|
|
|
|
only_approved: bool = True
|
|
|
|
licenses: List[License]
|
|
|
|
tags: List[Tag]
|
|
|
|
game: Optional[Package]
|
|
|
|
author: Optional[str]
|
|
|
|
random: bool
|
|
|
|
lucky: bool
|
|
|
|
order_dir: str
|
|
|
|
order_by: Optional[str]
|
|
|
|
flags: set[str]
|
|
|
|
hide_flags: set[str]
|
|
|
|
hide_deprecated: bool
|
|
|
|
hide_wip: bool
|
|
|
|
hide_nonfree: bool
|
|
|
|
show_added: bool
|
|
|
|
version: Optional[MinetestRelease]
|
|
|
|
has_lang: Optional[str]
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2022-10-28 17:21:31 +02:00
|
|
|
@property
|
|
|
|
def title(self):
|
|
|
|
if len(self.types) == 1:
|
|
|
|
package_type = str(self.types[0].plural)
|
|
|
|
else:
|
|
|
|
package_type = lazy_gettext("Packages")
|
|
|
|
|
|
|
|
if len(self.tags) == 0:
|
|
|
|
ret = package_type
|
|
|
|
elif len(self.tags) == 1:
|
2024-06-07 06:28:57 +02:00
|
|
|
ret = self.tags[0].get_translated()["title"] + " " + package_type
|
2022-10-28 17:21:31 +02:00
|
|
|
else:
|
2024-06-07 06:28:57 +02:00
|
|
|
tags = ", ".join([tag.get_translated()["title"] for tag in self.tags])
|
2022-10-28 17:21:31 +02:00
|
|
|
ret = f"{tags} - {package_type}"
|
|
|
|
|
|
|
|
if self.search:
|
|
|
|
ret = f"{self.search} - {ret}"
|
|
|
|
|
2024-01-28 23:35:49 +01:00
|
|
|
if self.game:
|
2024-02-26 01:45:25 +01:00
|
|
|
meta = self.game.get_translated(load_desc=False)
|
|
|
|
ret = gettext("%(package_type)s for %(game_name)s", package_type=ret, game_name=meta["title"])
|
2024-01-28 23:35:49 +01:00
|
|
|
|
2022-10-28 17:21:31 +02:00
|
|
|
return ret
|
|
|
|
|
2024-06-08 11:46:47 +02:00
|
|
|
@property
|
|
|
|
def query_hint(self):
|
|
|
|
return self.title
|
|
|
|
|
2023-04-24 01:17:26 +02:00
|
|
|
@property
|
|
|
|
def noindex(self):
|
|
|
|
return (self.search is not None or len(self.tags) > 1 or len(self.types) > 1 or len(self.hide_flags) > 0 or
|
2024-06-05 20:45:37 +02:00
|
|
|
self.random or self.lucky or self.author or self.version or self.game or len(self.licenses) > 0)
|
2023-04-24 01:17:26 +02:00
|
|
|
|
2024-06-05 20:27:09 +02:00
|
|
|
def __init__(self, args, cookies: bool = False, lang: Optional[str] = None, emit_http_errors: bool = True):
|
2024-02-25 22:53:55 +01:00
|
|
|
if lang is None:
|
|
|
|
locale = get_locale()
|
|
|
|
if locale:
|
|
|
|
self.lang = locale.language
|
|
|
|
else:
|
|
|
|
self.lang = lang
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
# Get request types
|
|
|
|
types = args.getlist("type")
|
|
|
|
types = [PackageType.get(tname) for tname in types]
|
|
|
|
types = [type for type in types if type is not None]
|
|
|
|
|
2020-04-07 19:23:06 +02:00
|
|
|
# Get tags types
|
|
|
|
tags = args.getlist("tag")
|
|
|
|
tags = [Tag.query.filter_by(name=tname).first() for tname in tags]
|
|
|
|
tags = [tag for tag in tags if tag is not None]
|
|
|
|
|
2020-07-15 20:06:00 +02:00
|
|
|
# Hide
|
2021-12-20 22:24:06 +01:00
|
|
|
self.hide_flags = set(args.getlist("hide"))
|
2019-02-03 13:59:33 +01:00
|
|
|
|
2023-12-30 18:09:10 +01:00
|
|
|
# Show flags
|
|
|
|
self.flags = set(args.getlist("flag"))
|
|
|
|
|
2024-06-05 20:27:09 +02:00
|
|
|
# License
|
2024-06-05 20:31:48 +02:00
|
|
|
self.licenses = [License.query.filter(func.lower(License.name) == name.lower()).first() for name in args.getlist("license")]
|
2024-06-05 20:27:09 +02:00
|
|
|
if emit_http_errors and any(map(lambda x: x is None, self.licenses)):
|
2024-06-05 20:45:37 +02:00
|
|
|
all_licenses = db.session.query(License.name).order_by(db.asc(License.name)).all()
|
|
|
|
all_licenses = [x[0] for x in all_licenses]
|
|
|
|
abort(make_response("Unknown license. Expected license name from: " + ", ".join(all_licenses)), 400)
|
2024-06-05 20:27:09 +02:00
|
|
|
|
2019-01-08 18:17:36 +01:00
|
|
|
self.types = types
|
2020-04-07 19:23:06 +02:00
|
|
|
self.tags = tags
|
2020-03-28 19:09:20 +01:00
|
|
|
|
2019-01-08 18:17:36 +01:00
|
|
|
self.random = "random" in args
|
2019-11-21 22:43:58 +01:00
|
|
|
self.lucky = "lucky" in args
|
2021-11-25 16:51:41 +01:00
|
|
|
self.limit = 1 if self.lucky else get_int_or_abort(args.get("limit"), None)
|
2019-03-29 20:45:29 +01:00
|
|
|
self.order_by = args.get("sort")
|
2020-07-15 16:44:59 +02:00
|
|
|
self.order_dir = args.get("order") or "desc"
|
2020-03-28 19:09:20 +01:00
|
|
|
|
2021-12-20 22:24:06 +01:00
|
|
|
if "android_default" in self.hide_flags:
|
2021-12-23 12:56:03 +01:00
|
|
|
self.hide_flags.update(["*", "deprecated"])
|
2021-12-20 22:24:06 +01:00
|
|
|
self.hide_flags.discard("android_default")
|
2021-12-20 22:07:12 +01:00
|
|
|
|
2021-12-20 22:24:06 +01:00
|
|
|
if "desktop_default" in self.hide_flags:
|
2021-12-23 12:56:03 +01:00
|
|
|
self.hide_flags.update(["deprecated"])
|
2021-12-20 22:24:06 +01:00
|
|
|
self.hide_flags.discard("desktop_default")
|
2021-12-20 22:07:12 +01:00
|
|
|
|
2021-12-20 22:24:06 +01:00
|
|
|
self.hide_nonfree = "nonfree" in self.hide_flags
|
|
|
|
self.hide_wip = "wip" in self.hide_flags
|
|
|
|
self.hide_deprecated = "deprecated" in self.hide_flags
|
2020-07-17 22:18:27 +02:00
|
|
|
self.hide_flags.discard("nonfree")
|
2021-12-20 22:07:12 +01:00
|
|
|
self.hide_flags.discard("wip")
|
|
|
|
self.hide_flags.discard("deprecated")
|
2020-07-17 22:18:27 +02:00
|
|
|
|
2020-03-28 19:09:20 +01:00
|
|
|
# Filters
|
|
|
|
self.search = args.get("q")
|
|
|
|
self.author = args.get("author")
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2021-01-30 20:32:04 +01:00
|
|
|
protocol_version = get_int_or_abort(args.get("protocol_version"))
|
|
|
|
minetest_version = args.get("engine_version")
|
|
|
|
if protocol_version or minetest_version:
|
|
|
|
self.version = MinetestRelease.get(minetest_version, protocol_version)
|
|
|
|
else:
|
|
|
|
self.version = None
|
|
|
|
|
2019-03-29 20:45:29 +01:00
|
|
|
self.show_added = args.get("show_added")
|
|
|
|
if self.show_added is not None:
|
2023-06-19 22:27:49 +02:00
|
|
|
self.show_added = is_yes(self.show_added)
|
2019-03-29 20:45:29 +01:00
|
|
|
|
2019-01-08 18:37:33 +01:00
|
|
|
if self.search is not None and self.search.strip() == "":
|
|
|
|
self.search = None
|
|
|
|
|
2022-02-01 22:22:28 +01:00
|
|
|
self.game = args.get("game")
|
|
|
|
if self.game:
|
|
|
|
self.game = Package.get_by_key(self.game)
|
|
|
|
|
2024-02-26 01:45:25 +01:00
|
|
|
self.has_lang = args.get("lang")
|
|
|
|
|
2023-12-30 19:07:00 +01:00
|
|
|
if cookies and request.cookies.get("hide_nonfree") == "1":
|
|
|
|
self.hide_nonfree = True
|
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
def set_sort_if_none(self, name, dir="desc"):
|
2019-03-29 20:45:29 +01:00
|
|
|
if self.order_by is None:
|
|
|
|
self.order_by = name
|
2020-07-15 01:21:20 +02:00
|
|
|
self.order_dir = dir
|
2019-03-29 20:45:29 +01:00
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
def get_releases(self):
|
2021-01-30 20:32:04 +01:00
|
|
|
releases_query = db.session.query(PackageRelease.package_id, func.max(PackageRelease.id)) \
|
|
|
|
.select_from(PackageRelease).filter(PackageRelease.approved) \
|
|
|
|
.group_by(PackageRelease.package_id)
|
|
|
|
|
|
|
|
if self.version:
|
|
|
|
releases_query = releases_query \
|
2023-03-29 11:58:09 +02:00
|
|
|
.filter(or_(PackageRelease.min_rel_id==None,
|
2021-01-30 20:32:04 +01:00
|
|
|
PackageRelease.min_rel_id <= self.version.id)) \
|
2023-03-29 11:58:09 +02:00
|
|
|
.filter(or_(PackageRelease.max_rel_id==None,
|
2021-01-30 20:32:04 +01:00
|
|
|
PackageRelease.max_rel_id >= self.version.id))
|
2019-03-13 15:37:20 +01:00
|
|
|
|
2021-01-30 20:32:04 +01:00
|
|
|
return releases_query.all()
|
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
def convert_to_dictionary(self, packages):
|
2021-01-30 20:32:04 +01:00
|
|
|
releases = {}
|
2023-06-19 22:27:49 +02:00
|
|
|
for [package_id, release_id] in self.get_releases():
|
2021-01-30 20:32:04 +01:00
|
|
|
releases[package_id] = release_id
|
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
def to_json(package: Package):
|
2021-01-30 20:49:15 +01:00
|
|
|
release_id = releases.get(package.id)
|
2024-02-25 19:43:27 +01:00
|
|
|
return package.as_short_dict(current_app.config["BASE_URL"], release_id=release_id, no_load=True, lang=self.lang)
|
2021-01-30 20:32:04 +01:00
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
return [to_json(pkg) for pkg in packages]
|
2019-03-13 15:37:20 +01:00
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
def build_package_query(self):
|
2020-07-18 02:22:52 +02:00
|
|
|
if self.order_by == "last_release":
|
2024-01-18 19:09:08 +01:00
|
|
|
query = db.session.query(Package).select_from(PackageRelease).join(Package)
|
2020-07-18 02:22:52 +02:00
|
|
|
else:
|
2024-01-18 19:09:08 +01:00
|
|
|
query = Package.query
|
|
|
|
|
|
|
|
if self.only_approved:
|
|
|
|
query = query.filter(Package.state == PackageState.APPROVED)
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2021-07-24 03:30:43 +02:00
|
|
|
query = query.options(subqueryload(Package.main_screenshot), subqueryload(Package.aliases))
|
2021-01-30 20:32:04 +01:00
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
query = self.order_package_query(self.filter_package_query(query))
|
2021-01-30 20:32:04 +01:00
|
|
|
|
|
|
|
if self.limit:
|
|
|
|
query = query.limit(self.limit)
|
|
|
|
|
|
|
|
return query
|
2020-07-18 04:14:56 +02:00
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
def filter_package_query(self, query):
|
2019-01-08 18:17:36 +01:00
|
|
|
if len(self.types) > 0:
|
|
|
|
query = query.filter(Package.type.in_(self.types))
|
|
|
|
|
2020-03-28 19:09:20 +01:00
|
|
|
if self.author:
|
|
|
|
author = User.query.filter_by(username=self.author).first()
|
|
|
|
if not author:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
query = query.filter_by(author=author)
|
|
|
|
|
2022-02-01 22:22:28 +01:00
|
|
|
if self.game:
|
2023-06-27 21:41:07 +02:00
|
|
|
query = query.filter(Package.supported_games.any(game=self.game, supports=True))
|
2022-02-01 22:22:28 +01:00
|
|
|
|
2024-02-26 01:45:25 +01:00
|
|
|
if self.has_lang:
|
|
|
|
query = query.filter(Package.translations.any(language_id=self.has_lang))
|
|
|
|
|
2020-04-07 19:23:06 +02:00
|
|
|
for tag in self.tags:
|
|
|
|
query = query.filter(Package.tags.any(Tag.id == tag.id))
|
|
|
|
|
2021-12-20 22:24:06 +01:00
|
|
|
if "*" in self.hide_flags:
|
2020-07-17 22:18:27 +02:00
|
|
|
query = query.filter(~ Package.content_warnings.any())
|
|
|
|
else:
|
|
|
|
for flag in self.hide_flags:
|
|
|
|
warning = ContentWarning.query.filter_by(name=flag).first()
|
|
|
|
if warning:
|
|
|
|
query = query.filter(~ Package.content_warnings.any(ContentWarning.id == warning.id))
|
|
|
|
|
2023-12-30 18:09:10 +01:00
|
|
|
flags = set(self.flags)
|
|
|
|
if "nonfree" in flags:
|
|
|
|
query = query.filter(or_(Package.license.has(is_foss=False), Package.media_license.has(is_foss=False)))
|
|
|
|
flags.discard("nonfree")
|
|
|
|
if "wip" in flags:
|
|
|
|
query = query.filter(Package.dev_state == PackageDevState.WIP)
|
|
|
|
flags.discard("wip")
|
|
|
|
if "deprecated" in flags:
|
|
|
|
query = query.filter(Package.dev_state == PackageDevState.DEPRECATED)
|
|
|
|
flags.discard("deprecated")
|
|
|
|
|
|
|
|
if "*" in flags:
|
|
|
|
query = query.filter(Package.content_warnings.any())
|
|
|
|
flags.discard("*")
|
|
|
|
else:
|
|
|
|
for flag in flags:
|
|
|
|
warning = ContentWarning.query.filter_by(name=flag).first()
|
|
|
|
flags.discard(flag)
|
|
|
|
if warning:
|
|
|
|
query = query.filter(Package.content_warnings.any(ContentWarning.id == warning.id))
|
|
|
|
|
2024-06-05 20:27:09 +02:00
|
|
|
licenses = [Package.license_id == license.id for license in self.licenses if license is not None]
|
|
|
|
licenses.extend([Package.media_license_id == license.id for license in self.licenses if license is not None])
|
|
|
|
if len(licenses) > 0:
|
|
|
|
query = query.filter(or_(*licenses))
|
|
|
|
|
2019-01-08 18:17:36 +01:00
|
|
|
if self.hide_nonfree:
|
|
|
|
query = query.filter(Package.license.has(License.is_foss == True))
|
|
|
|
query = query.filter(Package.media_license.has(License.is_foss == True))
|
|
|
|
|
2021-12-20 22:07:12 +01:00
|
|
|
if self.hide_wip:
|
2023-03-29 11:58:09 +02:00
|
|
|
query = query.filter(or_(Package.dev_state==None, Package.dev_state != PackageDevState.WIP))
|
2021-12-20 22:07:12 +01:00
|
|
|
if self.hide_deprecated:
|
2023-03-29 11:58:09 +02:00
|
|
|
query = query.filter(or_(Package.dev_state==None, Package.dev_state != PackageDevState.DEPRECATED))
|
2021-12-20 22:07:12 +01:00
|
|
|
|
2021-01-30 20:32:04 +01:00
|
|
|
if self.version:
|
|
|
|
query = query.join(Package.releases) \
|
|
|
|
.filter(PackageRelease.approved == True) \
|
2023-03-29 11:58:09 +02:00
|
|
|
.filter(or_(PackageRelease.min_rel_id==None,
|
2021-01-30 20:32:04 +01:00
|
|
|
PackageRelease.min_rel_id <= self.version.id)) \
|
2023-03-29 11:58:09 +02:00
|
|
|
.filter(or_(PackageRelease.max_rel_id==None,
|
2021-01-30 20:32:04 +01:00
|
|
|
PackageRelease.max_rel_id >= self.version.id))
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
return query
|
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
def order_package_query(self, query):
|
2020-07-18 04:14:56 +02:00
|
|
|
if self.search:
|
2022-08-18 17:44:40 +02:00
|
|
|
query = search(query, self.search, sort=self.order_by is None)
|
2020-07-18 04:14:56 +02:00
|
|
|
|
|
|
|
if self.random:
|
|
|
|
query = query.order_by(func.random())
|
|
|
|
return query
|
|
|
|
|
|
|
|
to_order = None
|
|
|
|
if self.order_by is None and self.search:
|
|
|
|
pass
|
|
|
|
elif self.order_by is None or self.order_by == "score":
|
|
|
|
to_order = Package.score
|
2020-12-14 12:48:07 +01:00
|
|
|
elif self.order_by == "reviews":
|
|
|
|
query = query.filter(Package.reviews.any())
|
|
|
|
to_order = (Package.score - Package.score_downloads)
|
2020-07-18 04:14:56 +02:00
|
|
|
elif self.order_by == "name":
|
|
|
|
to_order = Package.name
|
|
|
|
elif self.order_by == "title":
|
|
|
|
to_order = Package.title
|
|
|
|
elif self.order_by == "downloads":
|
|
|
|
to_order = Package.downloads
|
|
|
|
elif self.order_by == "created_at" or self.order_by == "date":
|
|
|
|
to_order = Package.created_at
|
|
|
|
elif self.order_by == "approved_at" or self.order_by == "date":
|
|
|
|
to_order = Package.approved_at
|
|
|
|
elif self.order_by == "last_release":
|
|
|
|
to_order = PackageRelease.releaseDate
|
|
|
|
else:
|
|
|
|
abort(400)
|
|
|
|
|
2020-12-14 12:48:07 +01:00
|
|
|
if to_order is not None:
|
2020-07-18 04:14:56 +02:00
|
|
|
if self.order_dir == "asc":
|
|
|
|
to_order = db.asc(to_order)
|
|
|
|
elif self.order_dir == "desc":
|
|
|
|
to_order = db.desc(to_order)
|
|
|
|
else:
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
query = query.order_by(to_order)
|
|
|
|
|
|
|
|
return query
|
|
|
|
|
2023-06-19 22:27:49 +02:00
|
|
|
def build_topic_query(self, show_added=False):
|
2019-03-29 20:45:29 +01:00
|
|
|
query = ForumTopic.query
|
|
|
|
|
|
|
|
show_added = self.show_added == True or (self.show_added is None and show_added)
|
|
|
|
if not show_added:
|
|
|
|
query = query.filter(~ db.exists().where(Package.forums==ForumTopic.topic_id))
|
|
|
|
|
|
|
|
if self.order_by is None or self.order_by == "name":
|
|
|
|
query = query.order_by(db.asc(ForumTopic.wip), db.asc(ForumTopic.name), db.asc(ForumTopic.title))
|
|
|
|
elif self.order_by == "views":
|
|
|
|
query = query.order_by(db.desc(ForumTopic.views))
|
2020-07-10 23:32:52 +02:00
|
|
|
elif self.order_by == "created_at" or self.order_by == "date":
|
2019-03-29 20:45:29 +01:00
|
|
|
query = query.order_by(db.asc(ForumTopic.created_at))
|
2019-01-08 18:37:33 +01:00
|
|
|
|
|
|
|
if self.search:
|
2021-02-25 15:11:10 +01:00
|
|
|
query = query.filter(or_(ForumTopic.title.ilike('%' + self.search + '%'),
|
|
|
|
ForumTopic.name == self.search.lower()))
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
if len(self.types) > 0:
|
2019-03-29 20:45:29 +01:00
|
|
|
query = query.filter(ForumTopic.type.in_(self.types))
|
2019-01-08 18:17:36 +01:00
|
|
|
|
|
|
|
if self.limit:
|
2019-03-29 20:45:29 +01:00
|
|
|
query = query.limit(self.limit)
|
2019-01-08 18:17:36 +01:00
|
|
|
|
2019-03-29 20:45:29 +01:00
|
|
|
return query
|