From 8a008e3a8a0658bc4071124407782f166d9bc002 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Fri, 25 Oct 2024 13:37:57 +1300 Subject: [PATCH] instance block on instance profile --- app/instance/routes.py | 36 +++++++++++++++++++++++++--- app/templates/instance/overview.html | 13 ++++++---- app/templates/user/filters.html | 2 +- app/templates/user/show_profile.html | 2 +- app/user/routes.py | 15 ------------ 5 files changed, 43 insertions(+), 25 deletions(-) diff --git a/app/instance/routes.py b/app/instance/routes.py index 8d8a64a9..d9d221c8 100644 --- a/app/instance/routes.py +++ b/app/instance/routes.py @@ -1,12 +1,13 @@ from collections import namedtuple -from flask import request, url_for, g, abort -from flask_login import current_user +from flask import request, url_for, g, abort, flash, redirect +from flask_login import current_user, login_required from flask_babel import _ from sqlalchemy import or_, desc +from app import db, cache from app.instance import bp -from app.models import Instance, User, Post, read_posts +from app.models import Instance, User, Post, read_posts, InstanceBlock from app.utils import render_template, moderating_communities, joined_communities, menu_topics, blocked_domains, \ blocked_instances, blocked_communities, blocked_users, user_filters_home, recently_upvoted_posts, \ recently_downvoted_posts @@ -179,3 +180,32 @@ def instance_posts(instance_domain): joined_communities=joined_communities(current_user.get_id()), menu_topics=menu_topics(), site=g.site,) + +@bp.route('/instance//block', methods=['GET']) +@login_required +def instance_block(instance_id): + instance = Instance.query.get_or_404(instance_id) + existing_block = InstanceBlock.query.filter_by(user_id=current_user.id, instance_id=instance.id).first() + if not existing_block: + db.session.add(InstanceBlock(user_id=current_user.id, instance_id=instance.id)) + db.session.commit() + cache.delete_memoized(blocked_instances, current_user.id) + flash(f'{instance.domain} has been blocked.') + + goto = request.args.get('redirect') if 'redirect' in request.args else url_for('user.user_settings_filters') + return redirect(goto) + + +@bp.route('/instance//unblock', methods=['GET']) +@login_required +def instance_unblock(instance_id): + instance = Instance.query.get_or_404(instance_id) + existing_block = InstanceBlock.query.filter_by(user_id=current_user.id, instance_id=instance.id).first() + if existing_block: + db.session.delete(existing_block) + db.session.commit() + cache.delete_memoized(blocked_instances, current_user.id) + flash(f'{instance.domain} has been unblocked.') + + goto = request.args.get('redirect') if 'redirect' in request.args else url_for('user.user_settings_filters') + return redirect(goto) diff --git a/app/templates/instance/overview.html b/app/templates/instance/overview.html index c1ba766c..1f1d3482 100644 --- a/app/templates/instance/overview.html +++ b/app/templates/instance/overview.html @@ -19,17 +19,20 @@

{{ _('This is a %(software)s instance.', software=instance.software) }}

{{ _('People') }}
{{ _('Posts') }}

-

{{ _('Go to instance') }}

+

{{ _('Go to instance') }}

diff --git a/app/templates/user/filters.html b/app/templates/user/filters.html index ce9381dd..4a1c5caf 100644 --- a/app/templates/user/filters.html +++ b/app/templates/user/filters.html @@ -142,7 +142,7 @@ {% for instance in blocked_instances %} {{ instance.domain }} - {{ _('Unblock') }} + {{ _('Unblock') }} {% endfor %} diff --git a/app/templates/user/show_profile.html b/app/templates/user/show_profile.html index 3210316d..877e8b98 100644 --- a/app/templates/user/show_profile.html +++ b/app/templates/user/show_profile.html @@ -94,7 +94,7 @@ {% endif -%} {% if not user.is_local() -%} {% if current_user.has_blocked_instance(user.instance_id) -%} - {{ _('Unblock %(instance_name)s', instance_name=user.ap_domain) }} + {{ _('Unblock %(instance_name)s', instance_name=user.ap_domain) }} {% else %} {{ _('Block everyone from %(instance_name)s', instance_name=user.ap_domain) }} {% endif -%} diff --git a/app/user/routes.py b/app/user/routes.py index 86a14e33..2be82cf8 100644 --- a/app/user/routes.py +++ b/app/user/routes.py @@ -706,21 +706,6 @@ def delete_profile(actor): return redirect(goto) -@bp.route('/instance//unblock', methods=['GET']) -@login_required -def instance_unblock(instance_id): - instance = Instance.query.get_or_404(instance_id) - existing_block = InstanceBlock.query.filter_by(user_id=current_user.id, instance_id=instance.id).first() - if existing_block: - db.session.delete(existing_block) - db.session.commit() - cache.delete_memoized(blocked_instances, current_user.id) - flash(f'{instance.domain} has been unblocked.') - - goto = request.args.get('redirect') if 'redirect' in request.args else url_for('user.user_settings_filters') - return redirect(goto) - - @bp.route('/user/community//unblock', methods=['GET']) @login_required def user_community_unblock(community_id):