2023-02-03 22:33:37 +01:00
|
|
|
from typing import Dict, Set
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2021-07-23 15:26:02 +02:00
|
|
|
from zerver.models import UserTopic
|
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
|
2022-02-08 00:13:33 +01:00
|
|
|
places where we are still using `topic_name` as
|
2018-12-23 18:45:18 +01:00
|
|
|
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
|
|
|
|
|
2022-09-12 14:14:24 +02:00
|
|
|
def user_ids_with_visibility_policy(self, visibility_policy: int) -> Set[int]:
|
2021-07-23 15:26:02 +02:00
|
|
|
query = UserTopic.objects.filter(
|
2017-10-23 19:39:12 +02:00
|
|
|
stream_id=self.stream_id,
|
|
|
|
topic_name__iexact=self.topic_name,
|
2022-09-12 14:14:24 +02:00
|
|
|
visibility_policy=visibility_policy,
|
2017-10-23 19:39:12 +02:00
|
|
|
).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}
|
2023-02-03 22:33:37 +01:00
|
|
|
|
|
|
|
def user_id_to_visibility_policy_dict(self) -> Dict[int, int]:
|
|
|
|
user_id_to_visibility_policy: Dict[int, int] = {}
|
|
|
|
|
|
|
|
query = UserTopic.objects.filter(
|
|
|
|
stream_id=self.stream_id, topic_name__iexact=self.topic_name
|
|
|
|
).values(
|
|
|
|
"visibility_policy",
|
|
|
|
"user_profile_id",
|
|
|
|
)
|
|
|
|
for row in query:
|
|
|
|
user_id_to_visibility_policy[row["user_profile_id"]] = row["visibility_policy"]
|
|
|
|
return user_id_to_visibility_policy
|