mirror of https://github.com/zulip/zulip.git
mute user: Record entries in RealmAuditLog.
This makes it so that RealmAuditLog entries are created when a user mutes/unmutes someone. We don't really need to store the time, but we do so anyways, because the `event_time` field is currently a non-nullable one in the `RealmAuditLog` model, and making it nullable would risk allowing not specifying the time in other more important code which also creates `RealmAuditLog` entries. This also fixes an incorrect test of successfully unmuting with the API. Earlier it did not mock the time in the `views/muting.py` code to return `mute_time`.
This commit is contained in:
parent
e1acfb9e9a
commit
9602aa1467
|
@ -6513,13 +6513,32 @@ def do_mute_user(
|
|||
event = dict(type="muted_users", muted_users=get_user_mutes(user_profile))
|
||||
send_event(user_profile.realm, event, [user_profile.id])
|
||||
|
||||
RealmAuditLog.objects.create(
|
||||
realm=user_profile.realm,
|
||||
acting_user=user_profile,
|
||||
modified_user=user_profile,
|
||||
event_type=RealmAuditLog.USER_MUTED,
|
||||
event_time=date_muted,
|
||||
extra_data=orjson.dumps({"muted_user_id": muted_user.id}).decode(),
|
||||
)
|
||||
|
||||
|
||||
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))
|
||||
send_event(user_profile.realm, event, [user_profile.id])
|
||||
|
||||
RealmAuditLog.objects.create(
|
||||
realm=user_profile.realm,
|
||||
acting_user=user_profile,
|
||||
modified_user=user_profile,
|
||||
event_type=RealmAuditLog.USER_UNMUTED,
|
||||
event_time=timezone_now(),
|
||||
extra_data=orjson.dumps({"unmuted_user_id": muted_user.id}).decode(),
|
||||
)
|
||||
|
||||
|
||||
def do_mark_hotspot_as_read(user: UserProfile, hotspot: str) -> None:
|
||||
UserHotspot.objects.get_or_create(user=user, hotspot=hotspot)
|
||||
|
|
|
@ -3293,6 +3293,9 @@ class AbstractRealmAuditLog(models.Model):
|
|||
SUBSCRIPTION_DEACTIVATED = 303
|
||||
SUBSCRIPTION_PROPERTY_CHANGED = 304
|
||||
|
||||
USER_MUTED = 350
|
||||
USER_UNMUTED = 351
|
||||
|
||||
STRIPE_CUSTOMER_CREATED = 401
|
||||
STRIPE_CARD_CHANGED = 402
|
||||
STRIPE_PLAN_CHANGED = 403
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
from datetime import datetime, timezone
|
||||
from unittest import mock
|
||||
|
||||
import orjson
|
||||
|
||||
from zerver.lib.test_classes import ZulipTestCase
|
||||
from zerver.lib.timestamp import datetime_to_timestamp
|
||||
from zerver.lib.user_mutes import get_mute_object, get_user_mutes
|
||||
from zerver.models import RealmAuditLog
|
||||
|
||||
|
||||
class MutedUsersTests(ZulipTestCase):
|
||||
|
@ -92,6 +95,22 @@ class MutedUsersTests(ZulipTestCase):
|
|||
)
|
||||
self.assertIsNotNone(get_mute_object(hamlet, cordelia))
|
||||
|
||||
audit_log_entries = list(
|
||||
RealmAuditLog.objects.filter(acting_user=hamlet, modified_user=hamlet).values_list(
|
||||
"event_type", "event_time", "extra_data"
|
||||
)
|
||||
)
|
||||
self.assertEqual(len(audit_log_entries), 1)
|
||||
audit_log_entry = audit_log_entries[0]
|
||||
self.assertEqual(
|
||||
audit_log_entry,
|
||||
(
|
||||
RealmAuditLog.USER_MUTED,
|
||||
mute_time,
|
||||
orjson.dumps({"muted_user_id": cordelia.id}).decode(),
|
||||
),
|
||||
)
|
||||
|
||||
def test_remove_muted_user_unmute_before_muting(self) -> None:
|
||||
hamlet = self.example_user("hamlet")
|
||||
self.login_user(hamlet)
|
||||
|
@ -107,12 +126,15 @@ class MutedUsersTests(ZulipTestCase):
|
|||
cordelia = self.example_user("cordelia")
|
||||
mute_time = datetime(2021, 1, 1, tzinfo=timezone.utc)
|
||||
|
||||
url = "/api/v1/users/me/muted_users/{}".format(cordelia.id)
|
||||
result = self.api_post(hamlet, url)
|
||||
self.assert_json_success(result)
|
||||
with mock.patch("zerver.views.muting.timezone_now", return_value=mute_time):
|
||||
url = "/api/v1/users/me/muted_users/{}".format(cordelia.id)
|
||||
result = self.api_post(hamlet, url)
|
||||
self.assert_json_success(result)
|
||||
|
||||
url = "/api/v1/users/me/muted_users/{}".format(cordelia.id)
|
||||
result = self.api_delete(hamlet, url)
|
||||
with mock.patch("zerver.lib.actions.timezone_now", return_value=mute_time):
|
||||
# To test that `RealmAuditLog` entry has correct `event_time`.
|
||||
url = "/api/v1/users/me/muted_users/{}".format(cordelia.id)
|
||||
result = self.api_delete(hamlet, url)
|
||||
|
||||
self.assert_json_success(result)
|
||||
self.assertNotIn(
|
||||
|
@ -123,3 +145,19 @@ class MutedUsersTests(ZulipTestCase):
|
|||
get_user_mutes(hamlet),
|
||||
)
|
||||
self.assertIsNone(get_mute_object(hamlet, cordelia))
|
||||
|
||||
audit_log_entries = list(
|
||||
RealmAuditLog.objects.filter(acting_user=hamlet, modified_user=hamlet)
|
||||
.values_list("event_type", "event_time", "extra_data")
|
||||
.order_by("id")
|
||||
)
|
||||
self.assertEqual(len(audit_log_entries), 2)
|
||||
audit_log_entry = audit_log_entries[1]
|
||||
self.assertEqual(
|
||||
audit_log_entry,
|
||||
(
|
||||
RealmAuditLog.USER_UNMUTED,
|
||||
mute_time,
|
||||
orjson.dumps({"unmuted_user_id": cordelia.id}).decode(),
|
||||
),
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue