2017-08-15 15:11:58 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2017-08-15 18:32:42 +02:00
|
|
|
import sys
|
2017-08-15 15:11:58 +02:00
|
|
|
import time
|
|
|
|
import ujson
|
|
|
|
|
2017-08-15 18:30:22 +02:00
|
|
|
from typing import Any, Callable, Dict, List, Set, Text, TypeVar
|
|
|
|
from psycopg2.extensions import cursor
|
|
|
|
CursorObj = TypeVar('CursorObj', bound=cursor)
|
2017-08-15 15:11:58 +02:00
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
from django.core.management.base import CommandError
|
|
|
|
from django.db import connection
|
|
|
|
|
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
|
|
|
from zerver.models import (
|
2017-08-15 18:32:42 +02:00
|
|
|
Realm,
|
2017-08-15 16:24:04 +02:00
|
|
|
Stream,
|
2017-08-15 15:11:58 +02:00
|
|
|
UserProfile
|
|
|
|
)
|
|
|
|
|
2017-08-23 22:10:38 +02:00
|
|
|
def update_unread_flags(cursor, user_message_ids):
|
|
|
|
# type: (CursorObj, List[int]) -> None
|
|
|
|
um_id_list = ', '.join(str(id) for id in user_message_ids)
|
|
|
|
query = '''
|
|
|
|
UPDATE zerver_usermessage
|
|
|
|
SET flags = flags | 1
|
|
|
|
WHERE id IN (%s)
|
|
|
|
''' % (um_id_list,)
|
|
|
|
|
|
|
|
cursor.execute(query)
|
|
|
|
|
|
|
|
|
2017-08-15 15:11:58 +02:00
|
|
|
def get_timing(message, f):
|
|
|
|
# type: (str, Callable) -> None
|
|
|
|
start = time.time()
|
|
|
|
print(message)
|
|
|
|
f()
|
|
|
|
elapsed = time.time() - start
|
|
|
|
print('elapsed time: %.03f\n' % (elapsed,))
|
|
|
|
|
|
|
|
|
2017-08-15 18:30:22 +02:00
|
|
|
def fix_unsubscribed(cursor, user_profile):
|
|
|
|
# type: (CursorObj, UserProfile) -> None
|
2017-08-15 15:11:58 +02:00
|
|
|
|
|
|
|
recipient_ids = []
|
|
|
|
|
|
|
|
def find_recipients():
|
|
|
|
# type: () -> None
|
|
|
|
query = '''
|
|
|
|
SELECT
|
|
|
|
zerver_subscription.recipient_id
|
|
|
|
FROM
|
|
|
|
zerver_subscription
|
|
|
|
INNER JOIN zerver_recipient ON (
|
|
|
|
zerver_recipient.id = zerver_subscription.recipient_id
|
|
|
|
)
|
|
|
|
WHERE (
|
|
|
|
zerver_subscription.user_profile_id = '%s' AND
|
|
|
|
zerver_recipient.type = 2 AND
|
|
|
|
(NOT zerver_subscription.active)
|
|
|
|
)
|
|
|
|
'''
|
|
|
|
cursor.execute(query, [user_profile.id])
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
for row in rows:
|
|
|
|
recipient_ids.append(row[0])
|
|
|
|
print(recipient_ids)
|
|
|
|
|
|
|
|
get_timing(
|
|
|
|
'get recipients',
|
|
|
|
find_recipients
|
|
|
|
)
|
|
|
|
|
|
|
|
if not recipient_ids:
|
|
|
|
return
|
|
|
|
|
|
|
|
user_message_ids = []
|
|
|
|
|
|
|
|
def find():
|
|
|
|
# type: () -> None
|
|
|
|
recips = ', '.join(str(id) for id in recipient_ids)
|
|
|
|
|
|
|
|
query = '''
|
|
|
|
SELECT
|
|
|
|
zerver_usermessage.id
|
|
|
|
FROM
|
|
|
|
zerver_usermessage
|
|
|
|
INNER JOIN zerver_message ON (
|
|
|
|
zerver_message.id = zerver_usermessage.message_id
|
|
|
|
)
|
|
|
|
WHERE (
|
|
|
|
zerver_usermessage.user_profile_id = %s AND
|
|
|
|
(zerver_usermessage.flags & 1) = 0 AND
|
|
|
|
zerver_message.recipient_id in (%s)
|
|
|
|
)
|
|
|
|
''' % (user_profile.id, recips)
|
|
|
|
|
|
|
|
print('''
|
|
|
|
EXPLAIN analyze''' + query.rstrip() + ';')
|
|
|
|
|
|
|
|
cursor.execute(query)
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
for row in rows:
|
|
|
|
user_message_ids.append(row[0])
|
|
|
|
print('rows found: %d' % (len(user_message_ids),))
|
|
|
|
|
|
|
|
get_timing(
|
|
|
|
'finding unread messages for non-active streams',
|
|
|
|
find
|
|
|
|
)
|
|
|
|
|
|
|
|
if not user_message_ids:
|
|
|
|
return
|
|
|
|
|
|
|
|
def fix():
|
|
|
|
# type: () -> None
|
2017-08-23 22:10:38 +02:00
|
|
|
update_unread_flags(cursor, user_message_ids)
|
2017-08-15 15:11:58 +02:00
|
|
|
|
|
|
|
get_timing(
|
|
|
|
'fixing unread messages for non-active streams',
|
|
|
|
fix
|
|
|
|
)
|
|
|
|
|
2017-08-15 16:24:04 +02:00
|
|
|
def build_topic_mute_checker(user_profile):
|
|
|
|
# type: (UserProfile) -> Callable[[int, Text], bool]
|
|
|
|
rows = ujson.loads(user_profile.muted_topics)
|
|
|
|
stream_names = {row[0] for row in rows}
|
|
|
|
stream_dict = dict()
|
|
|
|
for name in stream_names:
|
|
|
|
stream_id = Stream.objects.get(
|
|
|
|
name__iexact=name.strip(),
|
|
|
|
realm_id=user_profile.realm_id,
|
|
|
|
).id
|
|
|
|
stream_dict[name] = stream_id
|
|
|
|
tups = set()
|
|
|
|
for row in rows:
|
|
|
|
stream_name = row[0]
|
|
|
|
topic = row[1]
|
|
|
|
stream_id = stream_dict[stream_name]
|
|
|
|
tups.add((stream_id, topic.lower()))
|
|
|
|
|
|
|
|
def is_muted(stream_id, topic):
|
|
|
|
# type: (int, Text) -> bool
|
|
|
|
return (stream_id, topic.lower()) in tups
|
|
|
|
|
|
|
|
return is_muted
|
|
|
|
|
2017-08-15 18:30:22 +02:00
|
|
|
def fix_pre_pointer(cursor, user_profile):
|
|
|
|
# type: (CursorObj, UserProfile) -> None
|
2017-08-15 16:24:04 +02:00
|
|
|
|
|
|
|
pointer = user_profile.pointer
|
|
|
|
|
|
|
|
if not pointer:
|
|
|
|
return
|
|
|
|
|
|
|
|
is_topic_muted = build_topic_mute_checker(user_profile)
|
|
|
|
|
|
|
|
recipient_ids = []
|
|
|
|
|
|
|
|
def find_non_muted_recipients():
|
|
|
|
# type: () -> None
|
|
|
|
query = '''
|
|
|
|
SELECT
|
|
|
|
zerver_subscription.recipient_id
|
|
|
|
FROM
|
|
|
|
zerver_subscription
|
|
|
|
INNER JOIN zerver_recipient ON (
|
|
|
|
zerver_recipient.id = zerver_subscription.recipient_id
|
|
|
|
)
|
|
|
|
WHERE (
|
|
|
|
zerver_subscription.user_profile_id = '%s' AND
|
|
|
|
zerver_recipient.type = 2 AND
|
|
|
|
zerver_subscription.in_home_view AND
|
|
|
|
zerver_subscription.active
|
|
|
|
)
|
|
|
|
'''
|
|
|
|
cursor.execute(query, [user_profile.id])
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
for row in rows:
|
|
|
|
recipient_ids.append(row[0])
|
|
|
|
print(recipient_ids)
|
|
|
|
|
|
|
|
get_timing(
|
|
|
|
'find_non_muted_recipients',
|
|
|
|
find_non_muted_recipients
|
|
|
|
)
|
|
|
|
|
2017-08-24 02:15:18 +02:00
|
|
|
if not recipient_ids:
|
|
|
|
return
|
|
|
|
|
2017-08-15 16:24:04 +02:00
|
|
|
user_message_ids = []
|
|
|
|
|
|
|
|
def find_old_ids():
|
|
|
|
# type: () -> None
|
|
|
|
recips = ', '.join(str(id) for id in recipient_ids)
|
|
|
|
|
|
|
|
query = '''
|
|
|
|
SELECT
|
|
|
|
zerver_usermessage.id,
|
|
|
|
zerver_recipient.type_id,
|
|
|
|
subject
|
|
|
|
FROM
|
|
|
|
zerver_usermessage
|
|
|
|
INNER JOIN zerver_message ON (
|
|
|
|
zerver_message.id = zerver_usermessage.message_id
|
|
|
|
)
|
|
|
|
INNER JOIN zerver_recipient ON (
|
|
|
|
zerver_recipient.id = zerver_message.recipient_id
|
|
|
|
)
|
|
|
|
WHERE (
|
|
|
|
zerver_usermessage.user_profile_id = %s AND
|
|
|
|
zerver_message.id <= %s AND
|
|
|
|
(zerver_usermessage.flags & 1) = 0 AND
|
|
|
|
zerver_message.recipient_id in (%s)
|
|
|
|
)
|
|
|
|
''' % (user_profile.id, pointer, recips)
|
|
|
|
|
|
|
|
print('''
|
|
|
|
EXPLAIN analyze''' + query.rstrip() + ';')
|
|
|
|
|
|
|
|
cursor.execute(query)
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
for (um_id, stream_id, topic) in rows:
|
|
|
|
if not is_topic_muted(stream_id, topic):
|
|
|
|
user_message_ids.append(um_id)
|
|
|
|
print('rows found: %d' % (len(user_message_ids),))
|
|
|
|
|
|
|
|
get_timing(
|
|
|
|
'finding pre-pointer messages that are not muted',
|
|
|
|
find_old_ids
|
|
|
|
)
|
|
|
|
|
2017-08-23 22:10:38 +02:00
|
|
|
if not user_message_ids:
|
|
|
|
return
|
|
|
|
|
|
|
|
def fix():
|
|
|
|
# type: () -> None
|
|
|
|
update_unread_flags(cursor, user_message_ids)
|
|
|
|
|
|
|
|
get_timing(
|
|
|
|
'fixing unread messages for pre-pointer non-muted messages',
|
|
|
|
fix
|
|
|
|
)
|
|
|
|
|
2017-08-15 16:24:04 +02:00
|
|
|
def fix(user_profile):
|
|
|
|
# type: (UserProfile) -> None
|
2017-08-15 18:30:22 +02:00
|
|
|
print('\n---\nFixing %s:' % (user_profile.email,))
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
fix_unsubscribed(cursor, user_profile)
|
|
|
|
fix_pre_pointer(cursor, user_profile)
|
|
|
|
connection.commit()
|
2017-08-15 16:24:04 +02:00
|
|
|
|
2017-08-15 15:11:58 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
|
|
|
help = """Fix problems related to unread counts."""
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
# type: (ArgumentParser) -> None
|
2017-08-15 18:32:42 +02:00
|
|
|
parser.add_argument('emails',
|
|
|
|
metavar='<emails>',
|
|
|
|
type=str,
|
|
|
|
nargs='*',
|
2017-08-15 15:11:58 +02:00
|
|
|
help='email address to spelunk')
|
2017-08-15 18:32:42 +02:00
|
|
|
parser.add_argument('--all',
|
|
|
|
action='store_true',
|
|
|
|
dest='all',
|
|
|
|
default=False,
|
|
|
|
help='fix all users in specified realm')
|
2017-08-15 15:11:58 +02:00
|
|
|
self.add_realm_args(parser)
|
|
|
|
|
2017-08-15 18:32:42 +02:00
|
|
|
def fix_all_users(self, realm):
|
|
|
|
# type: (Realm) -> None
|
|
|
|
user_profiles = list(UserProfile.objects.filter(
|
|
|
|
realm=realm,
|
|
|
|
is_bot=False
|
|
|
|
))
|
|
|
|
for user_profile in user_profiles:
|
|
|
|
fix(user_profile)
|
|
|
|
|
|
|
|
def fix_emails(self, realm, emails):
|
|
|
|
# type: (Realm, List[Text]) -> None
|
|
|
|
|
|
|
|
for email in emails:
|
|
|
|
try:
|
|
|
|
user_profile = self.get_user(email, realm)
|
|
|
|
except CommandError:
|
|
|
|
print("e-mail %s doesn't exist in the realm %s, skipping" % (email, realm))
|
|
|
|
return
|
|
|
|
|
|
|
|
fix(user_profile)
|
|
|
|
|
2017-08-15 15:11:58 +02:00
|
|
|
def handle(self, *args, **options):
|
2017-08-23 02:31:55 +02:00
|
|
|
# type: (*Any, **Any) -> None
|
2017-08-15 15:11:58 +02:00
|
|
|
realm = self.get_realm(options)
|
2017-08-15 18:32:42 +02:00
|
|
|
|
|
|
|
if options['all']:
|
|
|
|
if realm is None:
|
|
|
|
print('You must specify a realm if you choose the --all option.')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
self.fix_all_users(realm)
|
2017-08-15 15:11:58 +02:00
|
|
|
return
|
|
|
|
|
2017-08-15 18:32:42 +02:00
|
|
|
self.fix_emails(realm, options['emails'])
|