diff --git a/blocking.py b/blocking.py index 8988290fe..55defbdf7 100644 --- a/blocking.py +++ b/blocking.py @@ -39,6 +39,70 @@ from conversation import mute_conversation from conversation import unmute_conversation +def get_global_block_reason(search_text: str, + blocking_reasons_filename: str) -> str: + """Returns the reason why a domain was globally blocked + """ + if not text_in_file(search_text, blocking_reasons_filename): + return '' + + reasons_str = '' + try: + with open(blocking_reasons_filename, 'r', + encoding='utf-8') as fp_reas: + reasons_str = fp_reas.read() + except OSError: + print('WARN: Failed to raed blocking reasons ' + + blocking_reasons_filename) + if not reasons_str: + return '' + + reasons_lines = reasons_str.split('\n') + for line in reasons_lines: + if line.startswith(search_text): + if ' ' in line: + return line.split(' ', 1)[1] + return '' + + +def get_account_blocks(base_dir: str, + nickname: str, domain: str) -> str: + """Returne the text for the textarea for "blocked accounts" + when editing profile + """ + account_directory = acct_dir(base_dir, nickname, domain) + blocking_filename = \ + account_directory + '/blocking.txt' + blocking_reasons_filename = \ + account_directory + '/blocking_reasons.txt' + + if not os.path.isfile(blocking_filename): + return '' + + blocked_accounts_textarea = '' + blocking_file_text = '' + try: + with open(blocking_filename, 'r', encoding='utf-8') as fp_block: + blocking_file_text = fp_block.read() + except OSError: + print('EX: Failed to read ' + blocking_filename) + return '' + + blocklist = blocking_file_text.split('\n') + for handle in blocklist: + handle = handle.strip() + reason = \ + get_global_block_reason(handle, + blocking_reasons_filename) + if reason: + blocked_accounts_textarea += \ + handle + ' - ' + reason + '\n' + continue + blocked_accounts_textarea += handle + '\n' + + return blocked_accounts_textarea + + def add_account_blocks(base_dir: str, nickname: str, domain: str, blocked_accounts_textarea: str) -> bool: @@ -53,7 +117,11 @@ def add_account_blocks(base_dir: str, for line in blocklist: line = line.strip() reason = None - if ' ' in line: + if ' - ' in line: + block_id = line.split(' - ', 1)[0] + reason = line.split(' - ', 1)[1] + blocking_reasons_file_text += block_id + ' ' + reason + '\n' + elif ' ' in line: block_id = line.split(' ', 1)[0] reason = line.split(' ', 1)[1] blocking_reasons_file_text += block_id + ' ' + reason + '\n' diff --git a/webapp_moderation.py b/webapp_moderation.py index 9fccf63a1..cd6f35d50 100644 --- a/webapp_moderation.py +++ b/webapp_moderation.py @@ -8,7 +8,6 @@ __status__ = "Production" __module_group__ = "Moderation" import os -from utils import text_in_file from utils import is_artist from utils import is_account_dir from utils import get_full_domain @@ -28,6 +27,7 @@ from webapp_utils import get_banner_file from webapp_utils import get_content_warning_button from webapp_utils import html_header_with_external_style from webapp_utils import html_footer +from blocking import get_global_block_reason from blocking import is_blocked_domain from blocking import is_blocked from session import create_session @@ -298,32 +298,6 @@ def html_account_info(translate: {}, return info_form -def _get_global_block_reason(search_text: str, - blocking_reasons_filename: str) -> str: - """Returns the reason why a domain was globally blocked - """ - if not text_in_file(search_text, blocking_reasons_filename): - return '' - - reasons_str = '' - try: - with open(blocking_reasons_filename, 'r', - encoding='utf-8') as fp_reas: - reasons_str = fp_reas.read() - except OSError: - print('WARN: Failed to raed blocking reasons ' + - blocking_reasons_filename) - if not reasons_str: - return '' - - reasons_lines = reasons_str.split('\n') - for line in reasons_lines: - if line.startswith(search_text): - if ' ' in line: - return line.split(' ', 1)[1] - return '' - - def html_moderation_info(translate: {}, base_dir: str, nickname: str, domain: str, theme: str, access_keys: {}) -> str: @@ -457,8 +431,8 @@ def html_moderation_info(translate: {}, base_dir: str, line = remove_eol(line).strip() if blocking_reasons_exist: reason = \ - _get_global_block_reason(line, - blocking_reasons_filename) + get_global_block_reason(line, + blocking_reasons_filename) if reason: blocked_str += \ line + ' - ' + reason + '\n' diff --git a/webapp_profile.py b/webapp_profile.py index 5a4167147..923c1e853 100644 --- a/webapp_profile.py +++ b/webapp_profile.py @@ -80,6 +80,7 @@ from blog import get_blog_address from webapp_post import individual_post_as_html from webapp_timeline import html_individual_share from webapp_timeline import page_number_buttons +from blocking import get_account_blocks from blocking import get_cw_list_variable from blocking import is_blocked from content import bold_reading_string @@ -1787,12 +1788,7 @@ def _html_edit_profile_filtering(base_dir: str, nickname: str, domain: str, with open(auto_cw_filename, 'r', encoding='utf-8') as cw_file: auto_cw = cw_file.read() - blocked_str = '' - blocked_filename = \ - acct_dir(base_dir, nickname, domain) + '/blocking.txt' - if os.path.isfile(blocked_filename): - with open(blocked_filename, 'r', encoding='utf-8') as blockedfile: - blocked_str = blockedfile.read() + blocked_str = get_account_blocks(base_dir, nickname, domain) dm_allowed_instances_str = '' dm_allowed_instances_filename = \