コード例 #1
0
ファイル: views.py プロジェクト: hoseinfzad/federation
        def get(request, *args, **kwargs):
            # TODO remove once AP support is more ready
            config = get_configuration()
            if not config.get("activitypub"):
                return func(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')
コード例 #2
0
def get_matrix_configuration() -> Optional[Dict]:
    """
    Return Matrix configuration.

    Requires Django support currently.
    """
    try:
        matrix_config_func = get_function_from_config("matrix_config_function")
    except AttributeError:
        raise AttributeError("Not configured for Matrix support")
    return matrix_config_func()
コード例 #3
0
    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:
            logger.warning(
                "ActivitypubFollow.post_receive - Failed to fetch remote profile for sending back Accept"
            )
            return
        try:
            handle_send(
                accept,
                UserType(id=self.target_id, private_key=key),
                recipients=[{
                    "endpoint": profile.inboxes["private"],
                    "fid": self.actor_id,
                    "protocol": "activitypub",
                    "public": False,
                }],
            )
        except Exception:
            logger.exception(
                "ActivitypubFollow.post_receive - Failed to send Accept back")
コード例 #4
0
 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)
コード例 #5
0
ファイル: views.py プロジェクト: trusttri/federation
    def put(self, request, *args, **kwargs):
        # Inject the transaction ID to the request as part of the meta items
        request.META["matrix_transaction_id"] = kwargs.get("txn_id")
        process_payload_function = get_function_from_config(
            'process_payload_function')
        result = process_payload_function(request)

        if result:
            return JsonResponse({},
                                content_type='application/json',
                                status=200)
        else:
            return JsonResponse({"error": "M_UNKNOWN"},
                                content_type='application/json',
                                status=400)
コード例 #6
0
def test_get_function_from_config():
    func = get_function_from_config("get_profile_function")
    assert callable(func)