2020-01-17 16:01:00 +01:00
|
|
|
import datetime
|
2022-06-23 22:54:31 +02:00
|
|
|
from typing import Callable, List, Optional, Tuple, TypedDict
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2022-03-18 01:19:16 +01:00
|
|
|
from django.db.models.query import QuerySet
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2020-11-16 22:52:27 +01:00
|
|
|
from sqlalchemy.sql import ClauseElement, and_, column, not_, or_
|
2021-08-21 01:07:28 +02:00
|
|
|
from sqlalchemy.types import Integer
|
2017-08-24 17:58:40 +02:00
|
|
|
|
2020-02-05 09:03:11 +01:00
|
|
|
from zerver.lib.timestamp import datetime_to_timestamp
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.topic import topic_match_sa
|
2022-02-22 21:14:28 +01:00
|
|
|
from zerver.lib.types import UserTopicDict
|
2021-07-23 15:26:02 +02:00
|
|
|
from zerver.models import UserProfile, UserTopic, get_stream
|
2017-08-24 17:58:40 +02:00
|
|
|
|
2020-01-17 16:01:00 +01:00
|
|
|
|
2022-02-22 21:14:28 +01:00
|
|
|
def get_user_topics(
|
|
|
|
user_profile: UserProfile,
|
|
|
|
include_deactivated: bool = False,
|
2022-02-25 21:48:56 +01:00
|
|
|
include_stream_name: bool = False,
|
2022-02-22 21:14:28 +01:00
|
|
|
visibility_policy: Optional[int] = None,
|
|
|
|
) -> List[UserTopicDict]:
|
|
|
|
"""
|
|
|
|
Fetches UserTopic objects associated with the target user.
|
|
|
|
* include_deactivated: Whether to include those associated with
|
|
|
|
deactivated streams.
|
2022-02-25 21:48:56 +01:00
|
|
|
* include_stream_name: Whether to include stream names in the
|
|
|
|
returned dictionaries.
|
2022-02-22 21:14:28 +01:00
|
|
|
* visibility_policy: If specified, returns only UserTopic objects
|
|
|
|
with the specified visibility_policy value.
|
|
|
|
"""
|
|
|
|
query = UserTopic.objects.filter(user_profile=user_profile)
|
|
|
|
|
|
|
|
if visibility_policy is not None:
|
|
|
|
query = query.filter(visibility_policy=visibility_policy)
|
|
|
|
|
|
|
|
# Exclude user topics that are part of deactivated streams unless
|
2021-02-16 02:26:56 +01:00
|
|
|
# explicitly requested.
|
|
|
|
if not include_deactivated:
|
|
|
|
query = query.filter(stream__deactivated=False)
|
2022-02-22 21:14:28 +01:00
|
|
|
|
2021-02-16 02:26:56 +01:00
|
|
|
rows = query.values(
|
2022-02-22 21:14:28 +01:00
|
|
|
"stream_id", "stream__name", "topic_name", "last_updated", "visibility_policy"
|
2017-08-30 02:19:34 +02:00
|
|
|
)
|
2022-02-22 21:14:28 +01:00
|
|
|
|
|
|
|
result = []
|
|
|
|
for row in rows:
|
2022-08-10 20:48:11 +02:00
|
|
|
user_topic_dict: UserTopicDict = {
|
|
|
|
"stream_id": row["stream_id"],
|
|
|
|
"topic_name": row["topic_name"],
|
|
|
|
"visibility_policy": row["visibility_policy"],
|
|
|
|
"last_updated": datetime_to_timestamp(row["last_updated"]),
|
|
|
|
}
|
2022-02-25 21:48:56 +01:00
|
|
|
|
2022-08-10 20:48:11 +02:00
|
|
|
if include_stream_name:
|
|
|
|
user_topic_dict["stream__name"] = row["stream__name"]
|
|
|
|
|
|
|
|
result.append(user_topic_dict)
|
2022-02-22 21:14:28 +01:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def get_topic_mutes(
|
|
|
|
user_profile: UserProfile, include_deactivated: bool = False
|
|
|
|
) -> List[Tuple[str, str, int]]:
|
|
|
|
user_topics = get_user_topics(
|
|
|
|
user_profile=user_profile,
|
|
|
|
include_deactivated=include_deactivated,
|
2022-02-25 21:48:56 +01:00
|
|
|
include_stream_name=True,
|
2022-02-22 21:14:28 +01:00
|
|
|
visibility_policy=UserTopic.MUTED,
|
|
|
|
)
|
|
|
|
|
2017-08-30 02:19:34 +02:00
|
|
|
return [
|
2022-02-22 21:14:28 +01:00
|
|
|
(user_topic["stream__name"], user_topic["topic_name"], user_topic["last_updated"])
|
|
|
|
for user_topic in user_topics
|
2017-08-30 02:19:34 +02:00
|
|
|
]
|
2017-08-24 17:58:40 +02:00
|
|
|
|
2021-02-12 08:19:30 +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.
|
2021-02-12 08:19:30 +01:00
|
|
|
"""
|
2017-08-30 02:19:34 +02:00
|
|
|
|
2021-07-23 15:26:02 +02:00
|
|
|
UserTopic.objects.filter(
|
2017-08-30 02:19:34 +02:00
|
|
|
user_profile=user_profile,
|
2021-08-02 09:49:56 +02:00
|
|
|
visibility_policy=UserTopic.MUTED,
|
2017-08-30 02:19:34 +02:00
|
|
|
).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
|
2022-06-15 04:59:36 +02:00
|
|
|
assert recipient_id is not None
|
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
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def add_topic_mute(
|
|
|
|
user_profile: UserProfile,
|
|
|
|
stream_id: int,
|
|
|
|
recipient_id: int,
|
|
|
|
topic_name: str,
|
|
|
|
date_muted: Optional[datetime.datetime] = None,
|
2022-03-25 00:42:12 +01:00
|
|
|
ignore_duplicate: bool = False,
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> None:
|
2020-01-17 16:01:00 +01:00
|
|
|
if date_muted is None:
|
|
|
|
date_muted = timezone_now()
|
2022-03-25 00:42:12 +01:00
|
|
|
UserTopic.objects.bulk_create(
|
|
|
|
[
|
|
|
|
UserTopic(
|
|
|
|
user_profile=user_profile,
|
|
|
|
stream_id=stream_id,
|
|
|
|
recipient_id=recipient_id,
|
|
|
|
topic_name=topic_name,
|
|
|
|
last_updated=date_muted,
|
|
|
|
visibility_policy=UserTopic.MUTED,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
ignore_conflicts=ignore_duplicate,
|
2017-08-30 02:19:34 +02:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def remove_topic_mute(user_profile: UserProfile, stream_id: int, topic_name: str) -> None:
|
2021-07-23 15:26:02 +02:00
|
|
|
row = UserTopic.objects.get(
|
2017-08-30 02:19:34 +02:00
|
|
|
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,
|
2021-08-02 09:49:56 +02:00
|
|
|
visibility_policy=UserTopic.MUTED,
|
2017-08-30 02:19:34 +02:00
|
|
|
)
|
|
|
|
row.delete()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def topic_is_muted(user_profile: UserProfile, stream_id: int, topic_name: str) -> bool:
|
2021-07-23 15:26:02 +02:00
|
|
|
is_muted = UserTopic.objects.filter(
|
2017-08-30 02:19:34 +02:00
|
|
|
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,
|
2021-08-02 09:49:56 +02:00
|
|
|
visibility_policy=UserTopic.MUTED,
|
2017-08-30 02:19:34 +02:00
|
|
|
).exists()
|
2017-08-24 17:58:40 +02:00
|
|
|
return is_muted
|
2017-08-29 17:16:53 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def exclude_topic_mutes(
|
|
|
|
conditions: List[ClauseElement], user_profile: UserProfile, stream_id: Optional[int]
|
|
|
|
) -> List[ClauseElement]:
|
2021-02-16 02:26:56 +01:00
|
|
|
# Note: Unlike get_topic_mutes, here we always want to
|
|
|
|
# consider topics in deactivated streams, so they are
|
|
|
|
# never filtered from the query in this method.
|
2021-07-23 15:26:02 +02:00
|
|
|
query = UserTopic.objects.filter(
|
2017-08-30 02:19:34 +02:00
|
|
|
user_profile=user_profile,
|
2021-08-02 09:49:56 +02:00
|
|
|
visibility_policy=UserTopic.MUTED,
|
2017-08-30 02:19:34 +02:00
|
|
|
)
|
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
|
|
|
|
2022-06-23 20:21:54 +02:00
|
|
|
rows = query.values(
|
|
|
|
"recipient_id",
|
|
|
|
"topic_name",
|
2017-08-30 02:19:34 +02:00
|
|
|
)
|
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
|
|
|
|
|
2022-06-23 22:54:31 +02:00
|
|
|
class RecipientTopicDict(TypedDict):
|
|
|
|
recipient_id: int
|
|
|
|
topic_name: str
|
|
|
|
|
|
|
|
def mute_cond(row: RecipientTopicDict) -> ClauseElement:
|
2021-02-12 08:20:45 +01:00
|
|
|
recipient_id = row["recipient_id"]
|
|
|
|
topic_name = row["topic_name"]
|
2021-08-21 01:07:28 +02:00
|
|
|
stream_cond = column("recipient_id", Integer) == 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))))
|
2020-09-02 06:59:07 +02:00
|
|
|
return [*conditions, condition]
|
2017-08-29 18:12:15 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def build_topic_mute_checker(user_profile: UserProfile) -> Callable[[int, str], bool]:
|
2021-08-02 09:49:56 +02:00
|
|
|
rows = UserTopic.objects.filter(
|
|
|
|
user_profile=user_profile, visibility_policy=UserTopic.MUTED
|
|
|
|
).values(
|
2021-02-12 08:20:45 +01:00
|
|
|
"recipient_id",
|
|
|
|
"topic_name",
|
2017-08-30 02:19:34 +02:00
|
|
|
)
|
|
|
|
|
2017-08-29 18:12:15 +02:00
|
|
|
tups = set()
|
|
|
|
for row in rows:
|
2021-02-12 08:20:45 +01:00
|
|
|
recipient_id = row["recipient_id"]
|
|
|
|
topic_name = row["topic_name"]
|
2017-08-30 02:19:34 +02:00
|
|
|
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
|
2022-03-18 01:19:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_users_muting_topic(stream_id: int, topic_name: str) -> QuerySet[UserProfile]:
|
|
|
|
return UserProfile.objects.select_related("realm").filter(
|
|
|
|
id__in=UserTopic.objects.filter(
|
|
|
|
stream_id=stream_id,
|
|
|
|
visibility_policy=UserTopic.MUTED,
|
|
|
|
topic_name__iexact=topic_name,
|
|
|
|
).values("user_profile_id")
|
|
|
|
)
|