diff --git a/blocking.py b/blocking.py index b5290af2a..8988290fe 100644 --- a/blocking.py +++ b/blocking.py @@ -39,6 +39,64 @@ from conversation import mute_conversation from conversation import unmute_conversation +def add_account_blocks(base_dir: str, + nickname: str, domain: str, + blocked_accounts_textarea: str) -> bool: + """Update the blockfile for an account after editing their + profile and changing "blocked accounts" + """ + if blocked_accounts_textarea is None: + return False + blocklist = blocked_accounts_textarea.split('\n') + blocking_file_text = '' + blocking_reasons_file_text = '' + for line in blocklist: + line = line.strip() + reason = None + if ' ' in line: + block_id = line.split(' ', 1)[0] + reason = line.split(' ', 1)[1] + blocking_reasons_file_text += block_id + ' ' + reason + '\n' + else: + block_id = line + blocking_file_text += block_id + '\n' + + account_directory = acct_dir(base_dir, nickname, domain) + blocking_filename = \ + account_directory + '/blocking.txt' + blocking_reasons_filename = \ + account_directory + '/blocking_reasons.txt' + + if not blocking_file_text: + if os.path.isfile(blocking_filename): + try: + os.remove(blocking_filename) + except OSError: + print('EX: _profile_edit unable to delete blocking ' + + blocking_filename) + if os.path.isfile(blocking_reasons_filename): + try: + os.remove(blocking_reasons_filename) + except OSError: + print('EX: _profile_edit unable to delete blocking reasons' + + blocking_reasons_filename) + return True + + try: + with open(blocking_filename, 'w+', encoding='utf-8') as fp_block: + fp_block.write(blocking_file_text) + except OSError: + print('EX: Failed to write ' + blocking_filename) + + try: + with open(blocking_reasons_filename, 'w+', + encoding='utf-8') as fp_block: + fp_block.write(blocking_reasons_file_text) + except OSError: + print('EX: Failed to write ' + blocking_reasons_filename) + return True + + def _add_global_block_reason(base_dir: str, block_nickname: str, block_domain: str, reason: str) -> bool: diff --git a/daemon.py b/daemon.py index 40972fbff..bb57eb85c 100644 --- a/daemon.py +++ b/daemon.py @@ -142,6 +142,7 @@ from media import replace_twitter from media import attach_media from media import path_is_video from media import path_is_audio +from blocking import add_account_blocks from blocking import get_cw_list_variable from blocking import load_cw_lists from blocking import update_blocked_cache @@ -7413,25 +7414,13 @@ class PubServer(BaseHTTPRequestHandler): auto_cw_filename) # save blocked accounts list - blocked_filename = \ - acct_dir(base_dir, nickname, domain) + \ - '/blocking.txt' if fields.get('blocked'): - try: - with open(blocked_filename, 'w+', - encoding='utf-8') as blockedfile: - blockedfile.write(fields['blocked']) - except OSError: - print('EX: unable to write blocked accounts ' + - blocked_filename) + add_account_blocks(base_dir, + nickname, domain, + fields['blocked']) else: - if os.path.isfile(blocked_filename): - try: - os.remove(blocked_filename) - except OSError: - print('EX: _profile_edit ' + - 'unable to delete ' + - blocked_filename) + add_account_blocks(base_dir, + nickname, domain, '') # Save DM allowed instances list. # The allow list for incoming DMs,