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
|
|
|
|
from zerver.lib.response import json_success
|
2021-04-08 06:20:43 +02:00
|
|
|
from zerver.lib.user_mutes import get_mute_object
|
2021-03-27 12:23:32 +01:00
|
|
|
from zerver.lib.users import access_user_by_id
|
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
|
|
|
|
2021-07-07 16:55:52 +02:00
|
|
|
muted_user = access_user_by_id(
|
|
|
|
user_profile, muted_user_id, allow_bots=False, allow_deactivated=True, for_admin=False
|
|
|
|
)
|
2021-03-27 12:23:32 +01:00
|
|
|
date_muted = timezone_now()
|
|
|
|
|
2021-04-08 06:20:43 +02:00
|
|
|
if get_mute_object(user_profile, muted_user) is not None:
|
2021-06-30 18:35:50 +02:00
|
|
|
raise JsonableError(_("User already muted"))
|
2021-03-27 12:23:32 +01:00
|
|
|
|
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:
|
2021-07-07 16:55:52 +02:00
|
|
|
muted_user = access_user_by_id(
|
|
|
|
user_profile, muted_user_id, allow_bots=False, allow_deactivated=True, for_admin=False
|
|
|
|
)
|
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)
|