From 65c9b249b7b7f0999bc1f1250b046346edac10c3 Mon Sep 17 00:00:00 2001 From: Kislay Udbhav Verma Date: Sun, 27 Oct 2024 16:50:08 +0530 Subject: [PATCH] markdown: Refactor classes handling stream topic links. The classes StreamPattern and StreamTopicPattern both had a separate copy of the function `find_stream_id` which did the same thing. Since another Pattern will be added as a part of #31920, it is a good idea to move that function into a superclass which is then inherited by all the related patterns. Fixes part of #31920 --- zerver/lib/markdown/__init__.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.py index 74962206f8..f004d7035f 100644 --- a/zerver/lib/markdown/__init__.py +++ b/zerver/lib/markdown/__init__.py @@ -1984,7 +1984,7 @@ class UserGroupMentionPattern(CompiledInlineProcessor): return None, None, None -class StreamPattern(CompiledInlineProcessor): +class StreamTopicMessageProcessor(CompiledInlineProcessor): def find_stream_id(self, name: str) -> int | None: db_data: DbData | None = self.zmd.zulip_db_data if db_data is None: @@ -1992,6 +1992,8 @@ class StreamPattern(CompiledInlineProcessor): stream_id = db_data.stream_names.get(name) return stream_id + +class StreamPattern(StreamTopicMessageProcessor): @override def handleMatch( # type: ignore[override] # https://github.com/python/mypy/issues/10197 self, m: Match[str], data: str @@ -2016,14 +2018,7 @@ class StreamPattern(CompiledInlineProcessor): return el, m.start(), m.end() -class StreamTopicPattern(CompiledInlineProcessor): - def find_stream_id(self, name: str) -> int | None: - db_data: DbData | None = self.zmd.zulip_db_data - if db_data is None: - return None - stream_id = db_data.stream_names.get(name) - return stream_id - +class StreamTopicPattern(StreamTopicMessageProcessor): @override def handleMatch( # type: ignore[override] # https://github.com/python/mypy/issues/10197 self, m: Match[str], data: str