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 stream name validation.

Alex Vandiver3574637fbf9d
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py
index 9fad046e187..ee4b639339c 100644
--- a/zerver/lib/actions.py
+++ b/zerver/lib/actions.py
@@ -148,7 +148,6 @@ from zerver.lib.streams import (
access_stream_for_send_message,
can_access_stream_user_ids,
check_stream_access_based_on_stream_post_policy,
- check_stream_name,
create_stream_if_needed,
get_default_value_for_history_public_to_subscribers,
get_web_public_streams_queryset,
@@ -156,6 +155,7 @@ from zerver.lib.streams import (
send_stream_creation_event,
subscribed_to_stream,
)
+from zerver.lib.string_validation import check_stream_name
from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime
from zerver.lib.timezone import canonicalize_timezone
from zerver.lib.topic import (
diff --git a/zerver/lib/streams.py b/zerver/lib/streams.py
index 0f064f06004..5168c29b112 100644
--- a/zerver/lib/streams.py
+++ b/zerver/lib/streams.py
@@ -13,6 +13,7 @@ from zerver.lib.exceptions import (
)
from zerver.lib.markdown import markdown_convert
from zerver.lib.stream_subscription import get_active_subscriptions_for_stream_id
+from zerver.lib.string_validation import check_stream_name
from zerver.models import (
DefaultStreamGroup,
Realm,
@@ -175,20 +176,6 @@ def create_streams_if_needed(
return added_streams, existing_streams
-def check_stream_name(stream_name: str) -> None:
- if stream_name.strip() == "":
- raise JsonableError(_("Invalid stream name '{}'").format(stream_name))
- if len(stream_name) > Stream.MAX_NAME_LENGTH:
- raise JsonableError(
- _("Stream name too long (limit: {} characters).").format(Stream.MAX_NAME_LENGTH)
- )
- for i in stream_name:
- if ord(i) == 0:
- raise JsonableError(
- _("Stream name '{}' contains NULL (0x00) characters.").format(stream_name)
- )
-
-
def subscribed_to_stream(user_profile: UserProfile, stream_id: int) -> bool:
return Subscription.objects.filter(
user_profile=user_profile,
diff --git a/zerver/lib/string_validation.py b/zerver/lib/string_validation.py
new file mode 100644
index 00000000000..b23a19bb483
--- /dev/null
+++ b/zerver/lib/string_validation.py
@@ -0,0 +1,20 @@
+from typing import Optional
+
+from django.utils.translation import gettext as _
+
+from zerver.lib.exceptions import JsonableError
+from zerver.models import Stream
+
+
+def check_stream_name(stream_name: str) -> None:
+ if stream_name.strip() == "":
+ raise JsonableError(_("Invalid stream name '{}'").format(stream_name))
+ if len(stream_name) > Stream.MAX_NAME_LENGTH:
+ raise JsonableError(
+ _("Stream name too long (limit: {} characters).").format(Stream.MAX_NAME_LENGTH)
+ )
+ for i in stream_name:
+ if ord(i) == 0:
+ raise JsonableError(
+ _("Stream name '{}' contains NULL (0x00) characters.").format(stream_name)
+ )
diff --git a/zerver/views/streams.py b/zerver/views/streams.py
index 081dc621c15..ee4e3cc80ea 100644
--- a/zerver/views/streams.py
+++ b/zerver/views/streams.py
@@ -61,11 +61,11 @@ from zerver.lib.streams import (
access_stream_by_name,
access_stream_for_delete_or_update,
access_web_public_stream,
- check_stream_name,
check_stream_name_available,
filter_stream_authorization,
list_to_streams,
)
+from zerver.lib.string_validation import check_stream_name
from zerver.lib.topic import (
get_topic_history_for_public_stream,
get_topic_history_for_stream,