backend: Move json_success to callers vs helper functions.

As a preparatory step to refactoring json_success to accept
request as a parameter, change interface of helper functions:
`handle_deferred_message` in `views.message_send.py` and
`mute_topic` and `unmute_topic` in `views.muting.py`, so
that they return None or data for json_success.

Instead call json_sucess in the caller function, which already
has the HttpRequest as a parameter.
This commit is contained in:
Lauryn Menard 2022-01-31 14:09:23 +01:00 committed by Tim Abbott
parent 81b1b18886
commit a4b347bb5f
2 changed files with 14 additions and 10 deletions

View File

@ -146,7 +146,7 @@ def handle_deferred_message(
tz_guess: Optional[str],
forwarder_user_profile: UserProfile,
realm: Optional[Realm],
) -> HttpResponse:
) -> str:
deliver_at = None
local_tz = "UTC"
if tz_guess:
@ -179,7 +179,7 @@ def handle_deferred_message(
realm=realm,
forwarder_user_profile=forwarder_user_profile,
)
return json_success({"deliver_at": str(deliver_at_usertz)})
return str(deliver_at_usertz)
@has_request_variables
@ -284,7 +284,7 @@ def send_message_backend(
raise JsonableError(_("Missing deliver_at in a request for delayed message delivery"))
if (delivery_type == "send_later" or delivery_type == "remind") and defer_until is not None:
return handle_deferred_message(
deliver_at = handle_deferred_message(
sender,
client,
message_type_name,
@ -297,6 +297,7 @@ def send_message_backend(
forwarder_user_profile=user_profile,
realm=realm,
)
return json_success(data={"deliver_at": deliver_at})
ret = check_send_message(
sender,

View File

@ -29,7 +29,7 @@ def mute_topic(
stream_name: Optional[str],
topic_name: str,
date_muted: datetime.datetime,
) -> HttpResponse:
) -> None:
if stream_name is not None:
(stream, sub) = access_stream_by_name(user_profile, stream_name)
else:
@ -40,12 +40,14 @@ def mute_topic(
raise JsonableError(_("Topic already muted"))
do_mute_topic(user_profile, stream, topic_name, date_muted)
return json_success()
def unmute_topic(
user_profile: UserProfile, stream_id: Optional[int], stream_name: Optional[str], topic_name: str
) -> HttpResponse:
user_profile: UserProfile,
stream_id: Optional[int],
stream_name: Optional[str],
topic_name: str,
) -> None:
error = _("Topic is not muted")
if stream_name is not None:
@ -55,7 +57,6 @@ def unmute_topic(
stream = access_stream_for_unmute_topic_by_id(user_profile, stream_id, error)
do_unmute_topic(user_profile, stream, topic_name)
return json_success()
@has_request_variables
@ -71,20 +72,22 @@ def update_muted_topic(
check_for_exactly_one_stream_arg(stream_id=stream_id, stream=stream)
if op == "add":
return mute_topic(
mute_topic(
user_profile=user_profile,
stream_id=stream_id,
stream_name=stream,
topic_name=topic,
date_muted=timezone_now(),
)
return json_success()
elif op == "remove":
return unmute_topic(
unmute_topic(
user_profile=user_profile,
stream_id=stream_id,
stream_name=stream,
topic_name=topic,
)
return json_success()
def mute_user(request: HttpRequest, user_profile: UserProfile, muted_user_id: int) -> HttpResponse: