Move post function to posts module

main
Bob Mottram 2024-08-31 16:31:40 +01:00
parent 530158ff6a
commit 19a374516d
3 changed files with 57 additions and 56 deletions

View File

@ -120,6 +120,7 @@ from utils import is_reply
from utils import has_actor
from utils import valid_content_warning
from httpsig import message_content_digest
from posts import json_post_allows_comments
from posts import outbox_message_create_wrap
from posts import convert_post_content_to_html
from posts import edited_post_filename
@ -3312,61 +3313,6 @@ def _receive_undo_announce(recent_posts_cache: {},
return True
def json_post_allows_comments(post_json_object: {}) -> bool:
"""Returns true if the given post allows comments/replies
"""
# reply control with
# https://codeberg.org/fediverse/fep/src/branch/main/fep/5624/fep-5624.md
reply_control = None
if 'canReply' in post_json_object:
reply_control = post_json_object['canReply']
if 'capabilities' in post_json_object:
if isinstance(post_json_object['capabilities'], dict):
if 'reply' in post_json_object['capabilities']:
if isinstance(post_json_object['capabilities']['reply'], str):
reply_control = post_json_object['capabilities']['reply']
else:
# capabilities exist but there is no reply field
reply_control = 'noreply'
obj_dict_exists = False
if has_object_dict(post_json_object):
obj_dict_exists = True
post_obj = post_json_object['object']
if 'canReply' in post_obj:
reply_control = post_obj['canReply']
if 'capabilities' in post_obj:
if isinstance(post_obj['capabilities'], dict):
if 'reply' in post_obj['capabilities']:
if isinstance(post_obj['capabilities']['reply'], str):
reply_control = post_obj['capabilities']['reply']
else:
# capabilities exist but there is no reply field
reply_control = 'noreply'
if reply_control:
if isinstance(reply_control, str):
if reply_control == 'noreply':
return False
if not reply_control.endswith('#Public'):
# TODO handle non-public reply permissions
print('CAPABILITIES: replies ' + str(reply_control))
return False
if 'commentsEnabled' in post_json_object:
return post_json_object['commentsEnabled']
if 'rejectReplies' in post_json_object:
return not post_json_object['rejectReplies']
if post_json_object.get('object'):
if not obj_dict_exists:
return False
if 'commentsEnabled' in post_json_object['object']:
return post_json_object['object']['commentsEnabled']
if 'rejectReplies' in post_json_object['object']:
return not post_json_object['object']['rejectReplies']
return True
def _post_allow_comments(post_filename: str) -> bool:
"""Returns true if the given post allows comments/replies
"""

View File

@ -6985,3 +6985,58 @@ def set_max_profile_posts(base_dir: str, nickname: str, domain: str,
max_posts_filename)
return False
return True
def json_post_allows_comments(post_json_object: {}) -> bool:
"""Returns true if the given post allows comments/replies
"""
# reply control with
# https://codeberg.org/fediverse/fep/src/branch/main/fep/5624/fep-5624.md
reply_control = None
if 'canReply' in post_json_object:
reply_control = post_json_object['canReply']
if 'capabilities' in post_json_object:
if isinstance(post_json_object['capabilities'], dict):
if 'reply' in post_json_object['capabilities']:
if isinstance(post_json_object['capabilities']['reply'], str):
reply_control = post_json_object['capabilities']['reply']
else:
# capabilities exist but there is no reply field
reply_control = 'noreply'
obj_dict_exists = False
if has_object_dict(post_json_object):
obj_dict_exists = True
post_obj = post_json_object['object']
if 'canReply' in post_obj:
reply_control = post_obj['canReply']
if 'capabilities' in post_obj:
if isinstance(post_obj['capabilities'], dict):
if 'reply' in post_obj['capabilities']:
if isinstance(post_obj['capabilities']['reply'], str):
reply_control = post_obj['capabilities']['reply']
else:
# capabilities exist but there is no reply field
reply_control = 'noreply'
if reply_control:
if isinstance(reply_control, str):
if reply_control == 'noreply':
return False
if not reply_control.endswith('#Public'):
# TODO handle non-public reply permissions
print('CAPABILITIES: replies ' + str(reply_control))
return False
if 'commentsEnabled' in post_json_object:
return post_json_object['commentsEnabled']
if 'rejectReplies' in post_json_object:
return not post_json_object['rejectReplies']
if post_json_object.get('object'):
if not obj_dict_exists:
return False
if 'commentsEnabled' in post_json_object['object']:
return post_json_object['object']['commentsEnabled']
if 'rejectReplies' in post_json_object['object']:
return not post_json_object['object']['rejectReplies']
return True

View File

@ -38,6 +38,7 @@ from daemon import run_daemon
from session import get_json_valid
from session import create_session
from session import get_json
from posts import json_post_allows_comments
from posts import convert_post_content_to_html
from posts import get_actor_from_in_reply_to
from posts import regenerate_index_for_box
@ -147,7 +148,6 @@ from media import get_image_dimensions
from media import get_media_path
from media import get_attachment_media_type
from delete import send_delete_via_server
from inbox import json_post_allows_comments
from inbox import valid_inbox
from inbox import valid_inbox_filenames
from categories import guess_hashtag_category