How to use the federation.utils.django.get_function_from_config function in federation

To help you get started, we’ve selected a few federation examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jaywink / federation / federation / entities / activitypub / django / views.py View on Github external
def get(request, *args, **kwargs):
            fallback = True
            accept = request.META.get('HTTP_ACCEPT', '')
            for content_type in (
                    'application/json', 'application/activity+json', 'application/ld+json',
            ):
                if accept.find(content_type) > -1:
                    fallback = False
                    break
            if fallback:
                return func(request, *args, **kwargs)

            get_object_function = get_function_from_config('get_object_function')
            obj = get_object_function(request)
            if not obj:
                return HttpResponseNotFound()

            as2_obj = obj.as_protocol('activitypub')
            return JsonResponse(as2_obj.to_as2(), content_type='application/activity+json')
github jaywink / federation / federation / entities / activitypub / django / views.py View on Github external
def post(request, *args, **kwargs):
            process_payload_function = get_function_from_config('process_payload_function')
            result = process_payload_function(request)
            if result:
                return JsonResponse({}, content_type='application/json', status=202)
            else:
                return JsonResponse({"result": "error"}, content_type='application/json', status=400)
github jaywink / federation / federation / hostmeta / django / generators.py View on Github external
def nodeinfo2_view(request, *args, **kwargs):
    try:
        nodeinfo2_func = get_function_from_config("nodeinfo2_function")
    except AttributeError:
        return HttpResponseBadRequest("Not configured")
    nodeinfo2 = nodeinfo2_func()

    return JsonResponse(generate_nodeinfo2_document(**nodeinfo2))
github jaywink / federation / federation / hostmeta / django / generators.py View on Github external
def rfc7033_webfinger_view(request, *args, **kwargs):
    """
    Django view to generate an RFC7033 webfinger.
    """
    resource = request.GET.get("resource")
    if not resource:
        return HttpResponseBadRequest("No resource found")
    if not resource.startswith("acct:"):
        return HttpResponseBadRequest("Invalid resource")
    handle = resource.replace("acct:", "").lower()
    logger.debug(f"{handle} requested with {request}")
    profile_func = get_function_from_config("get_profile_function")

    try:
        profile = profile_func(handle=handle, request=request)
    except Exception as exc:
        logger.warning("rfc7033_webfinger_view - Failed to get profile by handle %s: %s", handle, exc)
        return HttpResponseNotFound()

    config = get_configuration()
    webfinger = RFC7033Webfinger(
        id=profile.id,
        handle=profile.handle,
        guid=profile.guid,
        base_url=config.get('base_url'),
        profile_path=get_path_from_url(profile.url),
        hcard_path=config.get('hcard_path'),
        atom_path=get_path_from_url(profile.atom_url),
github jaywink / federation / federation / entities / activitypub / entities.py View on Github external
def post_receive(self) -> None:
        """
        Post receive hook - send back follow ack.
        """
        super().post_receive()
        if not self.following:
            return

        from federation.utils.activitypub import retrieve_and_parse_profile  # Circulars
        try:
            from federation.utils.django import get_function_from_config
        except ImportError:
            logger.warning("ActivitypubFollow.post_receive - Unable to send automatic Accept back, only supported on "
                           "Django currently")
            return
        get_private_key_function = get_function_from_config("get_private_key_function")
        key = get_private_key_function(self.target_id)
        if not key:
            logger.warning("ActivitypubFollow.post_receive - Failed to send automatic Accept back: could not find "
                           "profile to sign it with")
            return
        accept = ActivitypubAccept(
            activity_id=f"{self.target_id}#accept-{uuid.uuid4()}",
            actor_id=self.target_id,
            target_id=self.activity_id,
            object=self.to_as2(),
        )
        try:
            profile = retrieve_and_parse_profile(self.actor_id)
        except Exception:
            profile = None
        if not profile:
github jaywink / federation / federation / entities / utils.py View on Github external
def get_name_for_profile(fid: str) -> Optional[str]:
    """
    Get a profile display name from a profile via the configured profile getter.

    Currently only works with Django configuration.
    """
    try:
        from federation.utils.django import get_function_from_config
        profile_func = get_function_from_config("get_profile_function")
        if not profile_func:
            return
        profile = profile_func(fid=fid)
        if not profile:
            return
        if profile.name == fid and profile.username:
            return profile.username
        else:
            return profile.name
    except Exception:
        pass