2015-07-03 10:09:40 +00:00
|
|
|
import importlib
|
2016-07-24 18:54:54 +00:00
|
|
|
import logging
|
2015-07-03 10:09:40 +00:00
|
|
|
|
|
|
|
from federation.exceptions import NoSuitableProtocolFoundError
|
|
|
|
|
2016-10-04 19:07:24 +00:00
|
|
|
logger = logging.getLogger("federation")
|
2016-07-24 18:54:54 +00:00
|
|
|
|
2015-07-03 10:09:40 +00:00
|
|
|
PROTOCOLS = (
|
|
|
|
"diaspora",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-07-06 20:03:31 +00:00
|
|
|
def handle_receive(payload, user=None, sender_key_fetcher=None, skip_author_verification=False):
|
2015-07-03 10:20:09 +00:00
|
|
|
"""Takes a payload and passes it to the correct protocol.
|
|
|
|
|
2017-05-06 20:15:22 +00:00
|
|
|
Returns a tuple of:
|
|
|
|
- sender handle
|
|
|
|
- protocol name
|
|
|
|
- list of entities
|
|
|
|
|
|
|
|
NOTE! The returned sender is NOT necessarily the *author* of the entity. By sender here we're
|
|
|
|
talking about the sender of the *payload*. If this object is being relayed by the sender, the author
|
|
|
|
could actually be a different identity.
|
|
|
|
|
2016-10-02 10:08:37 +00:00
|
|
|
:arg payload: Payload blob (str)
|
2017-05-21 19:46:23 +00:00
|
|
|
:arg user: User that will be passed to `protocol.receive` (only required on private encrypted content)
|
|
|
|
MUST have a `private_key` and `guid` if given.
|
2016-10-02 10:08:37 +00:00
|
|
|
: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)
|
|
|
|
:returns: Tuple of sender handle, protocol name and list of entity objects
|
|
|
|
:raises NoSuitableProtocolFound: When no protocol was identified to pass message to
|
2015-07-03 10:20:09 +00:00
|
|
|
"""
|
2016-07-24 18:54:54 +00:00
|
|
|
logger.debug("handle_receive: processing payload: %s", payload)
|
2015-07-03 16:46:17 +00:00
|
|
|
found_protocol = None
|
2015-07-03 10:09:40 +00:00
|
|
|
for protocol_name in PROTOCOLS:
|
|
|
|
protocol = importlib.import_module("federation.protocols.%s.protocol" % protocol_name)
|
|
|
|
if protocol.identify_payload(payload):
|
2015-07-03 16:46:17 +00:00
|
|
|
found_protocol = protocol
|
2015-07-03 10:09:40 +00:00
|
|
|
break
|
|
|
|
|
2015-07-03 16:46:17 +00:00
|
|
|
if found_protocol:
|
2016-07-24 18:54:54 +00:00
|
|
|
logger.debug("handle_receive: using protocol %s", found_protocol.PROTOCOL_NAME)
|
2015-07-03 16:46:17 +00:00
|
|
|
protocol = found_protocol.Protocol()
|
2015-07-06 20:03:31 +00:00
|
|
|
sender, message = protocol.receive(
|
|
|
|
payload, user, sender_key_fetcher, skip_author_verification=skip_author_verification)
|
2016-07-24 18:54:54 +00:00
|
|
|
logger.debug("handle_receive: sender %s, message %s", sender, message)
|
2015-07-03 10:09:40 +00:00
|
|
|
else:
|
|
|
|
raise NoSuitableProtocolFoundError()
|
2015-07-03 20:36:49 +00:00
|
|
|
|
|
|
|
mappers = importlib.import_module("federation.entities.%s.mappers" % found_protocol.PROTOCOL_NAME)
|
2017-05-21 19:46:23 +00:00
|
|
|
entities = mappers.message_to_objects(message, sender_key_fetcher, user)
|
2016-07-24 18:54:54 +00:00
|
|
|
logger.debug("handle_receive: entities %s", entities)
|
2015-07-03 20:36:49 +00:00
|
|
|
|
|
|
|
return sender, found_protocol.PROTOCOL_NAME, entities
|