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,
|
2017-08-29 17:16:53 +02:00
|
|
|
UserProfile
|
|
|
|
)
|
|
|
|
from sqlalchemy.sql import (
|
|
|
|
and_,
|
|
|
|
column,
|
|
|
|
not_,
|
|
|
|
or_,
|
|
|
|
Selectable
|
|
|
|
)
|
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',
|
|
|
|
'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,
|
|
|
|
topic_name__iexact=topic_name
|
|
|
|
)
|
|
|
|
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',
|
|
|
|
'topic_name'
|
|
|
|
)
|
|
|
|
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',
|
|
|
|
'topic_name'
|
|
|
|
)
|
|
|
|
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
|