2022-04-14 23:41:25 +02:00
|
|
|
import datetime
|
2022-02-25 21:48:56 +01:00
|
|
|
from typing import Any, Dict, 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 (
|
|
|
|
get_topic_mutes,
|
|
|
|
set_user_topic_visibility_policy_in_database,
|
|
|
|
)
|
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-02-03 12:57:43 +01:00
|
|
|
def do_set_user_topic_visibility_policy(
|
|
|
|
user_profile: UserProfile,
|
|
|
|
stream: Stream,
|
|
|
|
topic: str,
|
|
|
|
*,
|
|
|
|
visibility_policy: int,
|
|
|
|
last_updated: Optional[datetime.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-14 15:09:12 +01:00
|
|
|
database_changed = set_user_topic_visibility_policy_in_database(
|
2023-03-09 15:29:44 +01:00
|
|
|
user_profile,
|
|
|
|
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-14 15:09:12 +01:00
|
|
|
# Requests to set the visibility_policy to its current value
|
|
|
|
# or to delete a UserTopic row that doesn't exist shouldn't
|
|
|
|
# send an unnecessary event.
|
|
|
|
if not database_changed:
|
|
|
|
return
|
|
|
|
|
2022-02-25 21:48:56 +01:00
|
|
|
# This first muted_topics event is deprecated and will be removed
|
|
|
|
# once clients are migrated to handle the user_topic event type
|
|
|
|
# instead.
|
2023-02-03 13:21:25 +01:00
|
|
|
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])
|
2022-02-25 21:48:56 +01:00
|
|
|
|
|
|
|
user_topic_event: Dict[str, Any] = {
|
|
|
|
"type": "user_topic",
|
|
|
|
"stream_id": stream.id,
|
|
|
|
"topic_name": topic,
|
2023-02-03 12:57:43 +01:00
|
|
|
"last_updated": datetime_to_timestamp(last_updated),
|
|
|
|
"visibility_policy": visibility_policy,
|
2022-02-25 21:48:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
send_event(user_profile.realm, user_topic_event, [user_profile.id])
|