2017-09-03 17:35:53 +02:00
|
|
|
import logging
|
2017-11-16 00:43:27 +01:00
|
|
|
from argparse import ArgumentParser
|
2018-05-10 19:30:04 +02:00
|
|
|
from typing import Any, List, Optional
|
2017-08-15 15:11:58 +02:00
|
|
|
|
2017-09-03 20:24:56 +02:00
|
|
|
from django.db import connection
|
2017-08-15 15:11:58 +02:00
|
|
|
|
2017-09-01 13:15:32 +02:00
|
|
|
from zerver.lib.fix_unreads import fix
|
2020-01-14 21:59:46 +01:00
|
|
|
from zerver.lib.management import CommandError, ZulipBaseCommand
|
2017-11-16 00:43:27 +01:00
|
|
|
from zerver.models import Realm, UserProfile
|
2017-08-15 15:11:58 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
logging.getLogger("zulip.fix_unreads").setLevel(logging.INFO)
|
2017-09-03 17:35:53 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-08-15 15:11:58 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
|
|
|
help = """Fix problems related to unread counts."""
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"emails", metavar="<emails>", nargs="*", help="email address to spelunk"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
parser.add_argument("--all", action="store_true", help="fix all users in specified realm")
|
2017-08-15 15:11:58 +02:00
|
|
|
self.add_realm_args(parser)
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def fix_all_users(self, realm: Realm) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
user_profiles = list(
|
|
|
|
UserProfile.objects.filter(
|
|
|
|
realm=realm,
|
|
|
|
is_bot=False,
|
|
|
|
)
|
|
|
|
)
|
2017-08-15 18:32:42 +02:00
|
|
|
for user_profile in user_profiles:
|
|
|
|
fix(user_profile)
|
2017-09-03 20:24:56 +02:00
|
|
|
connection.commit()
|
2017-08-15 18:32:42 +02:00
|
|
|
|
2018-05-10 19:30:04 +02:00
|
|
|
def fix_emails(self, realm: Optional[Realm], emails: List[str]) -> None:
|
2017-08-15 18:32:42 +02:00
|
|
|
|
|
|
|
for email in emails:
|
|
|
|
try:
|
|
|
|
user_profile = self.get_user(email, realm)
|
|
|
|
except CommandError:
|
2020-06-10 06:41:04 +02:00
|
|
|
print(f"e-mail {email} doesn't exist in the realm {realm}, skipping")
|
2017-08-15 18:32:42 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
fix(user_profile)
|
2017-09-03 20:24:56 +02:00
|
|
|
connection.commit()
|
2017-08-15 18:32:42 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2017-08-15 15:11:58 +02:00
|
|
|
realm = self.get_realm(options)
|
2017-08-15 18:32:42 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if options["all"]:
|
2017-08-15 18:32:42 +02:00
|
|
|
if realm is None:
|
2021-02-12 08:20:45 +01:00
|
|
|
raise CommandError("You must specify a realm if you choose the --all option.")
|
2017-08-15 18:32:42 +02:00
|
|
|
|
|
|
|
self.fix_all_users(realm)
|
2017-08-15 15:11:58 +02:00
|
|
|
return
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
self.fix_emails(realm, options["emails"])
|