2019-11-16 01:29:16 +01:00
|
|
|
from argparse import ArgumentParser
|
2023-08-30 18:36:47 +02:00
|
|
|
from typing import Any, Callable, Dict, List, Optional
|
2019-11-16 01:29:16 +01:00
|
|
|
|
2023-08-03 23:32:46 +02:00
|
|
|
import orjson
|
2021-05-19 22:42:49 +02:00
|
|
|
from django.conf import settings
|
2023-08-03 22:20:37 +02:00
|
|
|
from django.db.models import Q, QuerySet
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
2021-05-19 22:42:49 +02:00
|
|
|
|
2023-08-03 23:22:21 +02:00
|
|
|
from confirmation.models import one_click_unsubscribe_link
|
2021-07-16 22:11:10 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2020-04-11 13:15:57 +02:00
|
|
|
from zerver.lib.send_email import send_custom_email
|
2021-04-28 01:23:44 +02:00
|
|
|
from zerver.models import Realm, UserProfile
|
2019-11-16 01:29:16 +01:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2019-11-16 01:29:16 +01:00
|
|
|
class Command(ZulipBaseCommand):
|
2020-04-14 19:55:04 +02:00
|
|
|
help = """
|
|
|
|
Send a custom email with Zulip branding to the specified users.
|
|
|
|
|
|
|
|
Useful to send a notice to all users of a realm or server.
|
|
|
|
|
2020-08-11 01:47:49 +02:00
|
|
|
The From and Subject headers can be provided in the body of the Markdown
|
2020-04-14 19:55:04 +02:00
|
|
|
document used to generate the email, or on the command line."""
|
2019-11-16 01:29:16 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2019-11-16 01:29:16 +01:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2023-08-03 21:57:13 +02:00
|
|
|
targets = parser.add_mutually_exclusive_group(required=True)
|
|
|
|
targets.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"--entire-server", action="store_true", help="Send to every user on the server."
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2023-08-03 21:57:13 +02:00
|
|
|
targets.add_argument(
|
2021-08-02 06:35:01 +02:00
|
|
|
"--marketing",
|
|
|
|
action="store_true",
|
|
|
|
help="Send to active users and realm owners with the enable_marketing_emails setting enabled.",
|
|
|
|
)
|
2023-08-03 21:57:13 +02:00
|
|
|
targets.add_argument(
|
2021-12-15 02:09:12 +01:00
|
|
|
"--remote-servers",
|
|
|
|
action="store_true",
|
|
|
|
help="Send to registered contact email addresses for remote Zulip servers.",
|
|
|
|
)
|
2023-08-03 21:57:13 +02:00
|
|
|
targets.add_argument(
|
|
|
|
"--all-sponsored-org-admins",
|
|
|
|
action="store_true",
|
|
|
|
help="Send to all organization administrators of sponsored organizations.",
|
|
|
|
)
|
2023-08-03 23:32:46 +02:00
|
|
|
targets.add_argument(
|
|
|
|
"--json-file",
|
|
|
|
help="Load the JSON file, and send to the users whose ids are the keys in that dict; "
|
|
|
|
"the context for each email will be extended by each value in the dict.",
|
|
|
|
)
|
2023-08-03 21:57:13 +02:00
|
|
|
self.add_user_list_args(
|
|
|
|
targets,
|
|
|
|
help="Email addresses of user(s) to send emails to.",
|
|
|
|
all_users_help="Send to every user on the realm.",
|
|
|
|
)
|
|
|
|
# Realm is only required for --users and --all-users, so it is
|
|
|
|
# not mutually exclusive with the rest of the above.
|
|
|
|
self.add_realm_args(parser)
|
|
|
|
|
|
|
|
# This is an additional filter which is composed with the above
|
|
|
|
parser.add_argument(
|
|
|
|
"--admins-only",
|
|
|
|
help="Filter recipients selected via other options to to only organization administrators",
|
|
|
|
action="store_true",
|
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"--markdown-template-path",
|
|
|
|
"--path",
|
2021-02-12 08:19:30 +01:00
|
|
|
required=True,
|
2021-02-12 08:20:45 +01:00
|
|
|
help="Path to a Markdown-format body for the email.",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"--subject",
|
|
|
|
help="Subject for the email. It can be declared in Markdown file in headers",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"--from-name",
|
|
|
|
help="From line for the email. It can be declared in Markdown file in headers",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
parser.add_argument("--reply-to", help="Optional reply-to line for the email")
|
2023-08-03 21:57:13 +02:00
|
|
|
|
2021-04-07 01:27:02 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--dry-run",
|
|
|
|
action="store_true",
|
|
|
|
help="Prints emails of the recipients and text of the email.",
|
|
|
|
)
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2023-12-13 22:53:47 +01:00
|
|
|
def handle(
|
|
|
|
self, *args: Any, dry_run: bool = False, admins_only: bool = False, **options: str
|
|
|
|
) -> None:
|
2021-12-15 02:09:12 +01:00
|
|
|
target_emails: List[str] = []
|
2023-08-03 22:20:37 +02:00
|
|
|
users: QuerySet[UserProfile] = UserProfile.objects.none()
|
2023-08-30 18:36:47 +02:00
|
|
|
add_context: Optional[Callable[[Dict[str, object], UserProfile], None]] = None
|
2021-12-15 02:09:12 +01:00
|
|
|
|
2019-11-16 01:29:16 +01:00
|
|
|
if options["entire_server"]:
|
2021-04-28 01:23:44 +02:00
|
|
|
users = UserProfile.objects.filter(
|
|
|
|
is_active=True, is_bot=False, is_mirror_dummy=False, realm__deactivated=False
|
|
|
|
)
|
2021-08-02 06:35:01 +02:00
|
|
|
elif options["marketing"]:
|
|
|
|
# Marketing email sent at most once to each email address for users
|
2021-08-05 19:14:22 +02:00
|
|
|
# who are recently active (!long_term_idle) users of the product.
|
|
|
|
users = UserProfile.objects.filter(
|
|
|
|
is_active=True,
|
|
|
|
is_bot=False,
|
|
|
|
is_mirror_dummy=False,
|
|
|
|
realm__deactivated=False,
|
|
|
|
enable_marketing_emails=True,
|
|
|
|
long_term_idle=False,
|
|
|
|
).distinct("delivery_email")
|
2023-08-03 23:22:21 +02:00
|
|
|
|
2023-08-30 18:36:47 +02:00
|
|
|
def add_marketing_unsubscribe(context: Dict[str, object], user: UserProfile) -> None:
|
2023-08-03 23:22:21 +02:00
|
|
|
context["unsubscribe_link"] = one_click_unsubscribe_link(user, "marketing")
|
|
|
|
|
|
|
|
add_context = add_marketing_unsubscribe
|
2021-12-15 02:09:12 +01:00
|
|
|
elif options["remote_servers"]:
|
|
|
|
from zilencer.models import RemoteZulipServer
|
|
|
|
|
|
|
|
target_emails = list(
|
2022-01-14 04:26:28 +01:00
|
|
|
set(
|
|
|
|
RemoteZulipServer.objects.filter(deactivated=False).values_list(
|
|
|
|
"contact_email", flat=True
|
|
|
|
)
|
|
|
|
)
|
2021-12-15 02:09:12 +01:00
|
|
|
)
|
2021-04-28 01:23:44 +02:00
|
|
|
elif options["all_sponsored_org_admins"]:
|
|
|
|
# Sends at most one copy to each email address, even if it
|
|
|
|
# is an administrator in several organizations.
|
|
|
|
sponsored_realms = Realm.objects.filter(
|
2021-10-18 23:28:17 +02:00
|
|
|
plan_type=Realm.PLAN_TYPE_STANDARD_FREE, deactivated=False
|
2021-04-28 01:23:44 +02:00
|
|
|
)
|
|
|
|
admin_roles = [UserProfile.ROLE_REALM_ADMINISTRATOR, UserProfile.ROLE_REALM_OWNER]
|
|
|
|
users = UserProfile.objects.filter(
|
|
|
|
is_active=True,
|
|
|
|
is_bot=False,
|
|
|
|
is_mirror_dummy=False,
|
|
|
|
role__in=admin_roles,
|
|
|
|
realm__deactivated=False,
|
|
|
|
realm__in=sponsored_realms,
|
|
|
|
).distinct("delivery_email")
|
2023-08-03 23:32:46 +02:00
|
|
|
elif options["json_file"]:
|
|
|
|
with open(options["json_file"]) as f:
|
2023-08-30 18:36:47 +02:00
|
|
|
user_data: Dict[str, Dict[str, object]] = orjson.loads(f.read())
|
2023-08-03 23:32:46 +02:00
|
|
|
users = UserProfile.objects.filter(id__in=[int(user_id) for user_id in user_data])
|
|
|
|
|
2023-08-30 18:36:47 +02:00
|
|
|
def add_context_from_dict(context: Dict[str, object], user: UserProfile) -> None:
|
2023-08-03 23:32:46 +02:00
|
|
|
context.update(user_data.get(str(user.id), {}))
|
|
|
|
|
|
|
|
add_context = add_context_from_dict
|
|
|
|
|
2019-11-16 01:29:16 +01:00
|
|
|
else:
|
|
|
|
realm = self.get_realm(options)
|
2023-08-03 21:57:13 +02:00
|
|
|
users = self.get_users(options, realm, is_bot=False)
|
2019-11-16 01:29:16 +01:00
|
|
|
|
2023-12-13 23:02:26 +01:00
|
|
|
if admins_only:
|
|
|
|
users = users.filter(is_realm_admin=True)
|
|
|
|
|
2021-05-19 22:42:49 +02:00
|
|
|
# Only email users who've agreed to the terms of service.
|
2021-12-07 02:23:24 +01:00
|
|
|
if settings.TERMS_OF_SERVICE_VERSION is not None:
|
2023-08-03 22:20:37 +02:00
|
|
|
users = users.exclude(
|
|
|
|
Q(tos_version=None) | Q(tos_version=UserProfile.TOS_VERSION_BEFORE_FIRST_LOGIN)
|
2021-08-02 16:11:19 +02:00
|
|
|
)
|
2023-08-03 23:22:21 +02:00
|
|
|
send_custom_email(
|
2023-12-13 22:53:47 +01:00
|
|
|
users,
|
|
|
|
target_emails=target_emails,
|
|
|
|
dry_run=dry_run,
|
|
|
|
options=options,
|
|
|
|
add_context=add_context,
|
2023-08-03 23:22:21 +02:00
|
|
|
)
|
2021-04-07 01:27:02 +02:00
|
|
|
|
2023-12-13 22:53:47 +01:00
|
|
|
if dry_run:
|
2021-04-07 01:27:02 +02:00
|
|
|
print("Would send the above email to:")
|
|
|
|
for user in users:
|
2021-04-28 01:25:56 +02:00
|
|
|
print(f" {user.delivery_email} ({user.realm.string_id})")
|
2021-12-15 08:18:47 +01:00
|
|
|
for email in target_emails:
|
|
|
|
print(f" {email}")
|