2015-07-03 10:09:40 +00:00
|
|
|
import importlib
|
2016-07-24 18:54:54 +00:00
|
|
|
import logging
|
2018-07-31 20:30:21 +00:00
|
|
|
from typing import Tuple, List, Callable
|
2015-07-03 10:09:40 +00:00
|
|
|
|
2019-03-03 00:51:49 +00:00
|
|
|
from federation import identify_protocol_by_request
|
|
|
|
from federation.types import UserType, RequestType
|
2015-07-03 10:09:40 +00:00
|
|
|
|
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
|
|
|
|
2018-07-31 20:30:21 +00:00
|
|
|
def handle_receive(
|
2019-03-03 00:51:49 +00:00
|
|
|
request: RequestType,
|
|
|
|
user: UserType = None,
|
|
|
|
sender_key_fetcher: Callable[[str], str] = None,
|
|
|
|
skip_author_verification: bool = False
|
2018-07-31 20:30:21 +00:00
|
|
|
) -> Tuple[str, str, List]:
|
2019-03-03 00:51:49 +00:00
|
|
|
"""Takes a request and passes it to the correct protocol.
|
2015-07-03 10:20:09 +00:00
|
|
|
|
2017-05-06 20:15:22 +00:00
|
|
|
Returns a tuple of:
|
2018-07-31 20:30:21 +00:00
|
|
|
- sender id
|
2017-05-06 20:15:22 +00:00
|
|
|
- protocol name
|
|
|
|
- list of entities
|
|
|
|
|
|
|
|
NOTE! The returned sender is NOT necessarily the *author* of the entity. By sender here we're
|
2019-03-03 00:51:49 +00:00
|
|
|
talking about the sender of the *request*. If this object is being relayed by the sender, the author
|
2017-05-06 20:15:22 +00:00
|
|
|
could actually be a different identity.
|
|
|
|
|
2019-03-03 00:51:49 +00:00
|
|
|
:arg request: Request object of type RequestType - note not a HTTP request even though the structure is similar
|
2017-05-21 19:46:23 +00:00
|
|
|
:arg user: User that will be passed to `protocol.receive` (only required on private encrypted content)
|
2018-07-31 20:30:21 +00:00
|
|
|
MUST have a `private_key` and `id` 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)
|
2018-07-31 20:30:21 +00:00
|
|
|
:returns: Tuple of sender id, protocol name and list of entity objects
|
2015-07-03 10:20:09 +00:00
|
|
|
"""
|
2019-03-03 00:51:49 +00:00
|
|
|
logger.debug("handle_receive: processing request: %s", request)
|
|
|
|
found_protocol = identify_protocol_by_request(request)
|
2018-10-28 19:54:23 +00:00
|
|
|
|
|
|
|
logger.debug("handle_receive: using protocol %s", found_protocol.PROTOCOL_NAME)
|
|
|
|
protocol = found_protocol.Protocol()
|
|
|
|
sender, message = protocol.receive(
|
2019-03-03 00:51:49 +00:00
|
|
|
request, user, sender_key_fetcher, skip_author_verification=skip_author_verification)
|
2018-10-28 19:54:23 +00:00
|
|
|
logger.debug("handle_receive: sender %s, message %s", sender, message)
|
2015-07-03 20:36:49 +00:00
|
|
|
|
|
|
|
mappers = importlib.import_module("federation.entities.%s.mappers" % found_protocol.PROTOCOL_NAME)
|
2017-08-06 11:04:50 +00:00
|
|
|
entities = mappers.message_to_objects(message, sender, 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
|