2023-11-19 19:45:19 +01:00
|
|
|
from datetime import datetime
|
2022-04-14 23:55:22 +02:00
|
|
|
|
2024-08-19 09:14:18 +02:00
|
|
|
from django.db import transaction
|
2022-04-14 23:55:22 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
|
|
|
from zerver.actions.message_flags import do_mark_muted_user_messages_as_read
|
2023-02-10 14:33:24 +01:00
|
|
|
from zerver.lib.muted_users import add_user_mute, get_user_mutes
|
2022-04-14 23:55:22 +02:00
|
|
|
from zerver.models import MutedUser, RealmAuditLog, UserProfile
|
2024-08-19 09:30:24 +02:00
|
|
|
from zerver.tornado.django_api import send_event_on_commit
|
2022-04-14 23:55:22 +02:00
|
|
|
|
|
|
|
|
2024-08-19 09:14:18 +02:00
|
|
|
@transaction.atomic(durable=True)
|
2022-04-14 23:55:22 +02:00
|
|
|
def do_mute_user(
|
|
|
|
user_profile: UserProfile,
|
|
|
|
muted_user: UserProfile,
|
2024-07-12 02:30:23 +02:00
|
|
|
date_muted: datetime | None = None,
|
2022-04-14 23:55:22 +02:00
|
|
|
) -> None:
|
|
|
|
if date_muted is None:
|
|
|
|
date_muted = timezone_now()
|
|
|
|
add_user_mute(user_profile, muted_user, date_muted)
|
|
|
|
do_mark_muted_user_messages_as_read(user_profile, muted_user)
|
|
|
|
event = dict(type="muted_users", muted_users=get_user_mutes(user_profile))
|
2024-08-19 09:14:18 +02:00
|
|
|
send_event_on_commit(user_profile.realm, event, [user_profile.id])
|
2022-04-14 23:55:22 +02:00
|
|
|
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=user_profile.realm,
|
|
|
|
acting_user=user_profile,
|
|
|
|
modified_user=user_profile,
|
|
|
|
event_type=RealmAuditLog.USER_MUTED,
|
|
|
|
event_time=date_muted,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={"muted_user_id": muted_user.id},
|
2022-04-14 23:55:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-08-19 09:30:24 +02:00
|
|
|
@transaction.atomic(durable=True)
|
2022-04-14 23:55:22 +02:00
|
|
|
def do_unmute_user(mute_object: MutedUser) -> None:
|
|
|
|
user_profile = mute_object.user_profile
|
|
|
|
muted_user = mute_object.muted_user
|
|
|
|
mute_object.delete()
|
|
|
|
event = dict(type="muted_users", muted_users=get_user_mutes(user_profile))
|
2024-08-19 09:30:24 +02:00
|
|
|
send_event_on_commit(user_profile.realm, event, [user_profile.id])
|
2022-04-14 23:55:22 +02:00
|
|
|
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=user_profile.realm,
|
|
|
|
acting_user=user_profile,
|
|
|
|
modified_user=user_profile,
|
|
|
|
event_type=RealmAuditLog.USER_UNMUTED,
|
|
|
|
event_time=timezone_now(),
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={"unmuted_user_id": muted_user.id},
|
2022-04-14 23:55:22 +02:00
|
|
|
)
|