Fix document fetcher to parse correctly fetched AS2 document into profile

merge-requests/143/head
Jason Robinson 2019-03-14 22:06:39 +02:00
rodzic 9359831ea2
commit ad17e2cd2f
2 zmienionych plików z 10 dodań i 10 usunięć

Wyświetl plik

@ -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.

Wyświetl plik

@ -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: