channel_folders: Pass realm as argument to check_add_channel_folder.

Passing realm explicitly to check_add_channel_folder instead of
using realm field from acting_user seems much cleaner and readable.
This commit is contained in:
Sahil Batra
2025-05-21 12:47:23 +05:30
committed by Tim Abbott
parent ad9abb8e2d
commit 88b47be938
8 changed files with 36 additions and 20 deletions

View File

@@ -2,7 +2,7 @@ from django.db import transaction
from django.utils.timezone import now as timezone_now
from zerver.lib.channel_folders import get_channel_folder_dict, render_channel_folder_description
from zerver.models import ChannelFolder, RealmAuditLog, UserProfile
from zerver.models import ChannelFolder, Realm, RealmAuditLog, UserProfile
from zerver.models.realm_audit_logs import AuditLogEventType
from zerver.models.users import active_user_ids
from zerver.tornado.django_api import send_event_on_commit
@@ -10,9 +10,8 @@ from zerver.tornado.django_api import send_event_on_commit
@transaction.atomic(durable=True)
def check_add_channel_folder(
name: str, description: str, *, acting_user: UserProfile
realm: Realm, name: str, description: str, *, acting_user: UserProfile
) -> ChannelFolder:
realm = acting_user.realm
rendered_description = render_channel_folder_description(
description, realm, acting_user=acting_user
)

View File

@@ -833,8 +833,8 @@ class TestRealmAuditLog(ZulipTestCase):
def test_change_stream_folder(self) -> None:
user = self.example_user("iago")
stream = self.make_stream("test", user.realm)
frontend_folder = check_add_channel_folder("Frontend", "", acting_user=user)
backend_folder = check_add_channel_folder("Backend", "", acting_user=user)
frontend_folder = check_add_channel_folder(user.realm, "Frontend", "", acting_user=user)
backend_folder = check_add_channel_folder(user.realm, "Backend", "", acting_user=user)
now = timezone_now()
do_change_stream_folder(stream, frontend_folder, acting_user=user)
@@ -1612,6 +1612,7 @@ class TestRealmAuditLog(ZulipTestCase):
iago = self.example_user("iago")
now = timezone_now()
channel_folder = check_add_channel_folder(
iago.realm,
"Frontend",
"Channels for frontend discussions",
acting_user=iago,

View File

@@ -696,7 +696,7 @@ class TestCreateStreams(ZulipTestCase):
realm = get_realm("zulip")
iago = self.example_user("iago")
hamlet = self.example_user("hamlet")
channel_folder = check_add_channel_folder("Backend", "", acting_user=iago)
channel_folder = check_add_channel_folder(realm, "Backend", "", acting_user=iago)
subscriptions = [
{"name": "new_stream", "description": "New stream"},

View File

@@ -79,18 +79,21 @@ class GetChannelFoldersTest(ZulipTestCase):
def test_get_channel_folders(self) -> None:
iago = self.example_user("iago")
desdemona = self.example_user("desdemona")
zulip_realm = iago.realm
frontend_folder = check_add_channel_folder(
"Frontend", "Channels for frontend discussions", acting_user=iago
zulip_realm, "Frontend", "Channels for frontend discussions", acting_user=iago
)
backend_folder = check_add_channel_folder(
"Backend", "Channels for **backend** discussions", acting_user=iago
zulip_realm, "Backend", "Channels for **backend** discussions", acting_user=iago
)
marketing_folder = check_add_channel_folder(
"Marketing", "Channels for marketing discussions", acting_user=desdemona
zulip_realm, "Marketing", "Channels for marketing discussions", acting_user=desdemona
)
lear_user = self.lear_user("cordelia")
check_add_channel_folder("Devops", "Channels for devops discussions", acting_user=lear_user)
check_add_channel_folder(
lear_user.realm, "Devops", "Channels for devops discussions", acting_user=lear_user
)
def check_channel_folders_in_zulip_realm(
channel_folders: list[dict[str, Any]], marketing_folder_included: bool = True
@@ -168,12 +171,14 @@ class GetChannelFoldersTest(ZulipTestCase):
class UpdateChannelFoldersTest(ZulipTestCase):
def test_updating_channel_folder_name(self) -> None:
realm = get_realm("zulip")
channel_folder = check_add_channel_folder(
realm,
"Frontend",
"Channels for frontend discussions",
acting_user=self.example_user("desdemona"),
)
check_add_channel_folder("Backend", "", acting_user=self.example_user("desdemona"))
check_add_channel_folder(realm, "Backend", "", acting_user=self.example_user("desdemona"))
channel_folder_id = channel_folder.id
self.login("hamlet")
@@ -208,6 +213,7 @@ class UpdateChannelFoldersTest(ZulipTestCase):
def test_updating_channel_folder_description(self) -> None:
channel_folder = check_add_channel_folder(
get_realm("zulip"),
"Frontend",
"Channels for frontend discussions",
acting_user=self.example_user("desdemona"),
@@ -245,6 +251,7 @@ class UpdateChannelFoldersTest(ZulipTestCase):
def test_archiving_and_unarchiving_channel_folder(self) -> None:
channel_folder = check_add_channel_folder(
get_realm("zulip"),
"Frontend",
"Channels for frontend discussions",
acting_user=self.example_user("desdemona"),

View File

@@ -204,8 +204,8 @@ class EventsEndpointTest(ZulipTestCase):
realm = get_realm("zulip")
iago = self.example_user("iago")
frontend_folder = check_add_channel_folder("Frontend", "", acting_user=iago)
backend_folder = check_add_channel_folder("Backend", "", acting_user=iago)
frontend_folder = check_add_channel_folder(realm, "Frontend", "", acting_user=iago)
backend_folder = check_add_channel_folder(realm, "Backend", "", acting_user=iago)
result = self.client_post("/json/register")
self.assertEqual(result.status_code, 200)

View File

@@ -4978,7 +4978,7 @@ class SubscribeActionTest(BaseAction):
check_stream_update("events[0]", events[0])
check_message("events[1]", events[1])
channel_folder = check_add_channel_folder("Frontend", "", acting_user=iago)
channel_folder = check_add_channel_folder(realm, "Frontend", "", acting_user=iago)
with self.verify_action(include_subscribers=include_subscribers) as events:
do_change_stream_folder(stream, channel_folder, acting_user=iago)
check_stream_update("events[0]", events[0])
@@ -5494,12 +5494,20 @@ class ChannelFolderActionTest(BaseAction):
folder_name = "Frontend"
folder_description = "Channels for **frontend** discussions"
with self.verify_action() as events:
check_add_channel_folder(folder_name, folder_description, acting_user=self.user_profile)
check_add_channel_folder(
self.user_profile.realm,
folder_name,
folder_description,
acting_user=self.user_profile,
)
check_channel_folder_add("events[0]", events[0])
def test_channel_folder_update_event(self) -> None:
channel_folder = check_add_channel_folder(
"Frontend", "Channels for frontend discussion", acting_user=self.user_profile
self.user_profile.realm,
"Frontend",
"Channels for frontend discussion",
acting_user=self.user_profile,
)
iago = self.example_user("iago")

View File

@@ -2920,7 +2920,7 @@ class StreamAdminTest(ZulipTestCase):
def test_updating_stream_folder(self) -> None:
iago = self.example_user("iago")
channel_folder = check_add_channel_folder("Frontend", "", acting_user=iago)
channel_folder = check_add_channel_folder(iago.realm, "Frontend", "", acting_user=iago)
stream = self.make_stream("test_stream")
self.assertIsNone(stream.folder_id)
@@ -2953,7 +2953,7 @@ class StreamAdminTest(ZulipTestCase):
iago = self.example_user("iago")
hamlet = self.example_user("hamlet")
realm = iago.realm
channel_folder = check_add_channel_folder("Frontend", "", acting_user=iago)
channel_folder = check_add_channel_folder(realm, "Frontend", "", acting_user=iago)
stream = self.make_stream("test_stream")
self.assertIsNone(stream.folder_id)

View File

@@ -31,8 +31,9 @@ def create_channel_folder(
name: Annotated[str, StringConstraints(max_length=ChannelFolder.MAX_NAME_LENGTH)],
description: Annotated[str, StringConstraints(max_length=ChannelFolder.MAX_DESCRIPTION_LENGTH)],
) -> HttpResponse:
check_channel_folder_name(name, user_profile.realm)
channel_folder = check_add_channel_folder(name, description, acting_user=user_profile)
realm = user_profile.realm
check_channel_folder_name(name, realm)
channel_folder = check_add_channel_folder(realm, name, description, acting_user=user_profile)
return json_success(request, data={"channel_folder_id": channel_folder.id})