From ad17e2cd2f4ec869947d82f6bdb377abf631e01f Mon Sep 17 00:00:00 2001 From: Jason Robinson Date: Thu, 14 Mar 2019 22:06:39 +0200 Subject: [PATCH] Fix document fetcher to parse correctly fetched AS2 document into profile --- federation/entities/activitypub/mappers.py | 2 +- federation/utils/activitypub.py | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/federation/entities/activitypub/mappers.py b/federation/entities/activitypub/mappers.py index 986394a..163c66f 100644 --- a/federation/entities/activitypub/mappers.py +++ b/federation/entities/activitypub/mappers.py @@ -68,7 +68,7 @@ def get_outbound_entity(entity: BaseEntity, private_key: str): def message_to_objects( - message: Dict, sender: str, sender_key_fetcher:Callable[[str], str]=None, user: UserType =None, + message: Dict, sender: str, sender_key_fetcher: Callable[[str], str] = None, user: UserType = None, ) -> List: """ Takes in a message extracted by a protocol and maps it to entities. diff --git a/federation/utils/activitypub.py b/federation/utils/activitypub.py index a60c825..264d7ac 100644 --- a/federation/utils/activitypub.py +++ b/federation/utils/activitypub.py @@ -1,32 +1,32 @@ +import json import logging from typing import Optional, Any from federation.entities.activitypub.entities import ActivitypubProfile from federation.entities.activitypub.mappers import message_to_objects from federation.utils.network import fetch_document +from federation.utils.text import decode_if_bytes logger = logging.getLogger('federation') -def retrieve_and_parse_document(id: str) -> Optional[Any]: +def retrieve_and_parse_document(fid: str) -> Optional[Any]: """ Retrieve remote document by ID and return the entity. """ - document, status_code, ex = fetch_document(id, extra_headers={'accept': 'application/activity+json'}) + document, status_code, ex = fetch_document(fid, extra_headers={'accept': 'application/activity+json'}) if document: - from federation.protocols.activitypub.protocol import Protocol - protocol = Protocol() - sender, payload = protocol.receive(document) - entities = message_to_objects(payload, sender) + document = json.loads(decode_if_bytes(document)) + entities = message_to_objects(document, fid) if entities: return entities[0] -def retrieve_and_parse_profile(id: str) -> Optional[ActivitypubProfile]: +def retrieve_and_parse_profile(fid: str) -> Optional[ActivitypubProfile]: """ - Retrieve the remote id and return a Profile object. + Retrieve the remote fid and return a Profile object. """ - profile = retrieve_and_parse_document(id) + profile = retrieve_and_parse_document(fid) if not profile: return try: