2013-03-15 20:07:38 +01:00
|
|
|
import logging
|
|
|
|
import sys
|
2017-11-16 00:43:27 +01:00
|
|
|
from typing import Any, Iterable
|
2013-03-15 20:07:38 +01:00
|
|
|
|
2017-07-08 21:19:48 +02:00
|
|
|
from django.core.management.base import CommandParser
|
2017-11-16 00:43:27 +01:00
|
|
|
from django.db import models
|
2013-03-15 20:07:38 +01:00
|
|
|
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib import utils
|
2020-01-14 21:59:46 +01:00
|
|
|
from zerver.lib.management import CommandError, ZulipBaseCommand
|
2017-07-08 21:19:48 +02:00
|
|
|
from zerver.models import UserMessage
|
2013-03-15 20:07:38 +01:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-07-08 21:19:48 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2013-03-15 20:07:38 +01:00
|
|
|
help = """Sets user message flags. Used internally by actions.py. Marks all
|
|
|
|
Expects a comma-delimited list of user message ids via stdin, and an EOF to terminate."""
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"-l",
|
|
|
|
"--for-real",
|
|
|
|
action="store_true",
|
2021-02-12 08:19:30 +01:00
|
|
|
help="Actually change message flags. Default is a dry run.",
|
|
|
|
)
|
2016-11-03 10:22:19 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
parser.add_argument("-f", "--flag", help="The flag to add of remove")
|
2016-11-03 10:22:19 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
parser.add_argument("-o", "--op", help="The operation to do: 'add' or 'remove'")
|
2016-11-03 10:22:19 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"-u", "--until", dest="all_until", help="Mark all messages <= specific usermessage id"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2016-11-03 10:22:19 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
parser.add_argument("-m", "--email", help="Email to set messages for")
|
2017-07-08 21:19:48 +02:00
|
|
|
self.add_realm_args(parser)
|
2013-03-15 20:07:38 +01:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2013-03-15 20:07:38 +01:00
|
|
|
if not options["flag"] or not options["op"] or not options["email"]:
|
2019-05-03 23:20:39 +02:00
|
|
|
raise CommandError("Please specify an operation, a flag and an email")
|
2013-03-15 20:07:38 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
op = options["op"]
|
|
|
|
flag = getattr(UserMessage.flags, options["flag"])
|
|
|
|
all_until = options["all_until"]
|
|
|
|
email = options["email"]
|
2013-03-15 20:07:38 +01:00
|
|
|
|
2017-07-08 21:19:48 +02:00
|
|
|
realm = self.get_realm(options)
|
|
|
|
user_profile = self.get_user(email, realm)
|
2013-03-15 20:07:38 +01:00
|
|
|
|
2013-03-21 22:32:13 +01:00
|
|
|
if all_until:
|
|
|
|
filt = models.Q(id__lte=all_until)
|
2013-03-15 20:07:38 +01:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
filt = models.Q(message__id__in=[mid.strip() for mid in sys.stdin.read().split(",")])
|
2021-02-12 08:19:30 +01:00
|
|
|
mids = [
|
|
|
|
m.id
|
2021-02-12 08:20:45 +01:00
|
|
|
for m in UserMessage.objects.filter(filt, user_profile=user_profile).order_by("-id")
|
2021-02-12 08:19:30 +01:00
|
|
|
]
|
2013-03-21 22:32:13 +01:00
|
|
|
|
2013-03-15 20:07:38 +01:00
|
|
|
if options["for_real"]:
|
|
|
|
sys.stdin.close()
|
|
|
|
sys.stdout.close()
|
|
|
|
sys.stderr.close()
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def do_update(batch: Iterable[int]) -> None:
|
2013-11-01 19:06:45 +01:00
|
|
|
msgs = UserMessage.objects.filter(id__in=batch)
|
2021-02-12 08:20:45 +01:00
|
|
|
if op == "add":
|
|
|
|
msgs.update(flags=models.F("flags").bitor(flag))
|
|
|
|
elif op == "remove":
|
|
|
|
msgs.update(flags=models.F("flags").bitand(~flag))
|
2013-03-15 20:07:38 +01:00
|
|
|
|
|
|
|
if not options["for_real"]:
|
2020-05-02 08:44:14 +02:00
|
|
|
logging.info("Updating %s by %s %s", mids, op, flag)
|
2013-03-15 20:07:38 +01:00
|
|
|
logging.info("Dry run completed. Run with --for-real to change message flags.")
|
2019-05-03 23:20:39 +02:00
|
|
|
raise CommandError
|
2013-03-15 20:07:38 +01:00
|
|
|
|
|
|
|
utils.run_in_batches(mids, 400, do_update, sleep_time=3)
|
|
|
|
exit(0)
|