2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2017-07-08 21:42:50 +02:00
|
|
|
from django.core.management.base import CommandParser
|
2013-01-09 17:00:17 +01:00
|
|
|
|
2022-04-14 23:42:50 +02:00
|
|
|
from zerver.lib.actions import bulk_add_subscriptions
|
2017-07-07 20:35:31 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2022-04-14 23:42:50 +02:00
|
|
|
from zerver.lib.streams import ensure_stream
|
2013-01-09 17:00:17 +01:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-07-07 20:35:31 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2013-01-09 17:00:17 +01:00
|
|
|
help = """Add some or all users in a realm to a set of streams."""
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
2021-05-10 21:29:25 +02:00
|
|
|
self.add_realm_args(parser, required=True)
|
2017-08-24 21:46:13 +02:00
|
|
|
self.add_user_list_args(parser, all_users_help="Add all users in realm to these streams.")
|
2016-11-03 10:22:19 +01:00
|
|
|
|
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"-s", "--streams", required=True, help="A comma-separated list of stream names."
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2016-11-03 10:22:19 +01:00
|
|
|
|
2021-08-14 16:51:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2017-08-19 21:23:50 +02:00
|
|
|
realm = self.get_realm(options)
|
2017-09-26 01:25:39 +02:00
|
|
|
assert realm is not None # Should be ensured by parser
|
|
|
|
|
2017-08-19 21:23:50 +02:00
|
|
|
user_profiles = self.get_users(options, realm)
|
2020-04-09 21:51:58 +02:00
|
|
|
stream_names = {stream.strip() for stream in options["streams"].split(",")}
|
2013-01-09 17:00:17 +01:00
|
|
|
|
|
|
|
for stream_name in set(stream_names):
|
|
|
|
for user_profile in user_profiles:
|
2020-06-29 23:31:25 +02:00
|
|
|
stream = ensure_stream(realm, stream_name, acting_user=None)
|
2021-02-12 08:19:30 +01:00
|
|
|
_ignore, already_subscribed = bulk_add_subscriptions(
|
2021-04-02 18:33:28 +02:00
|
|
|
realm, [stream], [user_profile], acting_user=None
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2020-10-14 16:35:03 +02:00
|
|
|
was_there_already = user_profile.id in (info.user.id for info in already_subscribed)
|
2021-02-12 08:19:30 +01:00
|
|
|
print(
|
|
|
|
"{} {} to {}".format(
|
|
|
|
"Already subscribed" if was_there_already else "Subscribed",
|
|
|
|
user_profile.delivery_email,
|
|
|
|
stream_name,
|
|
|
|
)
|
|
|
|
)
|