From f46f30e61ad64db3b7fb2fc5343a35ca5ed74515 Mon Sep 17 00:00:00 2001 From: Alain St-Denis Date: Thu, 19 May 2022 12:48:57 +0000 Subject: [PATCH] Revert to request_cache monkey patching, take #2. Catch KeyError in model_to_objects. Run extract_receivers for unsupported objects. --- federation/entities/activitypub/models.py | 9 ++++----- federation/utils/network.py | 12 ++++++------ 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/federation/entities/activitypub/models.py b/federation/entities/activitypub/models.py index fda1e6d..38c2fa7 100644 --- a/federation/entities/activitypub/models.py +++ b/federation/entities/activitypub/models.py @@ -900,7 +900,6 @@ def element_to_objects(element: Union[Dict, Object]) -> List: """ Transform an Element to a list of entities. """ - entities = [] # json-ld handling with calamus # Skips unimplemented payloads @@ -914,14 +913,14 @@ def element_to_objects(element: Union[Dict, Object]) -> List: except ValueError: logger.error("Failed to validate entity %s: %s", entity, ex) return None - entities.append(entity) #if not found_parent and getattr(entity, 'target_id', None): # entities = retrieve_and_parse_document(entity.target_id) + entities #if getattr(entity, 'replies', None): # entities += process_reply_collection(getattr(entity.replies,'first', None)) - return entities + return [entity] elif entity: logger.info('Entity type "%s" was handled through the json-ld processor but is not a base entity', entity.__class__.__name__) + entity._receivers = extract_receivers(entity) return [entity] else: logger.warning("Payload not implemented by the json-ld processor, skipping") @@ -933,8 +932,8 @@ def model_to_objects(payload): if model and issubclass(model, Object): try: entity = model.schema().load(payload) - except (jsonld.JsonLdError, exceptions.ValidationError) as exc : # Just give up for now. This must be made robust - logger.warning(f"Invalid jsonld payload, skipping ({exc})") + except (KeyError, jsonld.JsonLdError, exceptions.ValidationError) as exc : # Just give up for now. This must be made robust + logger.warning(f"Error parsing jsonld payload ({exc})") return None if isinstance(getattr(entity, 'object_', None), Object): diff --git a/federation/utils/network.py b/federation/utils/network.py index b31b1c4..f7417ca 100644 --- a/federation/utils/network.py +++ b/federation/utils/network.py @@ -11,7 +11,7 @@ import requests from requests.exceptions import RequestException, HTTPError, SSLError from requests.exceptions import ConnectionError from requests.structures import CaseInsensitiveDict -from requests_cache import install_cache, RedisCache, SQLiteCache +import requests_cache as rc from federation import __version__ @@ -22,13 +22,13 @@ try: from federation.utils.django import get_configuration cfg = get_configuration() if cfg.get('redis'): - backend = RedisCache(namespace='fed_cache', **cfg['redis']) + backend = rc.RedisCache(namespace='fed_cache', **cfg['redis']) else: - backend = SQLiteCache(db_path='fed_cache') + backend = rc.SQLiteCache(db_path='fed_cache') except ImportError: - backend = SQLiteCache(db_path='fed_cache') + backend = rc.SQLiteCache(db_path='fed_cache') -install_cache(backend=backend, expire_after=7200) +rc.install_cache(backend=backend, expire_after=7200) USER_AGENT = "python/federation/%s" % __version__ @@ -77,7 +77,7 @@ def fetch_document(url=None, host=None, path="/", timeout=10, raise_ssl_errors=T if cache: response = requests.get(url, timeout=timeout, headers=headers, **kwargs) else: - with requests_cache.disabled(): + with rc.disabled(): response = requests.get(url, timeout=timeout, headers=headers, **kwargs) logger.debug("fetch_document: found document, code %s", response.status_code) response.raise_for_status()