2017-09-01 13:15:32 +02:00
|
|
|
import time
|
2017-09-03 17:35:53 +02:00
|
|
|
import logging
|
2017-09-01 13:15:32 +02:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
from typing import Callable, List, TypeVar
|
2017-09-01 13:15:32 +02:00
|
|
|
from psycopg2.extensions import cursor
|
2020-06-09 11:57:01 +02:00
|
|
|
from psycopg2.sql import SQL
|
|
|
|
|
2017-09-01 13:15:32 +02:00
|
|
|
CursorObj = TypeVar('CursorObj', bound=cursor)
|
|
|
|
|
|
|
|
from django.db import connection
|
|
|
|
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2017-09-04 17:31:50 +02:00
|
|
|
'''
|
|
|
|
NOTE! Be careful modifying this library, as it is used
|
|
|
|
in a migration, and it needs to be valid for the state
|
|
|
|
of the database that is in place when the 0104_fix_unreads
|
|
|
|
migration runs.
|
|
|
|
'''
|
|
|
|
|
2017-09-03 17:35:53 +02:00
|
|
|
logger = logging.getLogger('zulip.fix_unreads')
|
|
|
|
logger.setLevel(logging.WARNING)
|
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def build_topic_mute_checker(cursor: CursorObj, user_profile: UserProfile) -> Callable[[int, str], bool]:
|
2017-09-03 20:09:19 +02:00
|
|
|
'''
|
|
|
|
This function is similar to the function of the same name
|
|
|
|
in zerver/lib/topic_mutes.py, but it works without the ORM,
|
|
|
|
so that we can use it in migrations.
|
|
|
|
'''
|
2020-06-09 11:57:01 +02:00
|
|
|
query = SQL('''
|
2017-09-03 20:09:19 +02:00
|
|
|
SELECT
|
|
|
|
recipient_id,
|
|
|
|
topic_name
|
|
|
|
FROM
|
|
|
|
zerver_mutedtopic
|
|
|
|
WHERE
|
|
|
|
user_profile_id = %s
|
2020-06-09 11:57:01 +02:00
|
|
|
''')
|
2017-09-03 20:09:19 +02:00
|
|
|
cursor.execute(query, [user_profile.id])
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
|
|
|
|
tups = {
|
|
|
|
(recipient_id, topic_name.lower())
|
|
|
|
for (recipient_id, topic_name) in rows
|
|
|
|
}
|
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def is_muted(recipient_id: int, topic: str) -> bool:
|
2017-09-03 20:09:19 +02:00
|
|
|
return (recipient_id, topic.lower()) in tups
|
|
|
|
|
|
|
|
return is_muted
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def update_unread_flags(cursor: CursorObj, user_message_ids: List[int]) -> None:
|
2020-06-09 11:57:01 +02:00
|
|
|
query = SQL('''
|
2017-09-01 13:15:32 +02:00
|
|
|
UPDATE zerver_usermessage
|
|
|
|
SET flags = flags | 1
|
2020-06-09 11:57:01 +02:00
|
|
|
WHERE id IN %(user_message_ids)s
|
|
|
|
''')
|
2017-09-01 13:15:32 +02:00
|
|
|
|
2020-06-09 11:57:01 +02:00
|
|
|
cursor.execute(query, {"user_message_ids": tuple(user_message_ids)})
|
2017-09-01 13:15:32 +02:00
|
|
|
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def get_timing(message: str, f: Callable[[], None]) -> None:
|
2017-09-01 13:15:32 +02:00
|
|
|
start = time.time()
|
2017-09-03 17:35:53 +02:00
|
|
|
logger.info(message)
|
2017-09-01 13:15:32 +02:00
|
|
|
f()
|
|
|
|
elapsed = time.time() - start
|
2020-05-02 08:44:14 +02:00
|
|
|
logger.info('elapsed time: %.03f\n', elapsed)
|
2017-09-01 13:15:32 +02:00
|
|
|
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def fix_unsubscribed(cursor: CursorObj, user_profile: UserProfile) -> None:
|
2017-09-01 13:15:32 +02:00
|
|
|
|
|
|
|
recipient_ids = []
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def find_recipients() -> None:
|
2020-06-09 11:57:01 +02:00
|
|
|
query = SQL('''
|
2017-09-01 13:15:32 +02:00
|
|
|
SELECT
|
|
|
|
zerver_subscription.recipient_id
|
|
|
|
FROM
|
|
|
|
zerver_subscription
|
|
|
|
INNER JOIN zerver_recipient ON (
|
|
|
|
zerver_recipient.id = zerver_subscription.recipient_id
|
|
|
|
)
|
|
|
|
WHERE (
|
2020-06-09 11:57:01 +02:00
|
|
|
zerver_subscription.user_profile_id = %(user_profile_id)s AND
|
2017-09-01 13:15:32 +02:00
|
|
|
zerver_recipient.type = 2 AND
|
|
|
|
(NOT zerver_subscription.active)
|
|
|
|
)
|
2020-06-09 11:57:01 +02:00
|
|
|
''')
|
|
|
|
cursor.execute(query, {"user_profile_id": user_profile.id})
|
2017-09-01 13:15:32 +02:00
|
|
|
rows = cursor.fetchall()
|
|
|
|
for row in rows:
|
|
|
|
recipient_ids.append(row[0])
|
2017-09-03 17:35:53 +02:00
|
|
|
logger.info(str(recipient_ids))
|
2017-09-01 13:15:32 +02:00
|
|
|
|
|
|
|
get_timing(
|
|
|
|
'get recipients',
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
find_recipients,
|
2017-09-01 13:15:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if not recipient_ids:
|
|
|
|
return
|
|
|
|
|
|
|
|
user_message_ids = []
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def find() -> None:
|
2020-06-09 11:57:01 +02:00
|
|
|
query = SQL('''
|
2017-09-01 13:15:32 +02:00
|
|
|
SELECT
|
|
|
|
zerver_usermessage.id
|
|
|
|
FROM
|
|
|
|
zerver_usermessage
|
|
|
|
INNER JOIN zerver_message ON (
|
|
|
|
zerver_message.id = zerver_usermessage.message_id
|
|
|
|
)
|
|
|
|
WHERE (
|
2020-06-09 11:57:01 +02:00
|
|
|
zerver_usermessage.user_profile_id = %(user_profile_id)s AND
|
2017-09-01 13:15:32 +02:00
|
|
|
(zerver_usermessage.flags & 1) = 0 AND
|
2020-06-09 11:57:01 +02:00
|
|
|
zerver_message.recipient_id in %(recipient_ids)s
|
2017-09-01 13:15:32 +02:00
|
|
|
)
|
2020-06-09 11:57:01 +02:00
|
|
|
''')
|
2017-09-01 13:15:32 +02:00
|
|
|
|
2020-06-09 11:57:01 +02:00
|
|
|
cursor.execute(query, {
|
|
|
|
"user_profile_id": user_profile.id,
|
|
|
|
"recipient_ids": tuple(recipient_ids),
|
|
|
|
})
|
2017-09-01 13:15:32 +02:00
|
|
|
rows = cursor.fetchall()
|
|
|
|
for row in rows:
|
|
|
|
user_message_ids.append(row[0])
|
2020-05-02 08:44:14 +02:00
|
|
|
logger.info('rows found: %d', len(user_message_ids))
|
2017-09-01 13:15:32 +02:00
|
|
|
|
|
|
|
get_timing(
|
|
|
|
'finding unread messages for non-active streams',
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
find,
|
2017-09-01 13:15:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if not user_message_ids:
|
|
|
|
return
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def fix() -> None:
|
2017-09-01 13:15:32 +02:00
|
|
|
update_unread_flags(cursor, user_message_ids)
|
|
|
|
|
|
|
|
get_timing(
|
|
|
|
'fixing unread messages for non-active streams',
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
fix,
|
2017-09-01 13:15:32 +02:00
|
|
|
)
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def fix_pre_pointer(cursor: CursorObj, user_profile: UserProfile) -> None:
|
2017-09-01 13:15:32 +02:00
|
|
|
|
|
|
|
pointer = user_profile.pointer
|
|
|
|
|
|
|
|
if not pointer:
|
|
|
|
return
|
|
|
|
|
|
|
|
recipient_ids = []
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def find_non_muted_recipients() -> None:
|
2020-06-09 11:57:01 +02:00
|
|
|
query = SQL('''
|
2017-09-01 13:15:32 +02:00
|
|
|
SELECT
|
|
|
|
zerver_subscription.recipient_id
|
|
|
|
FROM
|
|
|
|
zerver_subscription
|
|
|
|
INNER JOIN zerver_recipient ON (
|
|
|
|
zerver_recipient.id = zerver_subscription.recipient_id
|
|
|
|
)
|
|
|
|
WHERE (
|
2020-06-09 11:57:01 +02:00
|
|
|
zerver_subscription.user_profile_id = %(user_profile_id)s AND
|
2017-09-01 13:15:32 +02:00
|
|
|
zerver_recipient.type = 2 AND
|
2018-08-02 23:46:05 +02:00
|
|
|
(NOT zerver_subscription.is_muted) AND
|
2017-09-01 13:15:32 +02:00
|
|
|
zerver_subscription.active
|
|
|
|
)
|
2020-06-09 11:57:01 +02:00
|
|
|
''')
|
|
|
|
cursor.execute(query, {"user_profile_id": user_profile.id})
|
2017-09-01 13:15:32 +02:00
|
|
|
rows = cursor.fetchall()
|
|
|
|
for row in rows:
|
|
|
|
recipient_ids.append(row[0])
|
2017-09-03 17:35:53 +02:00
|
|
|
logger.info(str(recipient_ids))
|
2017-09-01 13:15:32 +02:00
|
|
|
|
|
|
|
get_timing(
|
|
|
|
'find_non_muted_recipients',
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
find_non_muted_recipients,
|
2017-09-01 13:15:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if not recipient_ids:
|
|
|
|
return
|
|
|
|
|
|
|
|
user_message_ids = []
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def find_old_ids() -> None:
|
2017-09-03 20:09:19 +02:00
|
|
|
is_topic_muted = build_topic_mute_checker(cursor, user_profile)
|
2017-09-01 13:15:32 +02:00
|
|
|
|
2020-06-09 11:57:01 +02:00
|
|
|
query = SQL('''
|
2017-09-01 13:15:32 +02:00
|
|
|
SELECT
|
|
|
|
zerver_usermessage.id,
|
|
|
|
zerver_message.recipient_id,
|
|
|
|
zerver_message.subject
|
|
|
|
FROM
|
|
|
|
zerver_usermessage
|
|
|
|
INNER JOIN zerver_message ON (
|
|
|
|
zerver_message.id = zerver_usermessage.message_id
|
|
|
|
)
|
|
|
|
WHERE (
|
2020-06-09 11:57:01 +02:00
|
|
|
zerver_usermessage.user_profile_id = %(user_profile_id)s AND
|
|
|
|
zerver_usermessage.message_id <= %(pointer)s AND
|
2017-09-01 13:15:32 +02:00
|
|
|
(zerver_usermessage.flags & 1) = 0 AND
|
2020-06-09 11:57:01 +02:00
|
|
|
zerver_message.recipient_id in %(recipient_ids)s
|
2017-09-01 13:15:32 +02:00
|
|
|
)
|
2020-06-09 11:57:01 +02:00
|
|
|
''')
|
2017-09-01 13:15:32 +02:00
|
|
|
|
2020-06-09 11:57:01 +02:00
|
|
|
cursor.execute(query, {
|
|
|
|
"user_profile_id": user_profile.id,
|
|
|
|
"pointer": pointer,
|
|
|
|
"recipient_ids": tuple(recipient_ids),
|
|
|
|
})
|
2017-09-01 13:15:32 +02:00
|
|
|
rows = cursor.fetchall()
|
|
|
|
for (um_id, recipient_id, topic) in rows:
|
|
|
|
if not is_topic_muted(recipient_id, topic):
|
|
|
|
user_message_ids.append(um_id)
|
2020-05-02 08:44:14 +02:00
|
|
|
logger.info('rows found: %d', len(user_message_ids))
|
2017-09-01 13:15:32 +02:00
|
|
|
|
|
|
|
get_timing(
|
|
|
|
'finding pre-pointer messages that are not muted',
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
find_old_ids,
|
2017-09-01 13:15:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if not user_message_ids:
|
|
|
|
return
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def fix() -> None:
|
2017-09-01 13:15:32 +02:00
|
|
|
update_unread_flags(cursor, user_message_ids)
|
|
|
|
|
|
|
|
get_timing(
|
|
|
|
'fixing unread messages for pre-pointer non-muted messages',
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
fix,
|
2017-09-01 13:15:32 +02:00
|
|
|
)
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def fix(user_profile: UserProfile) -> None:
|
2020-05-02 08:44:14 +02:00
|
|
|
logger.info('\n---\nFixing %s:', user_profile.id)
|
2017-09-01 13:15:32 +02:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
fix_unsubscribed(cursor, user_profile)
|
|
|
|
fix_pre_pointer(cursor, user_profile)
|