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

@ -1,32 +1,32 @@
import json
import logging import logging
from typing import Optional, Any from typing import Optional, Any
from federation.entities.activitypub.entities import ActivitypubProfile from federation.entities.activitypub.entities import ActivitypubProfile
from federation.entities.activitypub.mappers import message_to_objects from federation.entities.activitypub.mappers import message_to_objects
from federation.utils.network import fetch_document from federation.utils.network import fetch_document
from federation.utils.text import decode_if_bytes
logger = logging.getLogger('federation') 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. 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: if document:
from federation.protocols.activitypub.protocol import Protocol document = json.loads(decode_if_bytes(document))
protocol = Protocol() entities = message_to_objects(document, fid)
sender, payload = protocol.receive(document)
entities = message_to_objects(payload, sender)
if entities: if entities:
return entities[0] 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: if not profile:
return return
try: try: