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
|
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
|
|
|
|
from zerver.lib.exceptions import JsonableError
|
2022-02-25 21:48:56 +01:00
|
|
|
from zerver.lib.timestamp import datetime_to_timestamp
|
2022-04-14 23:41:25 +02:00
|
|
|
from zerver.lib.user_topics import add_topic_mute, get_topic_mutes, remove_topic_mute
|
|
|
|
from zerver.models import Stream, UserProfile, UserTopic
|
|
|
|
from zerver.tornado.django_api import send_event
|
|
|
|
|
|
|
|
|
2023-02-03 12:57:43 +01:00
|
|
|
def do_unmute_topic(
|
|
|
|
user_profile: UserProfile, stream: Stream, topic: str, *, skip_muted_topics_event: bool = False
|
2022-04-14 23:41:25 +02:00
|
|
|
) -> None:
|
2023-02-03 12:57:43 +01:00
|
|
|
try:
|
|
|
|
remove_topic_mute(user_profile, stream.id, topic)
|
|
|
|
except UserTopic.DoesNotExist:
|
|
|
|
raise JsonableError(_("Topic is not muted"))
|
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 12:57:43 +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])
|
|
|
|
|
|
|
|
date_unmuted = timezone_now()
|
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(date_unmuted),
|
|
|
|
"visibility_policy": UserTopic.VISIBILITY_POLICY_INHERIT,
|
2022-02-25 21:48:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
send_event(user_profile.realm, user_topic_event, [user_profile.id])
|
2022-04-14 23:41:25 +02:00
|
|
|
|
|
|
|
|
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,
|
|
|
|
ignore_duplicate: 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()
|
|
|
|
|
|
|
|
assert stream.recipient_id is not None
|
|
|
|
add_topic_mute(
|
|
|
|
user_profile,
|
|
|
|
stream.id,
|
|
|
|
stream.recipient_id,
|
|
|
|
topic,
|
|
|
|
last_updated,
|
|
|
|
ignore_duplicate=ignore_duplicate,
|
|
|
|
)
|
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 12:57:43 +01:00
|
|
|
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])
|