2022-01-11 21:49:40 +01:00
|
|
|
import unicodedata
|
2022-01-11 21:47:44 +01:00
|
|
|
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() == "":
|
2022-01-11 21:57:53 +01:00
|
|
|
raise JsonableError(_("Stream name can't be empty!"))
|
|
|
|
|
2022-01-11 21:47:44 +01:00
|
|
|
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)
|
|
|
|
)
|
2022-01-11 21:49:40 +01:00
|
|
|
|
|
|
|
|
2022-01-11 21:54:50 +01:00
|
|
|
def check_stream_topic(topic: str) -> None:
|
|
|
|
if topic.strip() == "":
|
2022-01-11 21:49:40 +01:00
|
|
|
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!"))
|