From 5388beeb866a317f867486be670ea2606fff7a2b Mon Sep 17 00:00:00 2001 From: projectmoon Date: Fri, 1 Dec 2023 23:27:37 +0100 Subject: [PATCH] Handle existing blocks in a smarter way --- src/fediblockhole/__init__.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/fediblockhole/__init__.py b/src/fediblockhole/__init__.py index cfbbc91..4a1278b 100755 --- a/src/fediblockhole/__init__.py +++ b/src/fediblockhole/__init__.py @@ -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):