diff --git a/app/models.py b/app/models.py index a88a8dd0..c3f5d776 100644 --- a/app/models.py +++ b/app/models.py @@ -87,6 +87,10 @@ class Instance(db.Model): def __repr__(self): return ''.format(self.domain) + @classmethod + def unique_software_names(cls): + return list(db.session.execute(text('SELECT DISTINCT software FROM instance ORDER BY software')).scalars()) + class InstanceRole(db.Model): instance_id = db.Column(db.Integer, db.ForeignKey('instance.id'), primary_key=True) diff --git a/app/search/routes.py b/app/search/routes.py index 15f96a3d..f770a721 100644 --- a/app/search/routes.py +++ b/app/search/routes.py @@ -3,7 +3,7 @@ from flask_login import login_required, current_user from flask_babel import _ from sqlalchemy import or_, desc -from app.models import Post, Language, Community +from app.models import Post, Language, Community, Instance from app.search import bp from app.utils import moderating_communities, joined_communities, render_template, blocked_domains, blocked_instances, \ communities_banned_from, recently_upvoted_posts, recently_downvoted_posts, blocked_users, menu_topics, \ @@ -16,6 +16,7 @@ from app.activitypub.util import resolve_remote_post_from_search def run_search(): languages = Language.query.order_by(Language.name).all() communities = Community.query.filter(Community.banned == False).order_by(Community.name) + instance_software = Instance.unique_software_names() if current_user.is_authenticated: banned_from = communities_banned_from(current_user.id) communities = communities.filter(Community.id.not_in(banned_from)) @@ -26,6 +27,7 @@ def run_search(): community_id = request.args.get('community', 0, type=int) language_id = request.args.get('language', 0, type=int) type = request.args.get('type', 0, type=int) + software = request.args.get('software', '') low_bandwidth = request.cookies.get('low_bandwidth', '0') == '1' q = request.args.get('q') sort_by = request.args.get('sort_by', '') @@ -68,6 +70,10 @@ def run_search(): posts = posts.filter(Post.community_id == community_id) if language_id: posts = posts.filter(Post.language_id == language_id) + if software: + instances = Instance.query.filter_by(software=software) + instance_ids = [instance.id for instance in instances] + posts = posts.filter(Post.instance_id.in_(instance_ids)) if sort_by == 'date': posts = posts.order_by(desc(Post.posted_at)) elif sort_by == 'top': @@ -100,7 +106,7 @@ def run_search(): else: return render_template('search/start.html', title=_('Search'), communities=communities.all(), - languages=languages, + languages=languages, instance_software=instance_software, moderating_communities=moderating_communities(current_user.get_id()), joined_communities=joined_communities(current_user.get_id()), menu_topics=menu_topics(), diff --git a/app/templates/search/start.html b/app/templates/search/start.html index e4d5ff93..36756bb6 100644 --- a/app/templates/search/start.html +++ b/app/templates/search/start.html @@ -41,6 +41,16 @@ {% endfor %} +
+ +