2017-09-03 17:35:53 +02:00
|
|
|
import logging
|
2020-06-11 00:54:34 +02:00
|
|
|
import time
|
2021-08-18 19:40:01 +02:00
|
|
|
from typing import Callable, List
|
2017-09-01 13:15:32 +02:00
|
|
|
|
|
|
|
from django.db import connection
|
2021-08-18 19:40:01 +02:00
|
|
|
from django.db.backends.utils import CursorWrapper
|
|
|
|
from psycopg2.sql import SQL
|
2017-09-01 13:15:32 +02:00
|
|
|
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
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.
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
2017-09-04 17:31:50 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
logger = logging.getLogger("zulip.fix_unreads")
|
2017-09-03 17:35:53 +02:00
|
|
|
logger.setLevel(logging.WARNING)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-08-18 19:40:01 +02:00
|
|
|
def update_unread_flags(cursor: CursorWrapper, user_message_ids: List[int]) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
query = SQL(
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
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
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
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
|
2021-02-12 08:20:45 +01:00
|
|
|
logger.info("elapsed time: %.03f\n", elapsed)
|
2017-09-01 13:15:32 +02:00
|
|
|
|
|
|
|
|
2021-08-18 19:40:01 +02:00
|
|
|
def fix_unsubscribed(cursor: CursorWrapper, 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:
|
2021-02-12 08:19:30 +01:00
|
|
|
query = SQL(
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
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)
|
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
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])
|
2022-10-08 07:34:49 +02:00
|
|
|
logger.info("%s", recipient_ids)
|
2017-09-01 13:15:32 +02:00
|
|
|
|
|
|
|
get_timing(
|
2021-02-12 08:20:45 +01:00
|
|
|
"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:
|
2021-02-12 08:19:30 +01:00
|
|
|
query = SQL(
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
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
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
2021-02-12 08:19:30 +01: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])
|
2021-02-12 08:20:45 +01:00
|
|
|
logger.info("rows found: %d", len(user_message_ids))
|
2017-09-01 13:15:32 +02:00
|
|
|
|
|
|
|
get_timing(
|
2021-02-12 08:20:45 +01:00
|
|
|
"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(
|
2021-02-12 08:20:45 +01:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def fix(user_profile: UserProfile) -> None:
|
2021-02-12 08:20:45 +01: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)
|