diff --git a/apprise/plugins/matrix.py b/apprise/plugins/matrix.py index 536de9d9..3dde574e 100644 --- a/apprise/plugins/matrix.py +++ b/apprise/plugins/matrix.py @@ -1300,14 +1300,14 @@ class NotifyMatrix(NotifyBase): timeout=self.request_timeout, ) + # Store status code + status_code = r.status_code + self.logger.debug( 'Matrix Response: code=%d, %s' % ( r.status_code, str(r.content))) response = loads(r.content) - # Store status code - status_code = r.status_code - if r.status_code == requests.codes.too_many_requests: wait = self.default_wait_ms / 1000 try: @@ -1616,7 +1616,7 @@ class NotifyMatrix(NotifyBase): self.store.get(self.discovery_identity_key), ) - if base_url is not None and identity_url is not None: + if not (base_url is None and identity_url is None): # We can use our cached value and return early return base_url @@ -1641,7 +1641,7 @@ class NotifyMatrix(NotifyBase): self.logger.debug( 'Matrix Well-Known Base URI not found at %s', verify_url) - # Clear our keys out for fast recall later on + # Set our keys out for fast recall later on self.store.set( self.discovery_base_key, '', expires=self.discovery_cache_length_sec) @@ -1657,6 +1657,20 @@ class NotifyMatrix(NotifyBase): '%s - %s returned error code: %d', msg, verify_url, code) raise MatrixDiscoveryException(msg, error_code=code) + if not wk_response: + # This is an acceptable response; we simply do nothing + self.logger.debug( + 'Matrix Well-Known Base URI not defined %s', verify_url) + + # Set our keys out for fast recall later on + self.store.set( + self.discovery_base_key, '', + expires=self.discovery_cache_length_sec) + self.store.set( + self.discovery_identity_key, '', + expires=self.discovery_cache_length_sec) + return '' + # # Parse our m.homeserver information # @@ -1695,8 +1709,7 @@ class NotifyMatrix(NotifyBase): # # Phase 2: Handle m.identity_server IF defined # - if isinstance(wk_response, dict) \ - and 'm.identity_server' in wk_response: + if 'm.identity_server' in wk_response: try: identity_url = \ wk_response['m.identity_server']['base_url'].rstrip('/') @@ -1733,7 +1746,14 @@ class NotifyMatrix(NotifyBase): # Update our cache self.store.set( self.discovery_identity_key, identity_url, - expires=self.discovery_cache_length_sec) + # Add 2 seconds to prevent this key from expiring before base + expires=self.discovery_cache_length_sec + 2) + else: + # No identity server + self.store.set( + self.discovery_identity_key, '', + # Add 2 seconds to prevent this key from expiring before base + expires=self.discovery_cache_length_sec + 2) # Update our cache self.store.set( @@ -1758,6 +1778,8 @@ class NotifyMatrix(NotifyBase): self.discovery_base_key, self.discovery_identity_key) raise + # If we get hear, we need to build our URL dynamically based on what + # was provided to us during the plugins initialization default_port = 443 if self.secure else 80 return '{schema}://{hostname}{port}'.format( diff --git a/test/test_plugin_matrix.py b/test/test_plugin_matrix.py index 879bcca3..5562ce1c 100644 --- a/test/test_plugin_matrix.py +++ b/test/test_plugin_matrix.py @@ -1192,12 +1192,23 @@ def test_plugin_matrix_discovery_service(mock_post, mock_get): assert NotifyMatrix.discovery_base_key not in obj.store assert NotifyMatrix.discovery_identity_key not in obj.store - # Enforce cleanup + # Test an empty block response response.status_code = requests.codes.ok + response.content = '' mock_get.return_value = response mock_get.side_effect = None mock_post.return_value = response mock_post.side_effect = None + obj.store.clear( + NotifyMatrix.discovery_base_key, NotifyMatrix.discovery_identity_key) + + assert obj.base_url == 'https://example.com' + assert obj.identity_url == 'https://example.com' + + # Verify cache saved + assert NotifyMatrix.discovery_base_key in obj.store + assert NotifyMatrix.discovery_identity_key in obj.store + del obj