mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	url_decoding: Add 'is_same_server_message_link' function.
This prep commit adds a lib function 'is_same_server_message_link'. This will be currently used while compressing quote and reply in push notifications and later can be used at other places.
This commit is contained in:
		
				
					committed by
					
						
						Tim Abbott
					
				
			
			
				
	
			
			
			
						parent
						
							5fd676c4e0
						
					
				
				
					commit
					484befe9ce
				
			
							
								
								
									
										26
									
								
								zerver/lib/url_decoding.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								zerver/lib/url_decoding.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
from urllib.parse import urlsplit
 | 
			
		||||
 | 
			
		||||
from django.conf import settings
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def is_same_server_message_link(url: str) -> bool:
 | 
			
		||||
    split_result = urlsplit(url)
 | 
			
		||||
    hostname = split_result.hostname
 | 
			
		||||
    fragment = split_result.fragment
 | 
			
		||||
 | 
			
		||||
    if hostname not in {None, settings.EXTERNAL_HOST_WITHOUT_PORT}:
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
    # A message link always has category `narrow`, section `stream`
 | 
			
		||||
    # or `dm`, and ends with `/near/<message_id>`, where <message_id>
 | 
			
		||||
    # is a sequence of digits. The URL fragment of a message link has
 | 
			
		||||
    # at least 5 parts. e.g. '#narrow/dm/9,15-dm/near/43'
 | 
			
		||||
    fragment_parts = fragment.split("/")
 | 
			
		||||
    if len(fragment_parts) < 5:
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
    category = fragment_parts[0]
 | 
			
		||||
    section = fragment_parts[1]
 | 
			
		||||
    ends_with_near_message_id = fragment_parts[-2] == "near" and fragment_parts[-1].isdigit()
 | 
			
		||||
 | 
			
		||||
    return category == "narrow" and section in {"stream", "dm"} and ends_with_near_message_id
 | 
			
		||||
							
								
								
									
										13
									
								
								zerver/tests/test_url_decoding.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								zerver/tests/test_url_decoding.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
import orjson
 | 
			
		||||
 | 
			
		||||
from zerver.lib.test_classes import ZulipTestCase
 | 
			
		||||
from zerver.lib.url_decoding import is_same_server_message_link
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class URLDecodeTest(ZulipTestCase):
 | 
			
		||||
    def test_is_same_server_message_link(self) -> None:
 | 
			
		||||
        tests = orjson.loads(self.fixture_data("message_link_test_cases.json"))
 | 
			
		||||
        for test in tests:
 | 
			
		||||
            self.assertEqual(
 | 
			
		||||
                is_same_server_message_link(test["message_link"]), test["expected_output"]
 | 
			
		||||
            )
 | 
			
		||||
		Reference in New Issue
	
	Block a user