2022-02-09 16:49:46 +01:00
|
|
|
from django.db import IntegrityError
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2020-01-17 16:01:00 +01:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2022-04-14 23:55:22 +02:00
|
|
|
from zerver.actions.muted_users import do_mute_user, do_unmute_user
|
2021-06-30 18:35:50 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2023-02-10 14:33:24 +01:00
|
|
|
from zerver.lib.muted_users import get_mute_object
|
2023-02-11 00:47:32 +01:00
|
|
|
from zerver.lib.response import json_success
|
2023-11-27 05:39:27 +01:00
|
|
|
from zerver.lib.users import access_user_by_id_including_cross_realm
|
2019-02-02 23:53:22 +01:00
|
|
|
from zerver.models import UserProfile
|
2017-08-30 02:19:34 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2021-03-27 12:23:32 +01:00
|
|
|
def mute_user(request: HttpRequest, user_profile: UserProfile, muted_user_id: int) -> HttpResponse:
|
|
|
|
if user_profile.id == muted_user_id:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("Cannot mute self"))
|
2021-03-27 12:23:32 +01:00
|
|
|
|
2023-10-17 15:25:07 +02:00
|
|
|
# Arguably, access_used_by_id is not quite the right check; in the
|
|
|
|
# corner case of a limited guest trying to mute a deactivated user
|
|
|
|
# who they no longer have access to because the user was
|
|
|
|
# deactivated... it might from a policy perspective be OK to allow
|
|
|
|
# that operation even though this API will reject it.
|
|
|
|
#
|
|
|
|
# But it's quite possibly something nobody will try to do, so we
|
|
|
|
# just reuse the existing shared code path.
|
2023-11-27 05:39:27 +01:00
|
|
|
muted_user = access_user_by_id_including_cross_realm(
|
2023-06-13 18:15:36 +02:00
|
|
|
user_profile, muted_user_id, allow_bots=True, allow_deactivated=True, for_admin=False
|
2021-07-07 16:55:52 +02:00
|
|
|
)
|
2021-03-27 12:23:32 +01:00
|
|
|
date_muted = timezone_now()
|
|
|
|
|
2022-03-25 00:45:35 +01:00
|
|
|
try:
|
|
|
|
do_mute_user(user_profile, muted_user, date_muted)
|
|
|
|
except IntegrityError:
|
|
|
|
raise JsonableError(_("User already muted"))
|
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|
2021-03-27 12:23:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
def unmute_user(
|
|
|
|
request: HttpRequest, user_profile: UserProfile, muted_user_id: int
|
|
|
|
) -> HttpResponse:
|
2023-11-27 05:39:27 +01:00
|
|
|
muted_user = access_user_by_id_including_cross_realm(
|
2023-06-13 18:15:36 +02:00
|
|
|
user_profile, muted_user_id, allow_bots=True, allow_deactivated=True, for_admin=False
|
2021-07-07 16:55:52 +02:00
|
|
|
)
|
2021-04-08 06:20:43 +02:00
|
|
|
mute_object = get_mute_object(user_profile, muted_user)
|
2021-03-27 12:23:32 +01:00
|
|
|
|
2021-04-08 06:20:43 +02:00
|
|
|
if mute_object is None:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("User is not muted"))
|
2021-03-27 12:23:32 +01:00
|
|
|
|
2021-04-08 06:20:43 +02:00
|
|
|
do_unmute_user(mute_object)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|