2020-01-17 16:01:00 +01:00
|
|
|
import datetime
|
2023-03-14 15:09:12 +01:00
|
|
|
import logging
|
2023-09-25 09:09:53 +02:00
|
|
|
from collections import defaultdict
|
|
|
|
from typing import Callable, Dict, List, Optional, Tuple, TypedDict
|
2020-06-11 00:54:34 +02:00
|
|
|
|
user_topics: Refactor add_topic_mute.
In order to support different types of topic visibility policies,
this renames 'add_topic_mute' to
'set_user_topic_visibility_policy_in_database'
and refactors it to accept a parameter 'visibility_policy'.
Create a corresponding UserTopic row for any visibility policy,
not just muting topics.
When a UserTopic row for (user_profile, stream, topic, recipient_id)
exists already, it updates the row with the new visibility_policy.
In the event of a duplicate request, raises a JsonableError.
i.e., new_visibility_policy == existing_visibility_policy.
There is an increase in the database query count in the message-edit
code path.
Reason:
Earlier, 'add_topic_mute' used 'bulk_create' which either
creates or raises IntegrityError -- 1 query.
Now, 'set_user_topic_visibility_policy' uses get_or_create
-- 2 queries in the case of creating new row.
We can't use the previous approach, because now we have to
handle the case of updating the visibility_policy too.
Also, using bulk_* for a single row is not the correct way.
Co-authored-by: Kartik Srivastava <kaushiksri0908@gmail.com>
Co-authored-by: Prakhar Pratyush <prakhar841301@gmail.com>
2022-09-12 16:39:53 +02:00
|
|
|
from django.db import transaction
|
2023-03-04 01:52:14 +01:00
|
|
|
from django.db.models 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,
|
2023-03-12 16:19:42 +01:00
|
|
|
visibility_policy=UserTopic.VisibilityPolicy.MUTED,
|
2022-02-22 21:14:28 +01:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2023-03-26 15:36:01 +02:00
|
|
|
def set_topic_visibility_policy(
|
2021-02-12 08:19:30 +01:00
|
|
|
user_profile: UserProfile,
|
2023-03-26 15:36:01 +02:00
|
|
|
topics: List[List[str]],
|
|
|
|
visibility_policy: int,
|
|
|
|
last_updated: Optional[datetime.datetime] = None,
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> 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,
|
2023-03-26 15:36:01 +02:00
|
|
|
visibility_policy=visibility_policy,
|
2017-08-30 02:19:34 +02:00
|
|
|
).delete()
|
|
|
|
|
2023-03-26 15:36:01 +02:00
|
|
|
if last_updated is None:
|
|
|
|
last_updated = timezone_now()
|
|
|
|
for stream_name, topic_name in topics:
|
2017-08-30 02:19:34 +02:00
|
|
|
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
|
|
|
|
2023-03-24 18:32:06 +01:00
|
|
|
bulk_set_user_topic_visibility_policy_in_database(
|
|
|
|
user_profiles=[user_profile],
|
2017-08-30 02:19:34 +02:00
|
|
|
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,
|
2023-03-26 15:36:01 +02:00
|
|
|
visibility_policy=visibility_policy,
|
|
|
|
last_updated=last_updated,
|
2017-08-30 02:19:34 +02:00
|
|
|
)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
user_topics: Refactor add_topic_mute.
In order to support different types of topic visibility policies,
this renames 'add_topic_mute' to
'set_user_topic_visibility_policy_in_database'
and refactors it to accept a parameter 'visibility_policy'.
Create a corresponding UserTopic row for any visibility policy,
not just muting topics.
When a UserTopic row for (user_profile, stream, topic, recipient_id)
exists already, it updates the row with the new visibility_policy.
In the event of a duplicate request, raises a JsonableError.
i.e., new_visibility_policy == existing_visibility_policy.
There is an increase in the database query count in the message-edit
code path.
Reason:
Earlier, 'add_topic_mute' used 'bulk_create' which either
creates or raises IntegrityError -- 1 query.
Now, 'set_user_topic_visibility_policy' uses get_or_create
-- 2 queries in the case of creating new row.
We can't use the previous approach, because now we have to
handle the case of updating the visibility_policy too.
Also, using bulk_* for a single row is not the correct way.
Co-authored-by: Kartik Srivastava <kaushiksri0908@gmail.com>
Co-authored-by: Prakhar Pratyush <prakhar841301@gmail.com>
2022-09-12 16:39:53 +02:00
|
|
|
@transaction.atomic(savepoint=False)
|
2023-03-24 18:32:06 +01:00
|
|
|
def bulk_set_user_topic_visibility_policy_in_database(
|
|
|
|
user_profiles: List[UserProfile],
|
2021-02-12 08:19:30 +01:00
|
|
|
stream_id: int,
|
|
|
|
topic_name: str,
|
user_topics: Refactor add_topic_mute.
In order to support different types of topic visibility policies,
this renames 'add_topic_mute' to
'set_user_topic_visibility_policy_in_database'
and refactors it to accept a parameter 'visibility_policy'.
Create a corresponding UserTopic row for any visibility policy,
not just muting topics.
When a UserTopic row for (user_profile, stream, topic, recipient_id)
exists already, it updates the row with the new visibility_policy.
In the event of a duplicate request, raises a JsonableError.
i.e., new_visibility_policy == existing_visibility_policy.
There is an increase in the database query count in the message-edit
code path.
Reason:
Earlier, 'add_topic_mute' used 'bulk_create' which either
creates or raises IntegrityError -- 1 query.
Now, 'set_user_topic_visibility_policy' uses get_or_create
-- 2 queries in the case of creating new row.
We can't use the previous approach, because now we have to
handle the case of updating the visibility_policy too.
Also, using bulk_* for a single row is not the correct way.
Co-authored-by: Kartik Srivastava <kaushiksri0908@gmail.com>
Co-authored-by: Prakhar Pratyush <prakhar841301@gmail.com>
2022-09-12 16:39:53 +02:00
|
|
|
*,
|
|
|
|
visibility_policy: int,
|
2022-09-21 15:51:48 +02:00
|
|
|
recipient_id: Optional[int] = None,
|
user_topics: Refactor add_topic_mute.
In order to support different types of topic visibility policies,
this renames 'add_topic_mute' to
'set_user_topic_visibility_policy_in_database'
and refactors it to accept a parameter 'visibility_policy'.
Create a corresponding UserTopic row for any visibility policy,
not just muting topics.
When a UserTopic row for (user_profile, stream, topic, recipient_id)
exists already, it updates the row with the new visibility_policy.
In the event of a duplicate request, raises a JsonableError.
i.e., new_visibility_policy == existing_visibility_policy.
There is an increase in the database query count in the message-edit
code path.
Reason:
Earlier, 'add_topic_mute' used 'bulk_create' which either
creates or raises IntegrityError -- 1 query.
Now, 'set_user_topic_visibility_policy' uses get_or_create
-- 2 queries in the case of creating new row.
We can't use the previous approach, because now we have to
handle the case of updating the visibility_policy too.
Also, using bulk_* for a single row is not the correct way.
Co-authored-by: Kartik Srivastava <kaushiksri0908@gmail.com>
Co-authored-by: Prakhar Pratyush <prakhar841301@gmail.com>
2022-09-12 16:39:53 +02:00
|
|
|
last_updated: Optional[datetime.datetime] = None,
|
2023-03-24 18:32:06 +01:00
|
|
|
) -> List[UserProfile]:
|
|
|
|
# returns the list of user_profiles whose user_topic row
|
|
|
|
# is either deleted, updated, or created.
|
|
|
|
rows = UserTopic.objects.filter(
|
|
|
|
user_profile__in=user_profiles,
|
|
|
|
stream_id=stream_id,
|
|
|
|
topic_name__iexact=topic_name,
|
|
|
|
).select_related("user_profile", "user_profile__realm")
|
|
|
|
|
|
|
|
user_profiles_with_visibility_policy = [row.user_profile for row in rows]
|
|
|
|
user_profiles_without_visibility_policy = list(
|
|
|
|
set(user_profiles) - set(user_profiles_with_visibility_policy)
|
|
|
|
)
|
|
|
|
|
2023-03-12 16:19:42 +01:00
|
|
|
if visibility_policy == UserTopic.VisibilityPolicy.INHERIT:
|
2023-03-24 18:32:06 +01:00
|
|
|
for user_profile in user_profiles_without_visibility_policy:
|
|
|
|
# The user doesn't already have a visibility_policy for this topic.
|
2023-03-14 15:09:12 +01:00
|
|
|
logging.info(
|
|
|
|
"User %s tried to remove visibility_policy, which actually doesn't exist",
|
|
|
|
user_profile.id,
|
|
|
|
)
|
2023-03-24 18:32:06 +01:00
|
|
|
rows.delete()
|
|
|
|
return user_profiles_with_visibility_policy
|
2022-09-21 15:51:48 +02:00
|
|
|
|
user_topics: Refactor add_topic_mute.
In order to support different types of topic visibility policies,
this renames 'add_topic_mute' to
'set_user_topic_visibility_policy_in_database'
and refactors it to accept a parameter 'visibility_policy'.
Create a corresponding UserTopic row for any visibility policy,
not just muting topics.
When a UserTopic row for (user_profile, stream, topic, recipient_id)
exists already, it updates the row with the new visibility_policy.
In the event of a duplicate request, raises a JsonableError.
i.e., new_visibility_policy == existing_visibility_policy.
There is an increase in the database query count in the message-edit
code path.
Reason:
Earlier, 'add_topic_mute' used 'bulk_create' which either
creates or raises IntegrityError -- 1 query.
Now, 'set_user_topic_visibility_policy' uses get_or_create
-- 2 queries in the case of creating new row.
We can't use the previous approach, because now we have to
handle the case of updating the visibility_policy too.
Also, using bulk_* for a single row is not the correct way.
Co-authored-by: Kartik Srivastava <kaushiksri0908@gmail.com>
Co-authored-by: Prakhar Pratyush <prakhar841301@gmail.com>
2022-09-12 16:39:53 +02:00
|
|
|
assert last_updated is not None
|
2023-03-09 15:29:44 +01:00
|
|
|
assert recipient_id is not None
|
2017-08-30 02:19:34 +02:00
|
|
|
|
2023-03-24 18:32:06 +01:00
|
|
|
user_profiles_seeking_visibility_policy_update: List[UserProfile] = []
|
|
|
|
for row in rows:
|
|
|
|
duplicate_request: bool = row.visibility_policy == visibility_policy
|
|
|
|
if duplicate_request:
|
|
|
|
logging.info(
|
|
|
|
"User %s tried to set visibility_policy to its current value of %s",
|
|
|
|
row.user_profile_id,
|
|
|
|
visibility_policy,
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
# The request is to just 'update' the visibility policy of a topic
|
|
|
|
user_profiles_seeking_visibility_policy_update.append(row.user_profile)
|
user_topics: Refactor add_topic_mute.
In order to support different types of topic visibility policies,
this renames 'add_topic_mute' to
'set_user_topic_visibility_policy_in_database'
and refactors it to accept a parameter 'visibility_policy'.
Create a corresponding UserTopic row for any visibility policy,
not just muting topics.
When a UserTopic row for (user_profile, stream, topic, recipient_id)
exists already, it updates the row with the new visibility_policy.
In the event of a duplicate request, raises a JsonableError.
i.e., new_visibility_policy == existing_visibility_policy.
There is an increase in the database query count in the message-edit
code path.
Reason:
Earlier, 'add_topic_mute' used 'bulk_create' which either
creates or raises IntegrityError -- 1 query.
Now, 'set_user_topic_visibility_policy' uses get_or_create
-- 2 queries in the case of creating new row.
We can't use the previous approach, because now we have to
handle the case of updating the visibility_policy too.
Also, using bulk_* for a single row is not the correct way.
Co-authored-by: Kartik Srivastava <kaushiksri0908@gmail.com>
Co-authored-by: Prakhar Pratyush <prakhar841301@gmail.com>
2022-09-12 16:39:53 +02:00
|
|
|
|
2023-03-24 18:32:06 +01:00
|
|
|
if user_profiles_seeking_visibility_policy_update:
|
|
|
|
rows.filter(user_profile__in=user_profiles_seeking_visibility_policy_update).update(
|
|
|
|
visibility_policy=visibility_policy, last_updated=last_updated
|
user_topics: Refactor add_topic_mute.
In order to support different types of topic visibility policies,
this renames 'add_topic_mute' to
'set_user_topic_visibility_policy_in_database'
and refactors it to accept a parameter 'visibility_policy'.
Create a corresponding UserTopic row for any visibility policy,
not just muting topics.
When a UserTopic row for (user_profile, stream, topic, recipient_id)
exists already, it updates the row with the new visibility_policy.
In the event of a duplicate request, raises a JsonableError.
i.e., new_visibility_policy == existing_visibility_policy.
There is an increase in the database query count in the message-edit
code path.
Reason:
Earlier, 'add_topic_mute' used 'bulk_create' which either
creates or raises IntegrityError -- 1 query.
Now, 'set_user_topic_visibility_policy' uses get_or_create
-- 2 queries in the case of creating new row.
We can't use the previous approach, because now we have to
handle the case of updating the visibility_policy too.
Also, using bulk_* for a single row is not the correct way.
Co-authored-by: Kartik Srivastava <kaushiksri0908@gmail.com>
Co-authored-by: Prakhar Pratyush <prakhar841301@gmail.com>
2022-09-12 16:39:53 +02:00
|
|
|
)
|
2023-03-14 15:09:12 +01:00
|
|
|
|
2023-03-24 18:32:06 +01:00
|
|
|
if user_profiles_without_visibility_policy:
|
|
|
|
UserTopic.objects.bulk_create(
|
|
|
|
UserTopic(
|
|
|
|
user_profile=user_profile,
|
|
|
|
stream_id=stream_id,
|
|
|
|
recipient_id=recipient_id,
|
|
|
|
topic_name=topic_name,
|
|
|
|
last_updated=last_updated,
|
|
|
|
visibility_policy=visibility_policy,
|
|
|
|
)
|
|
|
|
for user_profile in user_profiles_without_visibility_policy
|
|
|
|
)
|
|
|
|
return user_profiles_seeking_visibility_policy_update + user_profiles_without_visibility_policy
|
user_topics: Refactor add_topic_mute.
In order to support different types of topic visibility policies,
this renames 'add_topic_mute' to
'set_user_topic_visibility_policy_in_database'
and refactors it to accept a parameter 'visibility_policy'.
Create a corresponding UserTopic row for any visibility policy,
not just muting topics.
When a UserTopic row for (user_profile, stream, topic, recipient_id)
exists already, it updates the row with the new visibility_policy.
In the event of a duplicate request, raises a JsonableError.
i.e., new_visibility_policy == existing_visibility_policy.
There is an increase in the database query count in the message-edit
code path.
Reason:
Earlier, 'add_topic_mute' used 'bulk_create' which either
creates or raises IntegrityError -- 1 query.
Now, 'set_user_topic_visibility_policy' uses get_or_create
-- 2 queries in the case of creating new row.
We can't use the previous approach, because now we have to
handle the case of updating the visibility_policy too.
Also, using bulk_* for a single row is not the correct way.
Co-authored-by: Kartik Srivastava <kaushiksri0908@gmail.com>
Co-authored-by: Prakhar Pratyush <prakhar841301@gmail.com>
2022-09-12 16:39:53 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-03-26 15:36:01 +02:00
|
|
|
def topic_has_visibility_policy(
|
|
|
|
user_profile: UserProfile, stream_id: int, topic_name: str, visibility_policy: int
|
|
|
|
) -> bool:
|
2023-04-09 21:32:44 +02:00
|
|
|
if visibility_policy == UserTopic.VisibilityPolicy.INHERIT:
|
|
|
|
has_user_topic_row = UserTopic.objects.filter(
|
|
|
|
user_profile=user_profile, stream_id=stream_id, topic_name__iexact=topic_name
|
|
|
|
).exists()
|
|
|
|
return not has_user_topic_row
|
2023-03-26 15:36:01 +02:00
|
|
|
has_visibility_policy = 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,
|
2023-03-26 15:36:01 +02:00
|
|
|
visibility_policy=visibility_policy,
|
2017-08-30 02:19:34 +02:00
|
|
|
).exists()
|
2023-03-26 15:36:01 +02:00
|
|
|
return has_visibility_policy
|
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,
|
2023-03-12 16:19:42 +01:00
|
|
|
visibility_policy=UserTopic.VisibilityPolicy.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)
|
|
|
|
|
2023-09-12 23:19:57 +02:00
|
|
|
condition = not_(or_(*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
|
|
|
|
2023-09-25 09:09:53 +02:00
|
|
|
def build_get_topic_visibility_policy(
|
|
|
|
user_profile: UserProfile,
|
|
|
|
) -> Callable[[int, str], int]:
|
|
|
|
"""Prefetch the visibility policies the user has configured for
|
|
|
|
various topics.
|
|
|
|
|
|
|
|
The prefetching helps to avoid the db queries later in the loop
|
|
|
|
to determine the user's visibility policy for a topic.
|
|
|
|
"""
|
|
|
|
rows = UserTopic.objects.filter(user_profile=user_profile).values(
|
2021-02-12 08:20:45 +01:00
|
|
|
"recipient_id",
|
|
|
|
"topic_name",
|
2023-09-25 09:09:53 +02:00
|
|
|
"visibility_policy",
|
2017-08-30 02:19:34 +02:00
|
|
|
)
|
|
|
|
|
2023-09-25 09:09:53 +02:00
|
|
|
topic_to_visiblity_policy: Dict[Tuple[int, str], int] = defaultdict(int)
|
2017-08-29 18:12:15 +02:00
|
|
|
for row in rows:
|
2021-02-12 08:20:45 +01:00
|
|
|
recipient_id = row["recipient_id"]
|
|
|
|
topic_name = row["topic_name"]
|
2023-09-25 09:09:53 +02:00
|
|
|
visibility_policy = row["visibility_policy"]
|
|
|
|
topic_to_visiblity_policy[(recipient_id, topic_name)] = visibility_policy
|
2017-08-30 02:19:34 +02:00
|
|
|
|
2023-09-25 09:09:53 +02:00
|
|
|
def get_topic_visibility_policy(recipient_id: int, topic: str) -> int:
|
|
|
|
return topic_to_visiblity_policy[(recipient_id, topic.lower())]
|
2017-08-29 18:12:15 +02:00
|
|
|
|
2023-09-25 09:09:53 +02:00
|
|
|
return get_topic_visibility_policy
|
2022-03-18 01:19:16 +01:00
|
|
|
|
|
|
|
|
2023-03-17 15:09:50 +01:00
|
|
|
def get_users_with_user_topic_visibility_policy(
|
|
|
|
stream_id: int, topic_name: str
|
|
|
|
) -> QuerySet[UserTopic]:
|
|
|
|
return UserTopic.objects.filter(
|
|
|
|
stream_id=stream_id, topic_name__iexact=topic_name
|
|
|
|
).select_related("user_profile", "user_profile__realm")
|