From 40de319b9a9d658d234413ea73b7a278b3b637c5 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Sat, 24 Feb 2024 11:07:06 +1300 Subject: [PATCH] allow comments on posts in communities that are restricted to mods --- app/activitypub/routes.py | 21 ++++++++++++++++----- app/activitypub/util.py | 1 + app/community/routes.py | 6 +++--- app/post/routes.py | 2 +- app/utils.py | 34 +++++++++++++++++----------------- profile_app.py | 4 ++-- pyfedi.py | 4 ++-- 7 files changed, 42 insertions(+), 30 deletions(-) diff --git a/app/activitypub/routes.py b/app/activitypub/routes.py index c0981c23..37a614f7 100644 --- a/app/activitypub/routes.py +++ b/app/activitypub/routes.py @@ -21,7 +21,7 @@ from app.activitypub.util import public_key, users_total, active_half_year, acti update_post_from_activity, undo_vote, undo_downvote from app.utils import gibberish, get_setting, is_image_url, allowlist_html, html_to_markdown, render_template, \ domain_from_url, markdown_to_html, community_membership, ap_datetime, markdown_to_text, ip_address, can_downvote, \ - can_upvote, can_create, awaken_dormant_instance, shorten_string + can_upvote, can_create_post, awaken_dormant_instance, shorten_string, can_create_post_reply import werkzeug.exceptions @@ -441,9 +441,15 @@ def process_inbox_request(request_json, activitypublog_id, ip_address): if object_type in new_content_types: # create a new post in_reply_to = request_json['object']['inReplyTo'] if 'inReplyTo' in request_json['object'] else None if not in_reply_to: - post = create_post(activity_log, community, request_json, user) + if can_create_post(user, community): + post = create_post(activity_log, community, request_json, user) + else: + post = None else: - post = create_post_reply(activity_log, community, in_reply_to, request_json, user) + if can_create_post_reply(user, community): + post = create_post_reply(activity_log, community, in_reply_to, request_json, user) + else: + post = None else: activity_log.exception_message = 'Unacceptable type (create): ' + object_type else: @@ -476,11 +482,16 @@ def process_inbox_request(request_json, activitypublog_id, ip_address): if object_type in new_content_types: # create a new post in_reply_to = request_json['object']['object']['inReplyTo'] if 'inReplyTo' in \ request_json['object']['object'] else None - if can_create(user, community): - if not in_reply_to: + if not in_reply_to: + if can_create_post(user, community): post = create_post(activity_log, community, request_json['object'], user, announce_id=request_json['id']) else: + post = None + else: + if can_create_post_reply(user, community): post = create_post_reply(activity_log, community, in_reply_to, request_json['object'], user, announce_id=request_json['id']) + else: + post = None else: activity_log.exception_message = 'Unacceptable type: ' + object_type else: diff --git a/app/activitypub/util.py b/app/activitypub/util.py index 173df0db..e25a1b67 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -1057,6 +1057,7 @@ def create_post_reply(activity_log: ActivityPubLog, community: Community, in_rep else: activity_log.exception_message = 'Comments disabled, reply discarded' activity_log.result = 'ignored' + return None return post else: activity_log.exception_message = 'Could not find parent post' diff --git a/app/community/routes.py b/app/community/routes.py index 48ca6196..d52576c6 100644 --- a/app/community/routes.py +++ b/app/community/routes.py @@ -21,7 +21,7 @@ from app.models import User, Community, CommunityMember, CommunityJoinRequest, C from app.community import bp from app.utils import get_setting, render_template, allowlist_html, markdown_to_html, validation_required, \ shorten_string, gibberish, community_membership, ap_datetime, \ - request_etag_matches, return_304, instance_banned, can_create, can_upvote, can_downvote, user_filters_posts, \ + request_etag_matches, return_304, instance_banned, can_create_post, can_upvote, can_downvote, user_filters_posts, \ joined_communities, moderating_communities, blocked_domains from feedgen.feed import FeedGenerator from datetime import timezone, timedelta @@ -403,12 +403,12 @@ def add_post(actor): form.communities.choices = [(c.id, c.display_name()) for c in current_user.communities()] - if not can_create(current_user, community): + if not can_create_post(current_user, community): abort(401) if form.validate_on_submit(): community = Community.query.get_or_404(form.communities.data) - if not can_create(current_user, community): + if not can_create_post(current_user, community): abort(401) post = Post(user_id=current_user.id, community_id=form.communities.data, instance_id=1) save_post(form, post) diff --git a/app/post/routes.py b/app/post/routes.py index f5c1e014..16f3ebdc 100644 --- a/app/post/routes.py +++ b/app/post/routes.py @@ -385,7 +385,7 @@ def add_reply(post_id: int, comment_id: int): post = Post.query.get_or_404(post_id) if not post.comments_enabled: - flash('The author of the post has changed their mind so comments have been disabled.', 'warning') + flash('Comments have been disabled.', 'warning') return redirect(url_for('activitypub.post_ap', post_id=post_id)) in_reply_to = PostReply.query.get_or_404(comment_id) diff --git a/app/utils.py b/app/utils.py index cb870356..6873b30a 100644 --- a/app/utils.py +++ b/app/utils.py @@ -431,31 +431,31 @@ def can_upvote(user, community: Community) -> bool: return True -def can_create(user, content: Union[Community, Post, PostReply]) -> bool: +def can_create_post(user, content: Community) -> bool: if user is None or content is None or user.banned: return False - if isinstance(content, Community): - if content.is_moderator(user) or user.is_admin(): - return True + if content.is_moderator(user) or user.is_admin(): + return True - if content.restricted_to_mods: - return False + if content.restricted_to_mods: + return False - if content.local_only and not user.is_local(): - return False - else: - if content.community.is_moderator(user) or user.is_admin(): - return True + if content.local_only and not user.is_local(): + return False - if content.community.restricted_to_mods and isinstance(content, Post): - return False + return True - if content.community.local_only and not user.is_local(): - return False - if isinstance(content, PostReply) and content.post.comments_enabled is False: - return False +def can_create_post_reply(user, content: Community) -> bool: + if user is None or content is None or user.banned: + return False + + if content.is_moderator(user) or user.is_admin(): + return True + + if content.local_only and not user.is_local(): + return False return True diff --git a/profile_app.py b/profile_app.py index d1557e01..50fb1cf1 100644 --- a/profile_app.py +++ b/profile_app.py @@ -7,7 +7,7 @@ from werkzeug.middleware.profiler import ProfilerMiddleware from app import create_app, db, cli from app.models import Site from app.utils import gibberish, shorten_number, community_membership, getmtime, digits, user_access, ap_datetime, \ - can_create, can_upvote, can_downvote, current_theme, shorten_string, shorten_url + can_create_post, can_upvote, can_downvote, current_theme, shorten_string, shorten_url app = create_app() @@ -21,7 +21,7 @@ with app.app_context(): app.jinja_env.globals['json_loads'] = json.loads app.jinja_env.globals['user_access'] = user_access app.jinja_env.globals['ap_datetime'] = ap_datetime - app.jinja_env.globals['can_create'] = can_create + app.jinja_env.globals['can_create'] = can_create_post app.jinja_env.globals['can_upvote'] = can_upvote app.jinja_env.globals['can_downvote'] = can_downvote app.jinja_env.globals['theme'] = current_theme diff --git a/pyfedi.py b/pyfedi.py index a44d2c56..040d22c2 100644 --- a/pyfedi.py +++ b/pyfedi.py @@ -11,7 +11,7 @@ from flask import session, g, json, request, current_app from app.constants import POST_TYPE_LINK, POST_TYPE_IMAGE, POST_TYPE_ARTICLE from app.models import Site from app.utils import getmtime, gibberish, shorten_string, shorten_url, digits, user_access, community_membership, \ - can_create, can_upvote, can_downvote, shorten_number, ap_datetime, current_theme + can_create_post, can_upvote, can_downvote, shorten_number, ap_datetime, current_theme app = create_app() cli.register(app) @@ -39,7 +39,7 @@ with app.app_context(): app.jinja_env.globals['json_loads'] = json.loads app.jinja_env.globals['user_access'] = user_access app.jinja_env.globals['ap_datetime'] = ap_datetime - app.jinja_env.globals['can_create'] = can_create + app.jinja_env.globals['can_create'] = can_create_post app.jinja_env.globals['can_upvote'] = can_upvote app.jinja_env.globals['can_downvote'] = can_downvote app.jinja_env.globals['theme'] = current_theme