Beispiel #1
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")
 def test_calls_profile_validate(self, mock_retrieve):
     mock_profile = Mock()
     mock_retrieve.return_value = mock_profile
     retrieve_and_parse_profile("https://example.com/profile")
     assert mock_profile.validate.called
 def test_calls_retrieve_and_parse_document__with_url_fid(
         self, mock_retrieve):
     retrieve_and_parse_profile("https://example.com/profile")
     mock_retrieve.assert_called_once_with("https://example.com/profile")
 def test_returns_none_on_invalid_document(self, mock_fetch):
     profile = retrieve_and_parse_profile("https://example.com/profile")
     assert profile is None
 def test_calls_get_profile_id_from_webfinger__with_handle_fid(
         self, mock_get):
     retrieve_and_parse_profile("*****@*****.**")
     mock_get.assert_called_once_with("*****@*****.**")