From f896a7667f849683f80cfa3ad907c13e232b09ff Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Bodas Date: Tue, 30 Mar 2021 15:38:03 +0530 Subject: [PATCH] refactor: Update some uses of "filter" to "linkifier". This updates some comments and local variables which could be changed without breaking other stuff. --- .../puppeteer_tests/realm-linkifier.ts | 2 +- zerver/lib/actions.py | 8 +-- zerver/lib/markdown/__init__.py | 6 +-- zerver/models.py | 2 +- zerver/tests/test_markdown.py | 54 +++++++++---------- zerver/tests/test_message_dict.py | 6 +-- zerver/tests/test_realm_linkifiers.py | 4 +- 7 files changed, 41 insertions(+), 41 deletions(-) diff --git a/frontend_tests/puppeteer_tests/realm-linkifier.ts b/frontend_tests/puppeteer_tests/realm-linkifier.ts index 8d72a51b53..74470ffef5 100644 --- a/frontend_tests/puppeteer_tests/realm-linkifier.ts +++ b/frontend_tests/puppeteer_tests/realm-linkifier.ts @@ -50,7 +50,7 @@ async function test_invalid_linkifier_pattern(page: Page): Promise { await page.waitForSelector("div#admin-linkifier-pattern-status", {visible: true}); assert.strictEqual( await common.get_text_from_selector(page, "div#admin-linkifier-pattern-status"), - "Failed: Invalid filter pattern. Valid characters are [ a-zA-Z_#=/:+!-].", + "Failed: Invalid linkifier pattern. Valid characters are [ a-zA-Z_#=/:+!-].", ); } diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index f6c1e2cb59..1a728d948f 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -6505,12 +6505,12 @@ def notify_realm_filters(realm: Realm) -> None: def do_add_realm_filter(realm: Realm, pattern: str, url_format_string: str) -> int: pattern = pattern.strip() url_format_string = url_format_string.strip() - realm_filter = RealmFilter(realm=realm, pattern=pattern, url_format_string=url_format_string) - realm_filter.full_clean() - realm_filter.save() + linkifier = RealmFilter(realm=realm, pattern=pattern, url_format_string=url_format_string) + linkifier.full_clean() + linkifier.save() notify_realm_filters(realm) - return realm_filter.id + return linkifier.id def do_remove_realm_filter( diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.py index cb25f42a51..4624b0bb3e 100644 --- a/zerver/lib/markdown/__init__.py +++ b/zerver/lib/markdown/__init__.py @@ -1781,7 +1781,7 @@ OUTER_CAPTURE_GROUP = "linkifier_actual_match" def prepare_realm_pattern(source: str) -> str: - """Augment a realm filter so it only matches after start-of-string, + """Augment a linkifier so it only matches after start-of-string, whitespace, or opening delimiters, won't match if there are word characters directly after, and saves what was matched as OUTER_CAPTURE_GROUP.""" @@ -2059,7 +2059,7 @@ class LinkInlineProcessor(markdown.inlinepatterns.LinkInlineProcessor): if not el.text or not el.text.strip(): el.text = href - # Prevent realm_filters from running on the content of a Markdown link, breaking up the link. + # Prevent linkifiers from running on the content of a Markdown link, breaking up the link. # This is a monkey-patch, but it might be worth sending a version of this change upstream. el.text = markdown.util.AtomicString(el.text) @@ -2633,7 +2633,7 @@ def do_convert( # Spend at most 5 seconds rendering; this protects the backend # from being overloaded by bugs (e.g. Markdown logic that is # extremely inefficient in corner cases) as well as user - # errors (e.g. a realm filter that makes some syntax + # errors (e.g. a linkifier that makes some syntax # infinite-loop). rendered_content = timeout(5, lambda: _md_engine.convert(content)) diff --git a/zerver/models.py b/zerver/models.py index cdf9a982c3..b355cdc5a8 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -849,7 +849,7 @@ post_delete.connect(flush_realm_emoji, sender=RealmEmoji) def filter_pattern_validator(value: str) -> None: regex = re.compile(r"^(?:(?:[\w\-#_= /:]*|[+]|[!])(\(\?P<\w+>.+\)))+$") - error_msg = _("Invalid filter pattern. Valid characters are {}.").format( + error_msg = _("Invalid linkifier pattern. Valid characters are {}.").format( "[ a-zA-Z_#=/:+!-]", ) diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.py index 8954161f83..a4039ad6c5 100644 --- a/zerver/tests/test_markdown.py +++ b/zerver/tests/test_markdown.py @@ -1257,12 +1257,12 @@ class MarkdownTest(ZulipTestCase): def test_realm_patterns(self) -> None: realm = get_realm("zulip") url_format_string = r"https://trac.example.com/ticket/%(id)s" - realm_filter = RealmFilter( + linkifier = RealmFilter( realm=realm, pattern=r"#(?P[0-9]{2,8})", url_format_string=url_format_string ) - realm_filter.save() + linkifier.save() self.assertEqual( - realm_filter.__str__(), + linkifier.__str__(), "[0-9]{2,8}) https://trac.example.com/ticket/%(id)s>", ) @@ -1368,27 +1368,27 @@ class MarkdownTest(ZulipTestCase): def test_multiple_matching_realm_patterns(self) -> None: realm = get_realm("zulip") url_format_string = r"https://trac.example.com/ticket/%(id)s" - realm_filter_1 = RealmFilter( + linkifier_1 = RealmFilter( realm=realm, pattern=r"(?PABC\-[0-9]+)(?![A-Z0-9-])", url_format_string=url_format_string, ) - realm_filter_1.save() + linkifier_1.save() self.assertEqual( - realm_filter_1.__str__(), + linkifier_1.__str__(), r"ABC\-[0-9]+)(?![A-Z0-9-])" " https://trac.example.com/ticket/%(id)s>", ) url_format_string = r"https://other-trac.example.com/ticket/%(id)s" - realm_filter_2 = RealmFilter( + linkifier_2 = RealmFilter( realm=realm, pattern=r"(?P[A-Z][A-Z0-9]*\-[0-9]+)(?![A-Z0-9-])", url_format_string=url_format_string, ) - realm_filter_2.save() + linkifier_2.save() self.assertEqual( - realm_filter_2.__str__(), + linkifier_2.__str__(), r"[A-Z][A-Z0-9]*\-[0-9]+)(?![A-Z0-9-])" " https://other-trac.example.com/ticket/%(id)s>", ) @@ -1404,8 +1404,8 @@ class MarkdownTest(ZulipTestCase): converted = markdown_convert(content, message_realm=realm, message=msg) converted_topic = topic_links(realm.id, msg.topic_name()) - # The second filter (which was saved later) was ignored as the content was marked AtomicString after first conversion. - # There was no easy way to support parsing both filters and not run into an infinite loop, hence the second filter is ignored. + # The second linkifier (which was saved later) was ignored as the content was marked AtomicString after first conversion. + # There was no easy way to support parsing both linkifiers and not run into an infinite loop, hence the second linkifier is ignored. self.assertEqual( converted, '

We should fix ABC-123 or trac ABC-123 today.

', @@ -1422,10 +1422,10 @@ class MarkdownTest(ZulipTestCase): def test_maybe_update_markdown_engines(self) -> None: realm = get_realm("zulip") url_format_string = r"https://trac.example.com/ticket/%(id)s" - realm_filter = RealmFilter( + linkifier = RealmFilter( realm=realm, pattern=r"#(?P[0-9]{2,8})", url_format_string=url_format_string ) - realm_filter.save() + linkifier.save() import zerver.lib.markdown @@ -1436,10 +1436,10 @@ class MarkdownTest(ZulipTestCase): self.assertEqual(len(zulip_filters), 1) self.assertEqual( zulip_filters[0], - ("#(?P[0-9]{2,8})", "https://trac.example.com/ticket/%(id)s", realm_filter.id), + ("#(?P[0-9]{2,8})", "https://trac.example.com/ticket/%(id)s", linkifier.id), ) - def test_flush_realm_filter(self) -> None: + def test_flush_linkifier(self) -> None: realm = get_realm("zulip") def flush() -> None: @@ -2083,12 +2083,12 @@ class MarkdownTest(ZulipTestCase): msg = Message(sender=sender_user_profile, sending_client=get_client("test")) # Create a linkifier. url_format_string = r"https://trac.example.com/ticket/%(id)s" - realm_filter = RealmFilter( + linkifier = RealmFilter( realm=realm, pattern=r"#(?P[0-9]{2,8})", url_format_string=url_format_string ) - realm_filter.save() + linkifier.save() self.assertEqual( - realm_filter.__str__(), + linkifier.__str__(), "[0-9]{2,8}) https://trac.example.com/ticket/%(id)s>", ) # Create a user that potentially interferes with the pattern. @@ -2167,12 +2167,12 @@ class MarkdownTest(ZulipTestCase): user_profile = self.example_user("hamlet") # Create a linkifier. url_format_string = r"https://trac.example.com/ticket/%(id)s" - realm_filter = RealmFilter( + linkifier = RealmFilter( realm=realm, pattern=r"#(?P[0-9]{2,8})", url_format_string=url_format_string ) - realm_filter.save() + linkifier.save() self.assertEqual( - realm_filter.__str__(), + linkifier.__str__(), "[0-9]{2,8}) https://trac.example.com/ticket/%(id)s>", ) # Create a user-group that potentially interferes with the pattern. @@ -2361,12 +2361,12 @@ class MarkdownTest(ZulipTestCase): # Create a linkifier. sender_user_profile = self.example_user("othello") url_format_string = r"https://trac.example.com/ticket/%(id)s" - realm_filter = RealmFilter( + linkifier = RealmFilter( realm=realm, pattern=r"#(?P[0-9]{2,8})", url_format_string=url_format_string ) - realm_filter.save() + linkifier.save() self.assertEqual( - realm_filter.__str__(), + linkifier.__str__(), "[0-9]{2,8}) https://trac.example.com/ticket/%(id)s>", ) # Create a topic link that potentially interferes with the pattern. @@ -2430,12 +2430,12 @@ class MarkdownTest(ZulipTestCase): # Create a linkifier. sender_user_profile = self.example_user("othello") url_format_string = r"https://trac.example.com/ticket/%(id)s" - realm_filter = RealmFilter( + linkifier = RealmFilter( realm=realm, pattern=r"#(?P[0-9]{2,8})", url_format_string=url_format_string ) - realm_filter.save() + linkifier.save() self.assertEqual( - realm_filter.__str__(), + linkifier.__str__(), "[0-9]{2,8}) https://trac.example.com/ticket/%(id)s>", ) # Create a stream that potentially interferes with the pattern. diff --git a/zerver/tests/test_message_dict.py b/zerver/tests/test_message_dict.py index 58af382881..6b2c08ab05 100644 --- a/zerver/tests/test_message_dict.py +++ b/zerver/tests/test_message_dict.py @@ -249,11 +249,11 @@ class MessageDictTest(ZulipTestCase): links = {"url": "https://trac.example.com/ticket/123", "text": "#123"} topic_name = "test #123" - realm_filter = RealmFilter( + linkifier = RealmFilter( realm=zulip_realm, pattern=r"#(?P[0-9]{2,8})", url_format_string=url_format_string ) self.assertEqual( - realm_filter.__str__(), + linkifier.__str__(), "[0-9]{2,8}) https://trac.example.com/ticket/%(id)s>", ) @@ -271,7 +271,7 @@ class MessageDictTest(ZulipTestCase): assert_topic_links([], get_message(self.example_user("othello"))) assert_topic_links([], get_message(self.lear_user("cordelia"))) assert_topic_links([], get_message(self.notification_bot())) - realm_filter.save() + linkifier.save() assert_topic_links([links], get_message(self.example_user("othello"))) assert_topic_links([links], get_message(self.lear_user("cordelia"))) assert_topic_links([links], get_message(self.notification_bot())) diff --git a/zerver/tests/test_realm_linkifiers.py b/zerver/tests/test_realm_linkifiers.py index 1c4459fbff..8777e2d8c1 100644 --- a/zerver/tests/test_realm_linkifiers.py +++ b/zerver/tests/test_realm_linkifiers.py @@ -24,13 +24,13 @@ class RealmFilterTest(ZulipTestCase): data["pattern"] = "$a" result = self.client_post("/json/realm/filters", info=data) self.assert_json_error( - result, "Invalid filter pattern. Valid characters are [ a-zA-Z_#=/:+!-]." + result, "Invalid linkifier pattern. Valid characters are [ a-zA-Z_#=/:+!-]." ) data["pattern"] = r"ZUL-(?P\d++)" result = self.client_post("/json/realm/filters", info=data) self.assert_json_error( - result, "Invalid filter pattern. Valid characters are [ a-zA-Z_#=/:+!-]." + result, "Invalid linkifier pattern. Valid characters are [ a-zA-Z_#=/:+!-]." ) data["pattern"] = r"ZUL-(?P\d+)"