Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

string_validation: Factor out topic validation.

Alex Vandiver1cdb93f6aa04
diff --git a/zerver/lib/addressee.py b/zerver/lib/addressee.py
index b4efd7bb9e9..207bf5cfd86 100644
--- a/zerver/lib/addressee.py
+++ b/zerver/lib/addressee.py
@@ -1,9 +1,9 @@
-import unicodedata
from typing import Iterable, List, Optional, Sequence, Union, cast
from django.utils.translation import gettext as _
from zerver.lib.exceptions import JsonableError
+from zerver.lib.string_validation import check_stream_topic
from zerver.models import (
Realm,
Stream,
@@ -35,19 +35,6 @@ def get_user_profiles_by_ids(user_ids: Iterable[int], realm: Realm) -> List[User
return user_profiles
-def validate_topic(topic: str) -> str:
- assert topic is not None
- topic = topic.strip()
- if topic == "":
- raise JsonableError(_("Topic can't be empty"))
-
- for character in topic:
- unicodeCategory = unicodedata.category(character)
- if unicodeCategory in ["Cc", "Cs", "Cn"]:
- raise JsonableError(_("Invalid characters in topic!"))
- return topic
-
-
class Addressee:
# This is really just a holder for vars that tended to be passed
# around in a non-type-safe way before this class was introduced.
@@ -160,7 +147,7 @@ class Addressee:
@staticmethod
def for_stream(stream: Stream, topic: str) -> "Addressee":
- topic = validate_topic(topic)
+ topic = check_stream_topic(topic)
return Addressee(
msg_type="stream",
stream=stream,
@@ -169,7 +156,7 @@ class Addressee:
@staticmethod
def for_stream_name(stream_name: str, topic: str) -> "Addressee":
- topic = validate_topic(topic)
+ topic = check_stream_topic(topic)
return Addressee(
msg_type="stream",
stream_name=stream_name,
@@ -178,7 +165,7 @@ class Addressee:
@staticmethod
def for_stream_id(stream_id: int, topic: str) -> "Addressee":
- topic = validate_topic(topic)
+ topic = check_stream_topic(topic)
return Addressee(
msg_type="stream",
stream_id=stream_id,
diff --git a/zerver/lib/string_validation.py b/zerver/lib/string_validation.py
index cb49de3978f..7a404e17564 100644
--- a/zerver/lib/string_validation.py
+++ b/zerver/lib/string_validation.py
@@ -1,3 +1,4 @@
+import unicodedata
from typing import Optional
from django.utils.translation import gettext as _
@@ -19,3 +20,16 @@ def check_stream_name(stream_name: str) -> None:
raise JsonableError(
_("Stream name '{}' contains NULL (0x00) characters.").format(stream_name)
)
+
+
+def check_stream_topic(topic: str) -> str:
+ assert topic is not None
+ topic = topic.strip()
+ if topic == "":
+ raise JsonableError(_("Topic can't be empty"))
+
+ for character in topic:
+ unicodeCategory = unicodedata.category(character)
+ if unicodeCategory in ["Cc", "Cs", "Cn"]:
+ raise JsonableError(_("Invalid characters in topic!"))
+ return topic