2021-11-01 23:55:07 +01:00
|
|
|
from argparse import ArgumentParser
|
2024-07-12 02:30:23 +02:00
|
|
|
from typing import Any
|
2021-11-01 23:55:07 +01:00
|
|
|
|
|
|
|
from django.core.management.base import CommandError
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
2021-11-01 23:55:07 +01:00
|
|
|
|
2023-09-19 02:01:43 +02:00
|
|
|
from zerver.actions.streams import deactivated_streams_by_old_name, do_unarchive_stream
|
2021-11-01 23:55:07 +01:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
|
|
|
from zerver.models import RealmAuditLog, Stream
|
2024-09-06 13:56:53 +02:00
|
|
|
from zerver.models.realm_audit_logs import AuditLogEventType
|
2021-11-01 23:55:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Command(ZulipBaseCommand):
|
2024-05-06 13:40:27 +02:00
|
|
|
help = """Reactivate a channel that was deactivated."""
|
2021-11-01 23:55:07 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2021-11-01 23:55:07 +01:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2024-05-06 13:40:27 +02:00
|
|
|
specify_channel = parser.add_mutually_exclusive_group(required=True)
|
|
|
|
specify_channel.add_argument(
|
|
|
|
"-c",
|
|
|
|
"--channel",
|
|
|
|
help="Name of a deactivated channel in the realm.",
|
2021-11-01 23:55:07 +01:00
|
|
|
)
|
2024-05-06 13:40:27 +02:00
|
|
|
specify_channel.add_argument(
|
|
|
|
"--channel-id",
|
|
|
|
help="ID of a deactivated channel in the realm.",
|
2021-11-01 23:55:07 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-n",
|
|
|
|
"--new-name",
|
|
|
|
required=False,
|
|
|
|
help="Name to reactivate as; defaults to the old name.",
|
|
|
|
)
|
|
|
|
self.add_realm_args(parser, required=True)
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2024-07-12 02:30:23 +02:00
|
|
|
def handle(self, *args: Any, **options: str | None) -> None:
|
2021-11-01 23:55:07 +01:00
|
|
|
realm = self.get_realm(options)
|
|
|
|
assert realm is not None # Should be ensured by parser
|
|
|
|
|
2024-05-06 13:40:27 +02:00
|
|
|
# Looking up the channel is complicated, since they get renamed
|
2021-11-01 23:55:07 +01:00
|
|
|
# when they are deactivated, in a transformation which may be
|
|
|
|
# lossy.
|
|
|
|
|
2024-05-06 13:40:27 +02:00
|
|
|
if options["channel_id"] is not None:
|
|
|
|
channel = Stream.objects.get(id=options["channel_id"])
|
|
|
|
if channel.realm_id != realm.id:
|
2024-04-10 23:27:08 +02:00
|
|
|
raise CommandError(
|
2024-05-06 13:40:27 +02:00
|
|
|
f"Channel id {channel.id}, named '{channel.name}', is in realm '{channel.realm.string_id}', not '{realm.string_id}'"
|
2024-04-10 23:27:08 +02:00
|
|
|
)
|
2024-05-06 13:40:27 +02:00
|
|
|
if not channel.deactivated:
|
2021-11-01 23:55:07 +01:00
|
|
|
raise CommandError(
|
2024-05-06 13:40:27 +02:00
|
|
|
f"Channel id {channel.id}, named '{channel.name}', is not deactivated"
|
2021-11-01 23:55:07 +01:00
|
|
|
)
|
|
|
|
if options["new_name"] is None:
|
2024-05-06 13:40:27 +02:00
|
|
|
raise CommandError("--new-name flag is required with --channel-id")
|
2021-11-01 23:55:07 +01:00
|
|
|
new_name = options["new_name"]
|
|
|
|
else:
|
2024-05-06 13:40:27 +02:00
|
|
|
channel_name = options["channel"]
|
|
|
|
assert channel_name is not None
|
2021-11-01 23:55:07 +01:00
|
|
|
|
2024-05-06 13:40:27 +02:00
|
|
|
possible_channels = deactivated_streams_by_old_name(realm, channel_name)
|
|
|
|
if len(possible_channels) == 0:
|
|
|
|
raise CommandError("No matching deactivated channels found!")
|
2021-11-01 23:55:07 +01:00
|
|
|
|
2024-05-06 13:40:27 +02:00
|
|
|
if len(possible_channels) > 1:
|
|
|
|
# Print ids of all possible channels, support passing by id
|
|
|
|
print("Matching channels:")
|
|
|
|
for channel in possible_channels:
|
2021-11-01 23:55:07 +01:00
|
|
|
last_deactivation = (
|
|
|
|
RealmAuditLog.objects.filter(
|
|
|
|
realm=realm,
|
2024-05-06 13:40:27 +02:00
|
|
|
modified_stream=channel,
|
2024-09-06 13:56:53 +02:00
|
|
|
event_type=AuditLogEventType.CHANNEL_DEACTIVATED,
|
2021-11-01 23:55:07 +01:00
|
|
|
)
|
|
|
|
.order_by("-id")
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
assert last_deactivation is not None
|
|
|
|
print(
|
2024-05-06 13:40:27 +02:00
|
|
|
f" ({channel.id}) {channel.name}, deactivated on {last_deactivation.event_time}"
|
2021-11-01 23:55:07 +01:00
|
|
|
)
|
|
|
|
raise CommandError(
|
2024-05-06 13:40:27 +02:00
|
|
|
"More than one matching channel found! Specify which with --channel-id"
|
2021-11-01 23:55:07 +01:00
|
|
|
)
|
|
|
|
|
2024-05-06 13:40:27 +02:00
|
|
|
channel = possible_channels[0]
|
2021-11-01 23:55:07 +01:00
|
|
|
if options["new_name"] is not None:
|
|
|
|
new_name = options["new_name"]
|
|
|
|
else:
|
2024-05-06 13:40:27 +02:00
|
|
|
new_name = channel_name
|
2021-11-01 23:55:07 +01:00
|
|
|
|
|
|
|
if Stream.objects.filter(realm=realm, name=new_name).exists():
|
|
|
|
raise CommandError(
|
2024-05-06 13:40:27 +02:00
|
|
|
f"Channel with name '{new_name}' already exists; pass a different --new-name"
|
2021-11-01 23:55:07 +01:00
|
|
|
)
|
|
|
|
|
2024-05-06 13:40:27 +02:00
|
|
|
assert channel is not None
|
|
|
|
do_unarchive_stream(channel, new_name, acting_user=None)
|