2020-02-05 09:03:11 +01:00
|
|
|
from typing import Any, Callable, Dict, List, Optional, Union
|
2020-01-17 16:01:00 +01:00
|
|
|
import datetime
|
2017-08-24 17:58:40 +02:00
|
|
|
|
2018-11-01 21:48:49 +01:00
|
|
|
from zerver.lib.topic import (
|
|
|
|
topic_match_sa,
|
|
|
|
)
|
2020-02-05 09:03:11 +01:00
|
|
|
from zerver.lib.timestamp import datetime_to_timestamp
|
2017-08-29 17:16:53 +02:00
|
|
|
from zerver.models import (
|
2017-08-30 02:19:34 +02:00
|
|
|
get_stream,
|
|
|
|
MutedTopic,
|
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
|
|
|
UserProfile,
|
2017-08-29 17:16:53 +02:00
|
|
|
)
|
|
|
|
from sqlalchemy.sql import (
|
|
|
|
and_,
|
|
|
|
column,
|
|
|
|
not_,
|
|
|
|
or_,
|
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
|
|
|
Selectable,
|
2017-08-29 17:16:53 +02:00
|
|
|
)
|
2017-08-24 17:58:40 +02:00
|
|
|
|
2020-01-17 16:01:00 +01:00
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
2020-02-05 09:03:11 +01:00
|
|
|
def get_topic_mutes(user_profile: UserProfile) -> List[List[Union[str, float]]]:
|
2017-08-30 02:19:34 +02:00
|
|
|
rows = MutedTopic.objects.filter(
|
|
|
|
user_profile=user_profile,
|
|
|
|
).values(
|
|
|
|
'stream__name',
|
2020-02-05 09:03:11 +01:00
|
|
|
'topic_name',
|
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
|
|
|
'date_muted',
|
2017-08-30 02:19:34 +02:00
|
|
|
)
|
|
|
|
return [
|
2020-02-05 09:03:11 +01:00
|
|
|
[row['stream__name'], row['topic_name'], datetime_to_timestamp(row['date_muted'])]
|
2017-08-30 02:19:34 +02:00
|
|
|
for row in rows
|
|
|
|
]
|
2017-08-24 17:58:40 +02:00
|
|
|
|
2020-01-17 16:01:00 +01:00
|
|
|
def set_topic_mutes(user_profile: UserProfile, muted_topics: List[List[str]],
|
|
|
|
date_muted: Optional[datetime.datetime]=None) -> None:
|
2017-08-30 02:19:34 +02:00
|
|
|
'''
|
|
|
|
This is only used in tests.
|
|
|
|
'''
|
|
|
|
|
|
|
|
MutedTopic.objects.filter(
|
|
|
|
user_profile=user_profile,
|
|
|
|
).delete()
|
|
|
|
|
2020-01-17 16:01:00 +01:00
|
|
|
if date_muted is None:
|
|
|
|
date_muted = timezone_now()
|
2017-08-30 02:19:34 +02:00
|
|
|
for stream_name, topic_name in muted_topics:
|
|
|
|
stream = get_stream(stream_name, user_profile.realm)
|
2020-02-18 17:25:43 +01:00
|
|
|
recipient_id = stream.recipient_id
|
2017-08-30 02:19:34 +02:00
|
|
|
|
|
|
|
add_topic_mute(
|
|
|
|
user_profile=user_profile,
|
|
|
|
stream_id=stream.id,
|
2020-02-18 17:25:43 +01:00
|
|
|
recipient_id=recipient_id,
|
2017-08-30 02:19:34 +02:00
|
|
|
topic_name=topic_name,
|
2020-01-17 16:01:00 +01:00
|
|
|
date_muted=date_muted,
|
2017-08-30 02:19:34 +02:00
|
|
|
)
|
|
|
|
|
2020-01-17 16:01:00 +01:00
|
|
|
def add_topic_mute(user_profile: UserProfile, stream_id: int, recipient_id: int, topic_name: str,
|
|
|
|
date_muted: Optional[datetime.datetime]=None) -> None:
|
|
|
|
if date_muted is None:
|
|
|
|
date_muted = timezone_now()
|
2017-08-30 02:19:34 +02:00
|
|
|
MutedTopic.objects.create(
|
|
|
|
user_profile=user_profile,
|
|
|
|
stream_id=stream_id,
|
|
|
|
recipient_id=recipient_id,
|
|
|
|
topic_name=topic_name,
|
2020-01-17 16:01:00 +01:00
|
|
|
date_muted=date_muted,
|
2017-08-30 02:19:34 +02:00
|
|
|
)
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def remove_topic_mute(user_profile: UserProfile, stream_id: int, topic_name: str) -> None:
|
2017-08-30 02:19:34 +02:00
|
|
|
row = MutedTopic.objects.get(
|
|
|
|
user_profile=user_profile,
|
|
|
|
stream_id=stream_id,
|
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
|
|
|
topic_name__iexact=topic_name,
|
2017-08-30 02:19:34 +02:00
|
|
|
)
|
|
|
|
row.delete()
|
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def topic_is_muted(user_profile: UserProfile, stream_id: int, topic_name: str) -> bool:
|
2017-08-30 02:19:34 +02:00
|
|
|
is_muted = MutedTopic.objects.filter(
|
|
|
|
user_profile=user_profile,
|
2017-10-05 15:36:44 +02:00
|
|
|
stream_id=stream_id,
|
2017-08-30 02:19:34 +02:00
|
|
|
topic_name__iexact=topic_name,
|
|
|
|
).exists()
|
2017-08-24 17:58:40 +02:00
|
|
|
return is_muted
|
2017-08-29 17:16:53 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def exclude_topic_mutes(conditions: List[Selectable],
|
|
|
|
user_profile: UserProfile,
|
|
|
|
stream_id: Optional[int]) -> List[Selectable]:
|
2017-08-30 02:19:34 +02:00
|
|
|
query = MutedTopic.objects.filter(
|
|
|
|
user_profile=user_profile,
|
|
|
|
)
|
2017-08-29 17:16:53 +02:00
|
|
|
|
2017-09-17 20:19:12 +02:00
|
|
|
if stream_id is not None:
|
2017-08-30 02:19:34 +02:00
|
|
|
# If we are narrowed to a stream, we can optimize the query
|
|
|
|
# by not considering topic mutes outside the stream.
|
2017-09-17 20:19:12 +02:00
|
|
|
query = query.filter(stream_id=stream_id)
|
2017-08-29 17:16:53 +02:00
|
|
|
|
2017-08-30 02:19:34 +02:00
|
|
|
query = query.values(
|
|
|
|
'recipient_id',
|
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
|
|
|
'topic_name',
|
2017-08-30 02:19:34 +02:00
|
|
|
)
|
|
|
|
rows = list(query)
|
2017-08-29 17:16:53 +02:00
|
|
|
|
2017-08-30 02:19:34 +02:00
|
|
|
if not rows:
|
2017-08-29 17:16:53 +02:00
|
|
|
return conditions
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def mute_cond(row: Dict[str, Any]) -> Selectable:
|
2017-08-30 02:19:34 +02:00
|
|
|
recipient_id = row['recipient_id']
|
|
|
|
topic_name = row['topic_name']
|
|
|
|
stream_cond = column("recipient_id") == recipient_id
|
2018-11-01 21:48:49 +01:00
|
|
|
topic_cond = topic_match_sa(topic_name)
|
2017-08-29 17:16:53 +02:00
|
|
|
return and_(stream_cond, topic_cond)
|
|
|
|
|
2017-08-30 02:19:34 +02:00
|
|
|
condition = not_(or_(*list(map(mute_cond, rows))))
|
2017-08-29 17:16:53 +02:00
|
|
|
return conditions + [condition]
|
2017-08-29 18:12:15 +02:00
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def build_topic_mute_checker(user_profile: UserProfile) -> Callable[[int, str], bool]:
|
2017-08-30 02:19:34 +02:00
|
|
|
rows = MutedTopic.objects.filter(
|
|
|
|
user_profile=user_profile,
|
|
|
|
).values(
|
|
|
|
'recipient_id',
|
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
|
|
|
'topic_name',
|
2017-08-30 02:19:34 +02:00
|
|
|
)
|
|
|
|
rows = list(rows)
|
|
|
|
|
2017-08-29 18:12:15 +02:00
|
|
|
tups = set()
|
|
|
|
for row in rows:
|
2017-08-30 02:19:34 +02:00
|
|
|
recipient_id = row['recipient_id']
|
|
|
|
topic_name = row['topic_name']
|
|
|
|
tups.add((recipient_id, topic_name.lower()))
|
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def is_muted(recipient_id: int, topic: str) -> bool:
|
2017-08-30 02:19:34 +02:00
|
|
|
return (recipient_id, topic.lower()) in tups
|
2017-08-29 18:12:15 +02:00
|
|
|
|
|
|
|
return is_muted
|