channel_folders: Add support to update channel folders.

Fixes part of #31972.
This commit is contained in:
Sahil Batra
2025-05-07 19:21:08 +05:30
committed by Tim Abbott
parent e93667cc06
commit 5de0f265bd
14 changed files with 534 additions and 6 deletions

View File

@@ -41,3 +41,95 @@ def check_add_channel_folder(
send_event_on_commit(realm, event, active_user_ids(realm.id))
return channel_folder
def do_send_channel_folder_update_event(
channel_folder: ChannelFolder, data: dict[str, str | bool]
) -> None:
realm = channel_folder.realm
event = dict(type="channel_folder", op="update", channel_folder_id=channel_folder.id, data=data)
send_event_on_commit(realm, event, active_user_ids(realm.id))
@transaction.atomic(durable=True)
def do_change_channel_folder_name(
channel_folder: ChannelFolder, name: str, *, acting_user: UserProfile
) -> None:
old_value = channel_folder.name
channel_folder.name = name
channel_folder.save(update_fields=["name"])
RealmAuditLog.objects.create(
realm=acting_user.realm,
acting_user=acting_user,
event_type=AuditLogEventType.CHANNEL_FOLDER_NAME_CHANGED,
event_time=timezone_now(),
modified_channel_folder=channel_folder,
extra_data={
RealmAuditLog.OLD_VALUE: old_value,
RealmAuditLog.NEW_VALUE: name,
},
)
do_send_channel_folder_update_event(channel_folder, dict(name=name))
@transaction.atomic(durable=True)
def do_change_channel_folder_description(
channel_folder: ChannelFolder, description: str, *, acting_user: UserProfile
) -> None:
old_value = channel_folder.description
rendered_description = render_channel_folder_description(
description, acting_user.realm, acting_user=acting_user
)
channel_folder.description = description
channel_folder.rendered_description = rendered_description
channel_folder.save(update_fields=["description", "rendered_description"])
RealmAuditLog.objects.create(
realm=acting_user.realm,
acting_user=acting_user,
event_type=AuditLogEventType.CHANNEL_FOLDER_DESCRIPTION_CHANGED,
event_time=timezone_now(),
modified_channel_folder=channel_folder,
extra_data={
RealmAuditLog.OLD_VALUE: old_value,
RealmAuditLog.NEW_VALUE: description,
},
)
do_send_channel_folder_update_event(
channel_folder, dict(description=description, rendered_description=rendered_description)
)
@transaction.atomic(durable=True)
def do_archive_channel_folder(channel_folder: ChannelFolder, *, acting_user: UserProfile) -> None:
channel_folder.is_archived = True
channel_folder.save(update_fields=["is_archived"])
RealmAuditLog.objects.create(
realm=acting_user.realm,
acting_user=acting_user,
event_type=AuditLogEventType.CHANNEL_FOLDER_ARCHIVED,
event_time=timezone_now(),
modified_channel_folder=channel_folder,
)
do_send_channel_folder_update_event(channel_folder, dict(is_archived=True))
@transaction.atomic(durable=True)
def do_unarchive_channel_folder(channel_folder: ChannelFolder, *, acting_user: UserProfile) -> None:
channel_folder.is_archived = False
channel_folder.save(update_fields=["is_archived"])
RealmAuditLog.objects.create(
realm=acting_user.realm,
acting_user=acting_user,
event_type=AuditLogEventType.CHANNEL_FOLDER_UNARCHIVED,
event_time=timezone_now(),
modified_channel_folder=channel_folder,
)
do_send_channel_folder_update_event(channel_folder, dict(is_archived=False))