Handle existing blocks in a smarter way

This commit is contained in:
projectmoon 2023-12-01 23:27:37 +01:00
parent e3aae6b2a9
commit 5388beeb86
1 changed files with 14 additions and 7 deletions

View File

@ -61,6 +61,18 @@ def sync_blocklists(conf: argparse.Namespace):
export_fields.extend(conf.export_fields)
blocklists = []
# Fetch existing destination blocklists, as Misskey server block
# update is an all at once operation. We cannot update blocks
# individually.
for dest in conf.blocklist_instance_destinations:
host = dest['domain']
token = dest['token']
scheme = dest.get('scheme', 'https')
blocklists.append(
fetch_instance_blocklist(host, token, True, import_fields, scheme)
)
# Fetch blocklists from URLs
if not conf.no_fetch_url:
blocklists.extend(fetch_from_urls(conf.blocklist_url_sources,
@ -762,6 +774,7 @@ def push_blocklist(token: str, host: str, blocklist: list[DomainBlock],
# Force use of the admin API, and add 'id' to the list of fields
if 'id' not in import_fields:
import_fields.append('id')
serverblocks = fetch_instance_blocklist(host, token, True, import_fields, scheme)
# # Convert serverblocks to a dictionary keyed by domain name
@ -774,7 +787,7 @@ def push_blocklist(token: str, host: str, blocklist: list[DomainBlock],
oldblock = serverblocks[newblock.domain]
change_needed = is_change_needed(oldblock, newblock, import_fields)
change_needed = is_change_needed(oldblock, newblock, ["severity"])
# Is the severity changing?
if 'severity' in change_needed:
@ -815,12 +828,6 @@ def push_blocklist(token: str, host: str, blocklist: list[DomainBlock],
# Make sure the new block doesn't clobber a domain with followers
newblock.severity = check_followed_severity(host, token, newblock.domain, newblock.severity, max_followed_severity, scheme)
# Append existing blocks to the new blocks (needed because Misskey
# is all at once update operation)
for existing_block in serverblocks.values():
if existing_block.domain not in blocklist:
blocklist.blocks[existing_block.domain] = existing_block
apply_blocks_misskey(blocklist, host, scheme, token)
def apply_blocks_misskey(blocklist: Blocklist, host:str, scheme: str="https", token: str=None):