2019-02-02 23:53:55 +01:00
|
|
|
from typing import Set
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-10-29 16:50:20 +01:00
|
|
|
from django.db.models.query import QuerySet
|
2017-10-23 19:39:12 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.stream_subscription import get_active_subscriptions_for_stream_id
|
|
|
|
from zerver.models import MutedTopic
|
2017-10-29 16:50:20 +01:00
|
|
|
|
2017-10-23 19:39:12 +02:00
|
|
|
|
2017-11-05 11:37:41 +01:00
|
|
|
class StreamTopicTarget:
|
2021-02-12 08:19:30 +01:00
|
|
|
"""
|
2017-10-23 19:39:12 +02:00
|
|
|
This class is designed to help us move to a
|
|
|
|
StreamTopic table or something similar. It isolates
|
2018-12-23 18:45:18 +01:00
|
|
|
places where we are are still using `topic_name` as
|
|
|
|
a key into tables.
|
2021-02-12 08:19:30 +01:00
|
|
|
"""
|
2020-04-22 01:45:30 +02:00
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def __init__(self, stream_id: int, topic_name: str) -> None:
|
2017-10-23 19:39:12 +02:00
|
|
|
self.stream_id = stream_id
|
|
|
|
self.topic_name = topic_name
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def user_ids_muting_topic(self) -> Set[int]:
|
2017-10-23 19:39:12 +02:00
|
|
|
query = MutedTopic.objects.filter(
|
|
|
|
stream_id=self.stream_id,
|
|
|
|
topic_name__iexact=self.topic_name,
|
|
|
|
).values(
|
2021-02-12 08:20:45 +01:00
|
|
|
"user_profile_id",
|
2017-10-23 19:39:12 +02:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
return {row["user_profile_id"] for row in query}
|
2017-10-29 16:50:20 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def get_active_subscriptions(self) -> QuerySet:
|
2021-04-17 13:29:55 +02:00
|
|
|
return get_active_subscriptions_for_stream_id(
|
|
|
|
self.stream_id, include_deactivated_users=True
|
|
|
|
)
|