channel-folders: Send event when reordering channel folders.

This commit is contained in:
Sahil Batra
2025-08-12 16:04:16 +05:30
committed by Tim Abbott
parent e132af28fc
commit 5675860707
9 changed files with 88 additions and 5 deletions

View File

@@ -20,6 +20,11 @@ format used by the Zulip server that they are interacting with.
## Changes in Zulip 11.0
**Feature level 418**
* [`GET /events`](/api/get-events): An event with `type: "channel_folder"`
and `op: "reorder"` is sent when channel folders are reordered.
**Feature level 417**
* [`POST channels/create`](/api/create-channel): Added a dedicated

View File

@@ -34,7 +34,7 @@ DESKTOP_WARNING_VERSION = "5.9.3"
# new level means in api_docs/changelog.md, as well as "**Changes**"
# entries in the endpoint's documentation in `zulip.yaml`.
API_FEATURE_LEVEL = 417
API_FEATURE_LEVEL = 418
# Bump the minor PROVISION_VERSION to indicate that folks should provision
# only when going from an old version of the code to a newer version. Bump

View File

@@ -1,5 +1,3 @@
from collections.abc import Iterable
from django.db import transaction
from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _
@@ -49,7 +47,7 @@ def check_add_channel_folder(
@transaction.atomic(durable=True)
def try_reorder_realm_channel_folders(realm: Realm, order: Iterable[int]) -> None:
def try_reorder_realm_channel_folders(realm: Realm, order: list[int]) -> None:
order_mapping = {_[1]: _[0] for _ in enumerate(order)}
channel_folders = ChannelFolder.objects.filter(realm=realm)
for channel_folder in channel_folders:
@@ -59,6 +57,13 @@ def try_reorder_realm_channel_folders(realm: Realm, order: Iterable[int]) -> Non
channel_folder.order = order_mapping[channel_folder.id]
channel_folder.save(update_fields=["order"])
event = dict(
type="channel_folder",
op="reorder",
order=order,
)
send_event_on_commit(realm, event, active_user_ids(realm.id))
def do_send_channel_folder_update_event(
channel_folder: ChannelFolder, data: dict[str, str | bool]

View File

@@ -24,6 +24,7 @@ from zerver.lib.event_types import (
EventAttachmentRemove,
EventAttachmentUpdate,
EventChannelFolderAdd,
EventChannelFolderReorder,
EventChannelFolderUpdate,
EventCustomProfileFields,
EventDefaultStreamGroups,
@@ -169,6 +170,7 @@ check_attachment_add = make_checker(EventAttachmentAdd)
check_attachment_remove = make_checker(EventAttachmentRemove)
check_attachment_update = make_checker(EventAttachmentUpdate)
check_channel_folder_add = make_checker(EventChannelFolderAdd)
check_channel_folder_reorder = make_checker(EventChannelFolderReorder)
check_custom_profile_fields = make_checker(EventCustomProfileFields)
check_default_stream_groups = make_checker(EventDefaultStreamGroups)
check_default_streams = make_checker(EventDefaultStreams)

View File

@@ -89,6 +89,12 @@ class ChannelFolderDataForUpdate(BaseModel):
is_archived: bool | None = None
class EventChannelFolderReorder(BaseEvent):
type: Literal["channel_folder"]
op: Literal["reorder"]
order: list[int]
class EventChannelFolderUpdate(BaseEvent):
type: Literal["channel_folder"]
op: Literal["update"]

View File

@@ -1964,6 +1964,11 @@ def apply_event(
for channel_folder in state["channel_folders"]:
if channel_folder["id"] == event["channel_folder_id"]:
channel_folder.update(event["data"])
elif event["op"] == "reorder":
order_mapping = {_[1]: _[0] for _ in enumerate(event["order"])}
for channel_folder in state["channel_folders"]:
channel_folder["order"] = order_mapping[channel_folder["id"]]
state["channel_folders"].sort(key=lambda folder: folder["order"])
else:
raise AssertionError("Unexpected event type {type}/{op}".format(**event))
elif event["type"] == "has_zoom_token":

View File

@@ -6153,6 +6153,37 @@ paths:
"data": {"name": "New frontend"},
"id": 0,
}
- type: object
additionalProperties: false
description: |
Event sent to users in an organization when channel folders are reordered.
**Changes**: New in Zulip 11.0 (feature level 418).
properties:
id:
$ref: "#/components/schemas/EventIdSchema"
type:
allOf:
- $ref: "#/components/schemas/EventTypeSchema"
- enum:
- channel_folder
op:
type: string
enum:
- reorder
order:
type: array
description: |
A list of channel folder IDs representing the new order.
items:
type: integer
example:
{
"type": "channel_folder",
"op": "reorder",
"order": [3, 1, 2],
"id": 0,
}
queue_id:
type: string
description: |

View File

@@ -230,7 +230,7 @@ class GetChannelFoldersTest(ChannelFoldersTestCase):
self.assertEqual(channel_folders_names, ["Frontend", "Backend", "Marketing"])
try_reorder_realm_channel_folders(
realm, reversed([item["id"] for item in channel_folders_data])
realm, list(reversed([item["id"] for item in channel_folders_data]))
)
result = self.client_get("/json/channel_folders")
channel_folders_data = orjson.loads(result.content)["channel_folders"]

View File

@@ -33,6 +33,7 @@ from zerver.actions.channel_folders import (
do_change_channel_folder_description,
do_change_channel_folder_name,
do_unarchive_channel_folder,
try_reorder_realm_channel_folders,
)
from zerver.actions.create_user import do_create_user, do_reactivate_user
from zerver.actions.custom_profile_fields import (
@@ -169,6 +170,7 @@ from zerver.lib.event_schema import (
check_attachment_remove,
check_attachment_update,
check_channel_folder_add,
check_channel_folder_reorder,
check_channel_folder_update,
check_custom_profile_fields,
check_default_stream_groups,
@@ -5647,3 +5649,30 @@ class ChannelFolderActionTest(BaseAction):
check_channel_folder_update("events[0]", events[0], {"is_archived"})
self.assertEqual(events[0]["channel_folder_id"], channel_folder.id)
self.assertFalse(events[0]["data"]["is_archived"])
def test_channel_folders_reordering_event(self) -> None:
frontend_folder = check_add_channel_folder(
self.user_profile.realm,
"Frontend",
"Channels for frontend discussion",
acting_user=self.user_profile,
)
backend_folder = check_add_channel_folder(
self.user_profile.realm,
"Backend",
"Channels for backend discussion",
acting_user=self.user_profile,
)
engineering_folder = check_add_channel_folder(
self.user_profile.realm,
"Engineering",
"",
acting_user=self.user_profile,
)
new_order = [backend_folder.id, engineering_folder.id, frontend_folder.id]
with self.verify_action() as events:
try_reorder_realm_channel_folders(self.user_profile.realm, new_order)
check_channel_folder_reorder("events[0]", events[0])
self.assertEqual(events[0]["order"], new_order)