2023-11-19 19:45:19 +01:00
|
|
|
from datetime import datetime
|
2023-03-24 18:32:06 +01:00
|
|
|
from typing import Any, Dict, List, Optional
|
2022-04-14 23:41:25 +02:00
|
|
|
|
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
2022-02-25 21:48:56 +01:00
|
|
|
from zerver.lib.timestamp import datetime_to_timestamp
|
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 zerver.lib.user_topics import (
|
2023-03-24 18:32:06 +01:00
|
|
|
bulk_set_user_topic_visibility_policy_in_database,
|
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
|
|
|
get_topic_mutes,
|
|
|
|
)
|
2023-03-09 15:29:44 +01:00
|
|
|
from zerver.models import Stream, UserProfile
|
2022-04-14 23:41:25 +02:00
|
|
|
from zerver.tornado.django_api import send_event
|
|
|
|
|
|
|
|
|
2023-03-24 18:32:06 +01:00
|
|
|
def bulk_do_set_user_topic_visibility_policy(
|
|
|
|
user_profiles: List[UserProfile],
|
2023-02-03 12:57:43 +01:00
|
|
|
stream: Stream,
|
|
|
|
topic: str,
|
|
|
|
*,
|
|
|
|
visibility_policy: int,
|
2023-11-19 19:45:19 +01:00
|
|
|
last_updated: Optional[datetime] = None,
|
2023-02-03 13:21:25 +01:00
|
|
|
skip_muted_topics_event: bool = False,
|
2022-08-05 01:50:08 +02:00
|
|
|
) -> None:
|
2023-02-03 12:57:43 +01:00
|
|
|
if last_updated is None:
|
|
|
|
last_updated = timezone_now()
|
|
|
|
|
2023-03-24 18:32:06 +01:00
|
|
|
user_profiles_with_changed_user_topic_rows = bulk_set_user_topic_visibility_policy_in_database(
|
|
|
|
user_profiles,
|
2023-03-09 15:29:44 +01:00
|
|
|
stream.id,
|
|
|
|
topic,
|
|
|
|
visibility_policy=visibility_policy,
|
|
|
|
recipient_id=stream.recipient_id,
|
|
|
|
last_updated=last_updated,
|
|
|
|
)
|
2022-02-25 21:48:56 +01:00
|
|
|
|
2023-03-24 18:32:06 +01:00
|
|
|
# Users with requests to set the visibility_policy to its current value
|
2023-03-14 15:09:12 +01:00
|
|
|
# or to delete a UserTopic row that doesn't exist shouldn't
|
|
|
|
# send an unnecessary event.
|
2023-03-24 18:32:06 +01:00
|
|
|
if len(user_profiles_with_changed_user_topic_rows) == 0:
|
2023-03-14 15:09:12 +01:00
|
|
|
return
|
|
|
|
|
2023-03-24 18:32:06 +01:00
|
|
|
for user_profile in user_profiles_with_changed_user_topic_rows:
|
|
|
|
# This first muted_topics event is deprecated and will be removed
|
|
|
|
# once clients are migrated to handle the user_topic event type
|
|
|
|
# instead.
|
|
|
|
if not skip_muted_topics_event:
|
|
|
|
muted_topics_event = dict(
|
|
|
|
type="muted_topics", muted_topics=get_topic_mutes(user_profile)
|
|
|
|
)
|
|
|
|
send_event(user_profile.realm, muted_topics_event, [user_profile.id])
|
|
|
|
|
|
|
|
user_topic_event: Dict[str, Any] = {
|
|
|
|
"type": "user_topic",
|
|
|
|
"stream_id": stream.id,
|
|
|
|
"topic_name": topic,
|
|
|
|
"last_updated": datetime_to_timestamp(last_updated),
|
|
|
|
"visibility_policy": visibility_policy,
|
|
|
|
}
|
|
|
|
|
|
|
|
send_event(user_profile.realm, user_topic_event, [user_profile.id])
|
|
|
|
|
|
|
|
|
|
|
|
def do_set_user_topic_visibility_policy(
|
|
|
|
user_profile: UserProfile,
|
|
|
|
stream: Stream,
|
|
|
|
topic: str,
|
|
|
|
*,
|
|
|
|
visibility_policy: int,
|
2023-11-19 19:45:19 +01:00
|
|
|
last_updated: Optional[datetime] = None,
|
2023-03-24 18:32:06 +01:00
|
|
|
skip_muted_topics_event: bool = False,
|
|
|
|
) -> None:
|
|
|
|
# For conciseness, this function should be used when a single
|
|
|
|
# user_profile is involved. In the case of multiple user profiles,
|
|
|
|
# call 'bulk_do_set_user_topic_visibility_policy' directly.
|
|
|
|
bulk_do_set_user_topic_visibility_policy(
|
|
|
|
[user_profile],
|
|
|
|
stream,
|
|
|
|
topic,
|
|
|
|
visibility_policy=visibility_policy,
|
|
|
|
last_updated=last_updated,
|
|
|
|
skip_muted_topics_event=skip_muted_topics_event,
|
|
|
|
)
|