allow comments on posts in communities that are restricted to mods

This commit is contained in:
rimu 2024-02-24 11:07:06 +13:00
parent 93c52b2a86
commit 40de319b9a
7 changed files with 42 additions and 30 deletions

View file

@ -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 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, \ 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, \ 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 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 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 in_reply_to = request_json['object']['inReplyTo'] if 'inReplyTo' in request_json['object'] else None
if not in_reply_to: 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: 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: else:
activity_log.exception_message = 'Unacceptable type (create): ' + object_type activity_log.exception_message = 'Unacceptable type (create): ' + object_type
else: 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 if object_type in new_content_types: # create a new post
in_reply_to = request_json['object']['object']['inReplyTo'] if 'inReplyTo' in \ in_reply_to = request_json['object']['object']['inReplyTo'] if 'inReplyTo' in \
request_json['object']['object'] else None 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']) post = create_post(activity_log, community, request_json['object'], user, announce_id=request_json['id'])
else: 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']) post = create_post_reply(activity_log, community, in_reply_to, request_json['object'], user, announce_id=request_json['id'])
else:
post = None
else: else:
activity_log.exception_message = 'Unacceptable type: ' + object_type activity_log.exception_message = 'Unacceptable type: ' + object_type
else: else:

View file

@ -1057,6 +1057,7 @@ def create_post_reply(activity_log: ActivityPubLog, community: Community, in_rep
else: else:
activity_log.exception_message = 'Comments disabled, reply discarded' activity_log.exception_message = 'Comments disabled, reply discarded'
activity_log.result = 'ignored' activity_log.result = 'ignored'
return None
return post return post
else: else:
activity_log.exception_message = 'Could not find parent post' activity_log.exception_message = 'Could not find parent post'

View file

@ -21,7 +21,7 @@ from app.models import User, Community, CommunityMember, CommunityJoinRequest, C
from app.community import bp from app.community import bp
from app.utils import get_setting, render_template, allowlist_html, markdown_to_html, validation_required, \ from app.utils import get_setting, render_template, allowlist_html, markdown_to_html, validation_required, \
shorten_string, gibberish, community_membership, ap_datetime, \ 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 joined_communities, moderating_communities, blocked_domains
from feedgen.feed import FeedGenerator from feedgen.feed import FeedGenerator
from datetime import timezone, timedelta 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()] 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) abort(401)
if form.validate_on_submit(): if form.validate_on_submit():
community = Community.query.get_or_404(form.communities.data) 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) abort(401)
post = Post(user_id=current_user.id, community_id=form.communities.data, instance_id=1) post = Post(user_id=current_user.id, community_id=form.communities.data, instance_id=1)
save_post(form, post) save_post(form, post)

View file

@ -385,7 +385,7 @@ def add_reply(post_id: int, comment_id: int):
post = Post.query.get_or_404(post_id) post = Post.query.get_or_404(post_id)
if not post.comments_enabled: 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)) return redirect(url_for('activitypub.post_ap', post_id=post_id))
in_reply_to = PostReply.query.get_or_404(comment_id) in_reply_to = PostReply.query.get_or_404(comment_id)

View file

@ -431,31 +431,31 @@ def can_upvote(user, community: Community) -> bool:
return True 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: if user is None or content is None or user.banned:
return False return False
if isinstance(content, Community): if content.is_moderator(user) or user.is_admin():
if content.is_moderator(user) or user.is_admin(): return True
return True
if content.restricted_to_mods: if content.restricted_to_mods:
return False return False
if content.local_only and not user.is_local(): if content.local_only and not user.is_local():
return False return False
else:
if content.community.is_moderator(user) or user.is_admin():
return True
if content.community.restricted_to_mods and isinstance(content, Post): return True
return False
if content.community.local_only and not user.is_local():
return False
if isinstance(content, PostReply) and content.post.comments_enabled is False: def can_create_post_reply(user, content: Community) -> bool:
return False 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 return True

View file

@ -7,7 +7,7 @@ from werkzeug.middleware.profiler import ProfilerMiddleware
from app import create_app, db, cli from app import create_app, db, cli
from app.models import Site from app.models import Site
from app.utils import gibberish, shorten_number, community_membership, getmtime, digits, user_access, ap_datetime, \ 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() app = create_app()
@ -21,7 +21,7 @@ with app.app_context():
app.jinja_env.globals['json_loads'] = json.loads app.jinja_env.globals['json_loads'] = json.loads
app.jinja_env.globals['user_access'] = user_access app.jinja_env.globals['user_access'] = user_access
app.jinja_env.globals['ap_datetime'] = ap_datetime 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_upvote'] = can_upvote
app.jinja_env.globals['can_downvote'] = can_downvote app.jinja_env.globals['can_downvote'] = can_downvote
app.jinja_env.globals['theme'] = current_theme app.jinja_env.globals['theme'] = current_theme

View file

@ -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.constants import POST_TYPE_LINK, POST_TYPE_IMAGE, POST_TYPE_ARTICLE
from app.models import Site from app.models import Site
from app.utils import getmtime, gibberish, shorten_string, shorten_url, digits, user_access, community_membership, \ 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() app = create_app()
cli.register(app) cli.register(app)
@ -39,7 +39,7 @@ with app.app_context():
app.jinja_env.globals['json_loads'] = json.loads app.jinja_env.globals['json_loads'] = json.loads
app.jinja_env.globals['user_access'] = user_access app.jinja_env.globals['user_access'] = user_access
app.jinja_env.globals['ap_datetime'] = ap_datetime 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_upvote'] = can_upvote
app.jinja_env.globals['can_downvote'] = can_downvote app.jinja_env.globals['can_downvote'] = can_downvote
app.jinja_env.globals['theme'] = current_theme app.jinja_env.globals['theme'] = current_theme