From 0c8c2bab8135e28c6afcd718baae371fe8a8e1fb Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Thu, 16 Mar 2023 15:46:48 +0000 Subject: [PATCH] Tidying --- daemon.py | 28 ++++++++++------------------ follow.py | 45 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 49 insertions(+), 24 deletions(-) diff --git a/daemon.py b/daemon.py index 48917cd17..ad6b32c59 100644 --- a/daemon.py +++ b/daemon.py @@ -119,8 +119,7 @@ from inbox import run_inbox_queue_watchdog from inbox import save_post_to_inbox_queue from inbox import populate_replies from inbox import receive_edit_to_post -from follow import get_followers_sync_json -from follow import get_followers_sync_hash +from follow import update_followers_sync_cache from follow import follower_approval_active from follow import is_following_actor from follow import get_following_feed @@ -16905,6 +16904,8 @@ class PubServer(BaseHTTPRequestHandler): self.server.debug) # followers synchronization + # See https://github.com/mastodon/mastodon/pull/14510 + # https://codeberg.org/fediverse/fep/src/branch/main/feps/fep-8fcf.md if self.path.startswith('/users/') and \ self.path.endswith('/followers_synchronization'): if self.server.followers_synchronization: @@ -16918,24 +16919,15 @@ class PubServer(BaseHTTPRequestHandler): # check authorized fetch if self._secure_mode(curr_session, proxy_type): nickname = get_nickname_from_actor(self.path) - foll_sync_key = nickname + ':' + calling_domain - sync_dict = self.server.followers_sync_cache - if sync_dict.get(foll_sync_key): - sync_hash = sync_dict[foll_sync_key]['hash'] - sync_json = sync_dict[foll_sync_key]['response'] - else: - sync_json = \ - get_followers_sync_json(self.server.base_dir, - nickname, self.server.domain, + sync_cache = self.server.followers_sync_cache + sync_json, _ = \ + update_followers_sync_cache(self.server.base_dir, + nickname, + self.server.domain, self.server.http_prefix, self.server.domain_full, - calling_domain) - sync_hash = get_followers_sync_hash(sync_json) - if sync_hash: - self.server.followers_sync_cache[foll_sync_key] = { - "hash": sync_hash, - "response": sync_json - } + calling_domain, + sync_cache) msg_str = json.dumps(sync_json, ensure_ascii=False) msg_str = self._convert_domains(calling_domain, referer_domain, msg_str) diff --git a/follow.py b/follow.py index 51989eda7..92db49b74 100644 --- a/follow.py +++ b/follow.py @@ -1372,12 +1372,16 @@ def _get_followers_for_domain(base_dir: str, if not os.path.isfile(followers_filename): return [] lines = [] + foll_text = '' try: with open(followers_filename, 'r', encoding='utf-8') as fp_foll: - lines = fp_foll.read().splitlines() + foll_text = fp_foll.read() except OSError: print('EX: get_followers_for_domain unable to read followers ' + followers_filename) + if search_domain not in foll_text: + return [] + lines = foll_text.splitlines() result = [] for line_str in lines: if search_domain not in line_str: @@ -1406,10 +1410,10 @@ def _get_followers_for_domain(base_dir: str, return result -def get_followers_sync_json(base_dir: str, - nickname: str, domain: str, - http_prefix: str, domain_full: str, - search_domain: str) -> {}: +def _get_followers_sync_json(base_dir: str, + nickname: str, domain: str, + http_prefix: str, domain_full: str, + search_domain: str) -> {}: """Returns a response for followers synchronization See https://github.com/mastodon/mastodon/pull/14510 https://codeberg.org/fediverse/fep/src/branch/main/feps/fep-8fcf.md @@ -1429,7 +1433,7 @@ def get_followers_sync_json(base_dir: str, return sync_json -def get_followers_sync_hash(sync_json: {}) -> str: +def _get_followers_sync_hash(sync_json: {}) -> str: """Returns a hash used within the Collection-Synchronization http header See https://github.com/mastodon/mastodon/pull/14510 https://codeberg.org/fediverse/fep/src/branch/main/feps/fep-8fcf.md @@ -1448,6 +1452,35 @@ def get_followers_sync_hash(sync_json: {}) -> str: return sync_hash +def update_followers_sync_cache(base_dir: str, + nickname: str, domain: str, + http_prefix: str, domain_full: str, + calling_domain: str, + sync_cache: {}) -> ({}, str): + """Updates the followers synchronization cache + See https://github.com/mastodon/mastodon/pull/14510 + https://codeberg.org/fediverse/fep/src/branch/main/feps/fep-8fcf.md + """ + foll_sync_key = nickname + ':' + calling_domain + if sync_cache.get(foll_sync_key): + sync_hash = sync_cache[foll_sync_key]['hash'] + sync_json = sync_cache[foll_sync_key]['response'] + else: + sync_json = \ + _get_followers_sync_json(base_dir, + nickname, domain, + http_prefix, + domain_full, + calling_domain) + sync_hash = _get_followers_sync_hash(sync_json) + if sync_hash: + sync_cache[foll_sync_key] = { + "hash": sync_hash, + "response": sync_json + } + return sync_json, sync_hash + + def get_followers_of_actor(base_dir: str, actor: str, debug: bool) -> {}: """In a shared inbox if we receive a post we know who it's from and if it's addressed to followers then we need to get a list of those.