2017-08-24 17:58:40 +02:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2017-09-17 20:19:12 +02:00
|
|
|
from typing import Any, Callable, Dict, List, Optional, Text
|
2017-08-24 17:58:40 +02:00
|
|
|
|
2017-08-29 17:16:53 +02:00
|
|
|
from zerver.models import (
|
|
|
|
bulk_get_recipients,
|
|
|
|
bulk_get_streams,
|
2017-08-30 02:19:34 +02:00
|
|
|
get_recipient,
|
|
|
|
get_stream,
|
|
|
|
get_recipient,
|
|
|
|
get_stream,
|
|
|
|
MutedTopic,
|
2017-08-29 17:16:53 +02:00
|
|
|
Recipient,
|
2017-08-29 18:12:15 +02:00
|
|
|
Stream,
|
2017-08-29 17:16:53 +02:00
|
|
|
UserProfile
|
|
|
|
)
|
|
|
|
from sqlalchemy.sql import (
|
|
|
|
and_,
|
|
|
|
column,
|
|
|
|
func,
|
|
|
|
not_,
|
|
|
|
or_,
|
|
|
|
Selectable
|
|
|
|
)
|
2017-08-24 17:58:40 +02:00
|
|
|
|
|
|
|
import ujson
|
|
|
|
|
|
|
|
def get_topic_mutes(user_profile):
|
|
|
|
# type: (UserProfile) -> List[List[Text]]
|
2017-08-30 02:19:34 +02:00
|
|
|
rows = MutedTopic.objects.filter(
|
|
|
|
user_profile=user_profile,
|
|
|
|
).values(
|
|
|
|
'stream__name',
|
|
|
|
'topic_name'
|
|
|
|
)
|
|
|
|
return [
|
|
|
|
[row['stream__name'], row['topic_name']]
|
|
|
|
for row in rows
|
|
|
|
]
|
2017-08-24 17:58:40 +02:00
|
|
|
|
|
|
|
def set_topic_mutes(user_profile, muted_topics):
|
|
|
|
# type: (UserProfile, List[List[Text]]) -> None
|
2017-08-30 02:19:34 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
This is only used in tests.
|
|
|
|
'''
|
|
|
|
|
|
|
|
MutedTopic.objects.filter(
|
|
|
|
user_profile=user_profile,
|
|
|
|
).delete()
|
|
|
|
|
|
|
|
for stream_name, topic_name in muted_topics:
|
|
|
|
stream = get_stream(stream_name, user_profile.realm)
|
|
|
|
recipient = get_recipient(Recipient.STREAM, stream.id)
|
|
|
|
|
|
|
|
add_topic_mute(
|
|
|
|
user_profile=user_profile,
|
|
|
|
stream_id=stream.id,
|
|
|
|
recipient_id=recipient.id,
|
|
|
|
topic_name=topic_name,
|
|
|
|
)
|
|
|
|
|
|
|
|
def add_topic_mute(user_profile, stream_id, recipient_id, topic_name):
|
|
|
|
# type: (UserProfile, int, int, str) -> None
|
|
|
|
MutedTopic.objects.create(
|
|
|
|
user_profile=user_profile,
|
|
|
|
stream_id=stream_id,
|
|
|
|
recipient_id=recipient_id,
|
|
|
|
topic_name=topic_name,
|
|
|
|
)
|
|
|
|
|
|
|
|
def remove_topic_mute(user_profile, stream_id, topic_name):
|
|
|
|
# type: (UserProfile, int, str) -> None
|
|
|
|
row = MutedTopic.objects.get(
|
|
|
|
user_profile=user_profile,
|
|
|
|
stream_id=stream_id,
|
|
|
|
topic_name__iexact=topic_name
|
|
|
|
)
|
|
|
|
row.delete()
|
|
|
|
|
|
|
|
def topic_is_muted(user_profile, stream, topic_name):
|
|
|
|
# type: (UserProfile, Stream, Text) -> bool
|
|
|
|
is_muted = MutedTopic.objects.filter(
|
|
|
|
user_profile=user_profile,
|
|
|
|
stream_id=stream.id,
|
|
|
|
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-09-17 20:19:12 +02:00
|
|
|
def exclude_topic_mutes(conditions, user_profile, stream_id):
|
|
|
|
# type: (List[Selectable], UserProfile, 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-08-30 02:19:34 +02:00
|
|
|
def mute_cond(row):
|
|
|
|
# type: (Dict[str, Any]) -> Selectable
|
|
|
|
recipient_id = row['recipient_id']
|
|
|
|
topic_name = row['topic_name']
|
|
|
|
stream_cond = column("recipient_id") == recipient_id
|
|
|
|
topic_cond = func.upper(column("subject")) == func.upper(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
|
|
|
|
|
|
|
def build_topic_mute_checker(user_profile):
|
|
|
|
# type: (UserProfile) -> Callable[[int, Text], 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()))
|
|
|
|
|
|
|
|
def is_muted(recipient_id, topic):
|
2017-08-29 18:12:15 +02:00
|
|
|
# type: (int, Text) -> 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
|