From 3cd6a3289e927866a98515d0556227f41232b363 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Tue, 24 Dec 2024 18:17:36 +0000 Subject: [PATCH] Tidying --- blocking.py | 21 +++++++++++++++++++++ webapp_confirm.py | 15 ++++++--------- webapp_create_post.py | 39 ++++++++++++++------------------------- webapp_person_options.py | 19 ++++++------------- webapp_post.py | 21 ++++++--------------- webapp_profile.py | 8 +++++--- 6 files changed, 58 insertions(+), 65 deletions(-) diff --git a/blocking.py b/blocking.py index 84aabfe6a..f1df05efc 100644 --- a/blocking.py +++ b/blocking.py @@ -2283,3 +2283,24 @@ def run_federated_blocks_daemon(base_dir: str, httpd, debug: bool) -> None: httpd.max_api_blocks, httpd.mitm_servers) time.sleep(seconds_per_hour * 6) + + +def sending_is_blocked2(base_dir: str, nickname: str, domain: str, + to_domain: str, to_actor: str) -> bool: + """is sending to the given actor blocked? + """ + if not to_domain: + return False + + send_block_filename = \ + acct_dir(base_dir, nickname, domain) + '/send_blocks.txt' + if not os.path.isfile(send_block_filename): + return False + + send_blocked = False + if text_in_file(to_actor, send_block_filename, False): + send_blocked = True + elif text_in_file('://' + to_domain + '\n', send_block_filename, False): + send_blocked = True + + return send_blocked diff --git a/webapp_confirm.py b/webapp_confirm.py index 4fe506ea9..d528809b8 100644 --- a/webapp_confirm.py +++ b/webapp_confirm.py @@ -19,7 +19,7 @@ from utils import get_config_param from utils import get_alt_path from utils import acct_dir from utils import get_account_timezone -from utils import text_in_file +from blocking import sending_is_blocked2 from webapp_utils import set_custom_background from webapp_utils import html_header_with_external_style from webapp_utils import html_footer @@ -242,14 +242,11 @@ def html_confirm_follow(translate: {}, base_dir: str, 'src="' + follow_profile_url + '"/>\n' follow_actor_nick = get_nickname_from_actor(follow_actor) if follow_actor_nick and follow_domain: - send_block_filename = \ - acct_dir(base_dir, nickname, domain) + '/send_blocks.txt' - if os.path.isfile(send_block_filename): - if text_in_file('://' + follow_domain + '\n', - send_block_filename, False): - follow_str += \ - '

' + \ - translate['FollowWarning'] + '

\n' + if sending_is_blocked2(base_dir, nickname, domain, + follow_domain, follow_actor): + follow_str += \ + '

' + \ + translate['FollowWarning'] + '

\n' follow_str += \ '

' + translate['Follow'] + ' ' + \ follow_actor_nick + '@' + follow_domain + ' ?

\n' diff --git a/webapp_create_post.py b/webapp_create_post.py index 257a39aa3..a1ee43821 100644 --- a/webapp_create_post.py +++ b/webapp_create_post.py @@ -27,9 +27,9 @@ from utils import get_currencies from utils import get_category_types from utils import get_account_timezone from utils import get_supported_languages -from utils import text_in_file from utils import get_attributed_to from utils import get_full_domain +from blocking import sending_is_blocked2 from webapp_utils import open_content_warning from webapp_utils import edit_check_box from webapp_utils import get_buy_links @@ -409,30 +409,19 @@ def html_new_post(edit_post_params: {}, translate['this post'] + '

\n' # is sending posts to this account blocked? - send_block_filename = \ - acct_dir(base_dir, nickname, domain) + \ - '/send_blocks.txt' - if os.path.isfile(send_block_filename): - reply_actor = in_reply_to - reply_nickname = get_nickname_from_actor(in_reply_to) - if reply_nickname: - reply_actor = \ - in_reply_to.split('/' + reply_nickname)[0] + \ - '/' + reply_nickname - if text_in_file(reply_actor, - send_block_filename, False): - new_post_text += \ - '

' + \ - translate['FollowAccountWarning'] + \ - '

\n' - else: - reply_domain, _ = \ - get_domain_from_actor(reply_actor) - if text_in_file('://' + reply_domain + '\n', - send_block_filename, False): - new_post_text += \ - '

' + \ - translate['FollowWarning'] + '

\n' + reply_actor = in_reply_to + reply_nickname = get_nickname_from_actor(in_reply_to) + if reply_nickname: + reply_actor = \ + in_reply_to.split('/' + reply_nickname)[0] + \ + '/' + reply_nickname + reply_domain, _ = get_domain_from_actor(reply_actor) + if sending_is_blocked2(base_dir, nickname, domain, + reply_domain, reply_actor): + new_post_text += \ + '

' + \ + translate['FollowAccountWarning'] + \ + '

\n' if post_json_object: timezone = \ diff --git a/webapp_person_options.py b/webapp_person_options.py index 664aab6a3..79e5de03f 100644 --- a/webapp_person_options.py +++ b/webapp_person_options.py @@ -25,6 +25,7 @@ from utils import acct_dir from utils import text_in_file from utils import remove_domain_port from blocking import is_blocked +from blocking import sending_is_blocked2 from follow import is_follower_of_person from follow import is_following_actor from followingCalendar import receiving_calendar_events @@ -323,19 +324,11 @@ def html_person_options(default_timeline: str, ' @' + handle_shown + mitm_str + '

\n' # is sending posts to this account blocked? - send_block_filename = \ - acct_dir(base_dir, nickname, domain) + '/send_blocks.txt' - if os.path.isfile(send_block_filename): - if text_in_file(options_actor, - send_block_filename, False): - options_str += \ - '

' + \ - translate['FollowAccountWarning'] + '

\n' - elif text_in_file('://' + options_domain + '\n', - send_block_filename, False): - options_str += \ - '

' + \ - translate['FollowWarning'] + '

\n' + if sending_is_blocked2(base_dir, nickname, domain, + options_domain, options_actor): + options_str += \ + '

' + \ + translate['FollowAccountWarning'] + '

\n' if follows_you and authorized: if follow_str != 'Unfollow': diff --git a/webapp_post.py b/webapp_post.py index 26a06536e..f1bee1b15 100644 --- a/webapp_post.py +++ b/webapp_post.py @@ -35,7 +35,6 @@ from flags import is_news_post from flags import is_recent_post from flags import is_chat_message from flags import is_pgp_encrypted -from utils import text_in_file from utils import text_mode_removals from utils import remove_header_tags from utils import get_actor_from_post_id @@ -123,6 +122,7 @@ from speaker import update_speaker from languages import auto_translate_post from cwlists import add_cw_from_lists from blocking import is_blocked +from blocking import sending_is_blocked2 from reaction import html_emoji_reactions from maps import html_open_street_map from maps import set_map_preferences_coords @@ -648,20 +648,11 @@ def _get_reply_icon_html(base_dir: str, nickname: str, domain: str, # Is this domain or account blocking you? reply_to_domain, _ = get_domain_from_actor(reply_to_link) reply_to_actor = get_actor_from_post(post_json_object) - if reply_to_domain: - send_block_filename = \ - acct_dir(base_dir, nickname, domain) + '/send_blocks.txt' - if os.path.isfile(send_block_filename): - reply_blocked = False - if text_in_file(reply_to_actor, send_block_filename, False): - reply_blocked = True - elif text_in_file('://' + reply_to_domain + '\n', - send_block_filename, False): - reply_blocked = True - if reply_blocked: - # You are blocked. Replies are unlikely to be received - # so don't show the reply icon - return '' + if sending_is_blocked2(base_dir, nickname, domain, + reply_to_domain, reply_to_actor): + # You are blocked. Replies are unlikely to be received + # so don't show the reply icon + return '' reply_to_link += page_number_param diff --git a/webapp_profile.py b/webapp_profile.py index 60a81ebe3..fe37938ae 100644 --- a/webapp_profile.py +++ b/webapp_profile.py @@ -114,6 +114,7 @@ from webapp_timeline import page_number_buttons from cwlists import get_cw_list_variable from blocking import get_account_blocks from blocking import is_blocked +from blocking import sending_is_blocked2 from content import remove_link_trackers_from_content from content import bold_reading_string from roles import is_devops @@ -432,10 +433,11 @@ def html_profile_after_search(authorized: bool, repo_url = get_repo_url(profile_json) # is sending posts to this account blocked? - send_block_filename = \ - acct_dir(base_dir, nickname, domain) + '/send_blocks.txt' send_blocks_str = '' - if os.path.isfile(send_block_filename): + if sending_is_blocked2(base_dir, nickname, domain, + search_domain_full, person_url): + send_block_filename = \ + acct_dir(base_dir, nickname, domain) + '/send_blocks.txt' if text_in_file(person_url, send_block_filename, False): send_blocks_str = translate['FollowAccountWarning']