string_validation: Factor out stream name validation.

Co-authored-by: Shlok Patel <shlokcpatel2001@gmail.com>
This commit is contained in:
Alex Vandiver
2022-01-11 12:47:44 -08:00
parent 339e70671c
commit 3574637fbf
4 changed files with 23 additions and 16 deletions

View File

@@ -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 (

View File

@@ -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,

View File

@@ -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)
)

View File

@@ -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,