Store reasons on global domain blocks

merge-requests/30/head
Bob Mottram 2022-11-23 13:57:38 +00:00
parent f2c56d6964
commit 7c561a309d
2 changed files with 89 additions and 12 deletions

View File

@ -39,10 +39,77 @@ from conversation import mute_conversation
from conversation import unmute_conversation
def _add_global_block_reason(base_dir: str,
block_nickname: str, block_domain: str,
reason: str) -> bool:
"""Store a global block reason
"""
if not reason:
return False
blocking_reasons_filename = \
base_dir + '/accounts/blocking_reasons.txt'
if not block_nickname.startswith('#'):
# is the handle already blocked?
block_id = block_nickname + '@' + block_domain
else:
block_id = block_nickname
reason = reason.replace('\n', '').strip()
reason_line = block_id + ' ' + reason + '\n'
if os.path.isfile(blocking_reasons_filename):
if not text_in_file(block_id,
blocking_reasons_filename):
try:
with open(blocking_reasons_filename, 'a+',
encoding='utf-8') as reas_file:
reas_file.write(reason_line)
except OSError:
print('EX: unable to add blocking reason ' +
block_id)
else:
reasons_str = ''
try:
with open(blocking_reasons_filename, 'r',
encoding='utf-8') as reas_file:
reasons_str = reas_file.read()
except OSError:
print('EX: unable to read blocking reasons')
reasons_lines = reasons_str.split('\n')
new_reasons_str = ''
for line in reasons_lines:
if not line.startswith(block_id + ' '):
new_reasons_str += line + '\n'
continue
new_reasons_str = reason_line
try:
with open(blocking_reasons_filename, 'w+',
encoding='utf-8') as reas_file:
reas_file.write(new_reasons_str)
except OSError:
print('EX: unable to save blocking reasons' +
blocking_reasons_filename)
else:
try:
with open(blocking_reasons_filename, 'w+',
encoding='utf-8') as reas_file:
reas_file.write(reason_line)
except OSError:
print('EX: unable to save blocking reason ' +
block_id + ' ' + blocking_reasons_filename)
def add_global_block(base_dir: str,
block_nickname: str, block_domain: str) -> bool:
block_nickname: str, block_domain: str,
reason: str) -> bool:
"""Global block which applies to all accounts
"""
_add_global_block_reason(base_dir,
block_nickname, block_domain,
reason)
blocking_filename = base_dir + '/accounts/blocking.txt'
if not block_nickname.startswith('#'):
# is the handle already blocked?

View File

@ -2611,25 +2611,35 @@ class PubServer(BaseHTTPRequestHandler):
remove_global_filter(base_dir, moderation_text)
if moderation_button == 'block':
full_block_domain = None
if moderation_text.startswith('http') or \
moderation_text.startswith('ipfs') or \
moderation_text.startswith('ipns') or \
moderation_text.startswith('hyper'):
moderation_text = moderation_text.strip()
moderation_reason = None
if ' ' in moderation_text:
moderation_domain = moderation_text.split(' ', 1)[0]
moderation_reason = moderation_text.split(' ', 1)[1]
else:
moderation_domain = moderation_text
if moderation_domain.startswith('http') or \
moderation_domain.startswith('ipfs') or \
moderation_domain.startswith('ipns') or \
moderation_domain.startswith('hyper'):
# https://domain
block_domain, block_port = \
get_domain_from_actor(moderation_text)
get_domain_from_actor(moderation_domain)
full_block_domain = \
get_full_domain(block_domain, block_port)
if '@' in moderation_text:
if '@' in moderation_domain:
# nick@domain or *@domain
full_block_domain = moderation_text.split('@')[1]
full_block_domain = \
moderation_domain.split('@')[1]
else:
# assume the text is a domain name
if not full_block_domain and '.' in moderation_text:
if not full_block_domain and '.' in moderation_domain:
nickname = '*'
full_block_domain = moderation_text.strip()
full_block_domain = \
moderation_domain.strip()
if full_block_domain or nickname.startswith('#'):
add_global_block(base_dir, nickname, full_block_domain)
add_global_block(base_dir, nickname,
full_block_domain, moderation_reason)
if moderation_button == 'unblock':
full_block_domain = None
if moderation_text.startswith('http') or \
@ -18880,7 +18890,7 @@ class PubServer(BaseHTTPRequestHandler):
block_domain = urllib.parse.unquote_plus(block_domain.strip())
if '?' in block_domain:
block_domain = block_domain.split('?')[0]
add_global_block(self.server.base_dir, '*', block_domain)
add_global_block(self.server.base_dir, '*', block_domain, None)
msg = \
html_account_info(self.server.translate,
self.server.base_dir,