Move handle_send protocol identification to federation module root

Add also "identify by id" function.
merge-requests/140/head
Jason Robinson 2018-10-28 21:54:23 +02:00
rodzic b30b0f5d78
commit a1b3afba7f
4 zmienionych plików z 45 dodań i 23 usunięć

Wyświetl plik

@ -1 +1,30 @@
import importlib
from federation.exceptions import NoSuitableProtocolFoundError
__version__ = "0.18.0-dev" __version__ = "0.18.0-dev"
PROTOCOLS = (
"activitypub",
"diaspora",
)
def identify_protocol(method: str, value: str):
"""
Loop through protocols, import the protocol module and try to identify the id or payload.
"""
for protocol_name in PROTOCOLS:
protocol = importlib.import_module(f"federation.protocols.{protocol_name}.protocol")
if getattr(protocol, f"identify_{method}")(value):
return protocol
else:
raise NoSuitableProtocolFoundError()
def identify_protocol_by_id(id: str):
return identify_protocol('id', id)
def identify_protocol_by_payload(payload: str):
return identify_protocol('payload', payload)

Wyświetl plik

@ -2,16 +2,11 @@ import importlib
import logging import logging
from typing import Tuple, List, Callable from typing import Tuple, List, Callable
from federation.exceptions import NoSuitableProtocolFoundError from federation import identify_protocol_by_payload
from federation.types import UserType from federation.types import UserType
logger = logging.getLogger("federation") logger = logging.getLogger("federation")
PROTOCOLS = (
"activitypub",
"diaspora",
)
def handle_receive( def handle_receive(
payload: str, payload: str,
@ -36,24 +31,15 @@ def handle_receive(
:arg sender_key_fetcher: Function that accepts sender handle and returns public key (optional) :arg sender_key_fetcher: Function that accepts sender handle and returns public key (optional)
:arg skip_author_verification: Don't verify sender (test purposes, false default) :arg skip_author_verification: Don't verify sender (test purposes, false default)
:returns: Tuple of sender id, protocol name and list of entity objects :returns: Tuple of sender id, protocol name and list of entity objects
:raises NoSuitableProtocolFound: When no protocol was identified to pass message to
""" """
logger.debug("handle_receive: processing payload: %s", payload) logger.debug("handle_receive: processing payload: %s", payload)
found_protocol = None found_protocol = identify_protocol_by_payload(payload)
for protocol_name in PROTOCOLS:
protocol = importlib.import_module("federation.protocols.%s.protocol" % protocol_name)
if protocol.identify_payload(payload):
found_protocol = protocol
break
if found_protocol: logger.debug("handle_receive: using protocol %s", found_protocol.PROTOCOL_NAME)
logger.debug("handle_receive: using protocol %s", found_protocol.PROTOCOL_NAME) protocol = found_protocol.Protocol()
protocol = found_protocol.Protocol() sender, message = protocol.receive(
sender, message = protocol.receive( payload, user, sender_key_fetcher, skip_author_verification=skip_author_verification)
payload, user, sender_key_fetcher, skip_author_verification=skip_author_verification) logger.debug("handle_receive: sender %s, message %s", sender, message)
logger.debug("handle_receive: sender %s, message %s", sender, message)
else:
raise NoSuitableProtocolFoundError()
mappers = importlib.import_module("federation.entities.%s.mappers" % found_protocol.PROTOCOL_NAME) mappers = importlib.import_module("federation.entities.%s.mappers" % found_protocol.PROTOCOL_NAME)
entities = mappers.message_to_objects(message, sender, sender_key_fetcher, user) entities = mappers.message_to_objects(message, sender, sender_key_fetcher, user)

Wyświetl plik

@ -13,7 +13,7 @@ from federation.protocols.diaspora.encrypted import EncryptedPayload
from federation.protocols.diaspora.magic_envelope import MagicEnvelope from federation.protocols.diaspora.magic_envelope import MagicEnvelope
from federation.types import UserType from federation.types import UserType
from federation.utils.diaspora import fetch_public_key from federation.utils.diaspora import fetch_public_key
from federation.utils.text import decode_if_bytes, encode_if_text from federation.utils.text import decode_if_bytes, encode_if_text, validate_handle
logger = logging.getLogger("federation") logger = logging.getLogger("federation")
@ -22,6 +22,13 @@ PROTOCOL_NS = "https://joindiaspora.com/protocol"
MAGIC_ENV_TAG = "{http://salmon-protocol.org/ns/magic-env}env" MAGIC_ENV_TAG = "{http://salmon-protocol.org/ns/magic-env}env"
def identify_id(id: str) -> bool:
"""
Try to identify if this ID is a Diaspora ID.
"""
return validate_handle(id)
def identify_payload(payload): def identify_payload(payload):
"""Try to identify whether this is a Diaspora payload. """Try to identify whether this is a Diaspora payload.

Wyświetl plik

@ -8,7 +8,7 @@ from federation.protocols.diaspora.protocol import Protocol
from federation.tests.fixtures.payloads import DIASPORA_PUBLIC_PAYLOAD from federation.tests.fixtures.payloads import DIASPORA_PUBLIC_PAYLOAD
class TestHandleReceiveProtocolIdentification(): class TestHandleReceiveProtocolIdentification:
def test_handle_receive_routes_to_identified_protocol(self): def test_handle_receive_routes_to_identified_protocol(self):
payload = DIASPORA_PUBLIC_PAYLOAD payload = DIASPORA_PUBLIC_PAYLOAD
with patch.object( with patch.object(