Blocked collection

merge-requests/30/head
Bob Mottram 2023-07-05 12:16:23 +01:00
parent d43027a779
commit f252846f7e
2 changed files with 102 additions and 3 deletions

View File

@ -105,6 +105,60 @@ def get_account_blocks(base_dir: str,
return blocked_accounts_textarea
def blocked_timeline_json(actor: str, page_number: int, items_per_page: int,
base_dir: str,
nickname: str, domain: str) -> {}:
"""Returns blocked collection for an account
https://codeberg.org/fediverse/fep/src/branch/main/fep/c648/fep-c648.md
"""
blocked_accounts_textarea = \
get_account_blocks(base_dir, nickname, domain)
blocked_list = []
if blocked_accounts_textarea:
blocked_list = blocked_accounts_textarea.split('\n')
start_index = (page_number - 1) * items_per_page
if start_index >= len(blocked_list):
start_index = 0
result_json = {
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://purl.archive.org/socialweb/blocked"
],
"id": actor,
"type": "OrderedCollection",
"name": nickname + "'s Blocked Collection",
"orderedItems": []
}
index = start_index
for _ in range(items_per_page):
if index >= len(blocked_list):
break
block_handle = blocked_list[index]
block_reason = ''
if ' - ' in block_handle:
block_reason = block_handle.split(' - ')[1]
block_handle = block_handle.split(' - ')[0]
block_type = "Person"
if block_handle.startswith('*@'):
block_type = "Application"
block_handle = block_handle.split('*@', 1)[1]
block_json = {
"type": "Block",
"id": actor + '/' + str(index),
"object": {
"type": block_type,
"id": block_handle
}
}
if block_reason:
block_json["object"]["name"] = block_reason
result_json["orderedItems"].append(block_json)
index += 1
return result_json
def add_account_blocks(base_dir: str,
nickname: str, domain: str,
blocked_accounts_textarea: str) -> bool:

View File

@ -153,6 +153,7 @@ from media import path_is_transcript
from media import path_is_audio
from cwlists import get_cw_list_variable
from cwlists import load_cw_lists
from blocking import blocked_timeline_json
from blocking import import_blocking_file
from blocking import export_blocking_file
from blocking import add_account_blocks
@ -17277,9 +17278,6 @@ class PubServer(BaseHTTPRequestHandler):
domain_full = self.server.domain_full
http_prefix = self.server.http_prefix
nickname = self.path.split('/users/')[1]
if '/' in nickname:
nickname = nickname.split('/')[0]
if self.server.debug:
print('Offers collection for account: ' + nickname)
base_dir = self.server.base_dir
@ -17312,6 +17310,53 @@ class PubServer(BaseHTTPRequestHandler):
self._write(msg)
return
if self.path.startswith('/users/') and '/blocked' in self.path:
blocked_collection_authorized = authorized
nickname = self.path.split('/users/')[1]
if '/' in nickname:
nickname = nickname.split('/')[0]
page_number = 1
if '?page=' in self.path:
page_number_str = self.path.split('?page=')[1]
if ';' in page_number_str:
page_number_str = page_number_str.split(';')[0]
if page_number_str.isdigit():
page_number = int(page_number_str)
# show blocked collection for the nickname
blocked_json = []
if self._has_accept(calling_domain) and \
blocked_collection_authorized:
if self.server.debug:
print('Preparing blocked collection')
domain_full = self.server.domain_full
http_prefix = self.server.http_prefix
if self.server.debug:
print('Blocked collection for account: ' + nickname)
base_dir = self.server.base_dir
blocked_items_per_page = 12
actor = \
local_actor_url(http_prefix, nickname, domain_full) + \
'/blocked'
blocked_json = \
blocked_timeline_json(actor, page_number,
blocked_items_per_page, base_dir,
nickname, self.server.domain)
msg_str = json.dumps(blocked_json,
ensure_ascii=False)
msg_str = self._convert_domains(calling_domain,
referer_domain,
msg_str)
msg = msg_str.encode('utf-8')
msglen = len(msg)
accept_str = self.headers['Accept']
protocol_str = \
get_json_content_from_accept(accept_str)
self._set_headers(protocol_str, msglen,
None, calling_domain, False)
self._write(msg)
return
# wanted items collection for this instance
# this is only accessible to instance members or to
# other instances which present an authorization token