muted_topics: Clean up unmute topic event code duplication.

The previous construction was pretty fragile, and had a long comment
to reflect that fact; rework to achieve the same effect in a much
cleaner way.
This commit is contained in:
Tim Abbott 2022-08-04 16:50:08 -07:00
parent 1291e7000b
commit bd04733c0f
2 changed files with 19 additions and 21 deletions

View File

@ -50,7 +50,7 @@ from zerver.lib.topic import (
)
from zerver.lib.types import EditHistoryEvent
from zerver.lib.user_message import UserMessageLite, bulk_insert_ums
from zerver.lib.user_topics import get_users_muting_topic, remove_topic_mute
from zerver.lib.user_topics import get_users_muting_topic
from zerver.lib.widget import is_widget_message
from zerver.models import (
ArchivedAttachment,
@ -61,7 +61,6 @@ from zerver.models import (
Stream,
UserMessage,
UserProfile,
UserTopic,
get_stream_by_id_in_realm,
get_system_bot,
)
@ -799,22 +798,18 @@ def do_update_message(
# access. Unmute the topic for such users.
do_unmute_topic(muting_user, stream_being_edited, orig_topic_name)
else:
# Otherwise, we move the muted topic record for the user.
# We call remove_topic_mute rather than do_unmute_topic to
# avoid sending two events with new muted topics in
# immediate succession; this is correct only because
# muted_topics events always send the full set of topics.
remove_topic_mute(muting_user, stream_being_edited.id, orig_topic_name)
date_unmuted = timezone_now()
user_topic_event: Dict[str, Any] = {
"type": "user_topic",
"stream_id": stream_being_edited.id,
"topic_name": orig_topic_name,
"last_updated": datetime_to_timestamp(date_unmuted),
"visibility_policy": UserTopic.VISIBILITY_POLICY_INHERIT,
}
send_event(user_profile.realm, user_topic_event, [user_profile.id])
# Otherwise, we move the muted topic record for the
# user, but removing the old topic mute and then
# creating a new one.
do_unmute_topic(
muting_user,
stream_being_edited,
orig_topic_name,
# do_mute_topic will send an updated muted topic
# event, which contains the full set of muted
# topics, just after this.
skip_muted_topics_event=True,
)
do_mute_topic(
muting_user,

View File

@ -47,7 +47,9 @@ def do_mute_topic(
send_event(user_profile.realm, user_topic_event, [user_profile.id])
def do_unmute_topic(user_profile: UserProfile, stream: Stream, topic: str) -> None:
def do_unmute_topic(
user_profile: UserProfile, stream: Stream, topic: str, *, skip_muted_topics_event: bool = False
) -> None:
# Note: If you add any new code to this function, the
# remove_topic_mute call in do_update_message will need to be
# updated for correctness.
@ -59,8 +61,9 @@ def do_unmute_topic(user_profile: UserProfile, stream: Stream, topic: str) -> No
# This first muted_topics event is deprecated and will be removed
# once clients are migrated to handle the user_topic event type
# instead.
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])
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])
date_unmuted = timezone_now()