audit-log: Move subscription event types to AuditLogEventType enum.

Event types moved: SUBSCRIPTION_CREATED, SUBSCRIPTION_ACTIVATED,
SUBSCRIPTION_DEACTIVATED, SUBSCRIPTION_PROPERTY_CHANGED.
This commit is contained in:
Lauryn Menard 2024-09-03 17:25:32 +02:00 committed by Tim Abbott
parent 56c8cbde1e
commit 10d161638e
9 changed files with 35 additions and 32 deletions

View File

@ -36,6 +36,7 @@ from zerver.models import (
UserProfile, UserProfile,
) )
from zerver.models.groups import SystemGroups from zerver.models.groups import SystemGroups
from zerver.models.realm_audit_logs import AuditLogEventType
class Command(ZulipBaseCommand): class Command(ZulipBaseCommand):
@ -139,7 +140,7 @@ class Command(ZulipBaseCommand):
modified_user=shylock, modified_user=shylock,
modified_stream=stream, modified_stream=stream,
event_last_message_id=0, event_last_message_id=0,
event_type=RealmAuditLog.SUBSCRIPTION_CREATED, event_type=AuditLogEventType.SUBSCRIPTION_CREATED,
event_time=installation_time, event_time=installation_time,
) )

View File

@ -67,6 +67,7 @@ from zerver.models import (
UserProfile, UserProfile,
) )
from zerver.models.groups import SystemGroups from zerver.models.groups import SystemGroups
from zerver.models.realm_audit_logs import AuditLogEventType
from zerver.models.users import active_non_guest_user_ids, active_user_ids, get_system_bot from zerver.models.users import active_non_guest_user_ids, active_user_ids, get_system_bot
from zerver.tornado.django_api import send_event_on_commit from zerver.tornado.django_api import send_event_on_commit
@ -500,8 +501,8 @@ def bulk_add_subs_to_db_with_logging(
event_time=event_time, event_time=event_time,
) )
for event_type, subs in [ for event_type, subs in [
(RealmAuditLog.SUBSCRIPTION_CREATED, subs_to_add), (AuditLogEventType.SUBSCRIPTION_CREATED, subs_to_add),
(RealmAuditLog.SUBSCRIPTION_ACTIVATED, subs_to_activate), (AuditLogEventType.SUBSCRIPTION_ACTIVATED, subs_to_activate),
] ]
for sub_info in subs for sub_info in subs
] ]
@ -1066,7 +1067,7 @@ def bulk_remove_subscriptions(
modified_user=sub_info.user, modified_user=sub_info.user,
modified_stream=sub_info.stream, modified_stream=sub_info.stream,
event_last_message_id=event_last_message_id, event_last_message_id=event_last_message_id,
event_type=RealmAuditLog.SUBSCRIPTION_DEACTIVATED, event_type=AuditLogEventType.SUBSCRIPTION_DEACTIVATED,
event_time=event_time, event_time=event_time,
) )
for sub_info in subs_to_deactivate for sub_info in subs_to_deactivate
@ -1124,7 +1125,7 @@ def do_change_subscription_property(
event_time = timezone_now() event_time = timezone_now()
RealmAuditLog.objects.create( RealmAuditLog.objects.create(
realm=user_profile.realm, realm=user_profile.realm,
event_type=RealmAuditLog.SUBSCRIPTION_PROPERTY_CHANGED, event_type=AuditLogEventType.SUBSCRIPTION_PROPERTY_CHANGED,
event_time=event_time, event_time=event_time,
modified_user=user_profile, modified_user=user_profile,
acting_user=acting_user, acting_user=acting_user,

View File

@ -286,9 +286,9 @@ def get_user_stream_map(user_ids: list[int], cutoff_date: datetime) -> dict[int,
bugs for any of those classes of users. bugs for any of those classes of users.
""" """
events = [ events = [
RealmAuditLog.SUBSCRIPTION_CREATED, AuditLogEventType.SUBSCRIPTION_CREATED,
RealmAuditLog.SUBSCRIPTION_ACTIVATED, AuditLogEventType.SUBSCRIPTION_ACTIVATED,
RealmAuditLog.SUBSCRIPTION_DEACTIVATED, AuditLogEventType.SUBSCRIPTION_DEACTIVATED,
] ]
# This uses the zerver_realmauditlog_user_subscriptions_idx # This uses the zerver_realmauditlog_user_subscriptions_idx
# partial index on RealmAuditLog which is specifically for those # partial index on RealmAuditLog which is specifically for those

View File

@ -267,7 +267,7 @@ def create_subscription_events(data: TableData, realm_id: int) -> None:
modified_stream_id=stream_id, modified_stream_id=stream_id,
event_last_message_id=event_last_message_id, event_last_message_id=event_last_message_id,
event_time=event_time, event_time=event_time,
event_type=RealmAuditLog.SUBSCRIPTION_CREATED, event_type=AuditLogEventType.SUBSCRIPTION_CREATED,
) )
) )
RealmAuditLog.objects.bulk_create(all_subscription_logs) RealmAuditLog.objects.bulk_create(all_subscription_logs)

View File

@ -73,7 +73,7 @@ def filter_by_subscription_history(
event_last_message_id = assert_is_not_none(log_entry.event_last_message_id) event_last_message_id = assert_is_not_none(log_entry.event_last_message_id)
if log_entry.event_type == RealmAuditLog.SUBSCRIPTION_DEACTIVATED: if log_entry.event_type == AuditLogEventType.SUBSCRIPTION_DEACTIVATED:
# If the event shows the user was unsubscribed after # If the event shows the user was unsubscribed after
# event_last_message_id, we know they must have been # event_last_message_id, we know they must have been
# subscribed immediately before the event. # subscribed immediately before the event.
@ -83,8 +83,8 @@ def filter_by_subscription_history(
else: else:
break break
elif log_entry.event_type in ( elif log_entry.event_type in (
RealmAuditLog.SUBSCRIPTION_ACTIVATED, AuditLogEventType.SUBSCRIPTION_ACTIVATED,
RealmAuditLog.SUBSCRIPTION_CREATED, AuditLogEventType.SUBSCRIPTION_CREATED,
): ):
initial_msg_count = len(stream_messages) initial_msg_count = len(stream_messages)
for i, stream_message in enumerate(stream_messages): for i, stream_message in enumerate(stream_messages):
@ -104,8 +104,8 @@ def filter_by_subscription_history(
# event was a subscription_deactivated then we don't want to create # event was a subscription_deactivated then we don't want to create
# UserMessage rows for any of the remaining messages. # UserMessage rows for any of the remaining messages.
if len(stream_messages) > 0 and stream_subscription_logs[-1].event_type in ( if len(stream_messages) > 0 and stream_subscription_logs[-1].event_type in (
RealmAuditLog.SUBSCRIPTION_ACTIVATED, AuditLogEventType.SUBSCRIPTION_ACTIVATED,
RealmAuditLog.SUBSCRIPTION_CREATED, AuditLogEventType.SUBSCRIPTION_CREATED,
): ):
message_ids.update(stream_message["id"] for stream_message in stream_messages) message_ids.update(stream_message["id"] for stream_message in stream_messages)
return sorted(message_ids) return sorted(message_ids)
@ -165,9 +165,9 @@ def add_missing_messages(user_profile: UserProfile) -> None:
# this set changes, the partial index must be updated as well, to # this set changes, the partial index must be updated as well, to
# keep this query performant # keep this query performant
events = [ events = [
RealmAuditLog.SUBSCRIPTION_CREATED, AuditLogEventType.SUBSCRIPTION_CREATED,
RealmAuditLog.SUBSCRIPTION_DEACTIVATED, AuditLogEventType.SUBSCRIPTION_DEACTIVATED,
RealmAuditLog.SUBSCRIPTION_ACTIVATED, AuditLogEventType.SUBSCRIPTION_ACTIVATED,
] ]
# Important: We order first by event_last_message_id, which is the # Important: We order first by event_last_message_id, which is the
@ -193,7 +193,7 @@ def add_missing_messages(user_profile: UserProfile) -> None:
recipient_ids = [] recipient_ids = []
for sub in all_stream_subs: for sub in all_stream_subs:
stream_subscription_logs = all_stream_subscription_logs[sub["recipient__type_id"]] stream_subscription_logs = all_stream_subscription_logs[sub["recipient__type_id"]]
if stream_subscription_logs[-1].event_type == RealmAuditLog.SUBSCRIPTION_DEACTIVATED: if stream_subscription_logs[-1].event_type == AuditLogEventType.SUBSCRIPTION_DEACTIVATED:
assert stream_subscription_logs[-1].event_last_message_id is not None assert stream_subscription_logs[-1].event_last_message_id is not None
if ( if (
stream_subscription_logs[-1].event_last_message_id stream_subscription_logs[-1].event_last_message_id

View File

@ -66,6 +66,11 @@ class AuditLogEventType(IntEnum):
REALM_LINKIFIERS_REORDERED = 228 REALM_LINKIFIERS_REORDERED = 228
REALM_IMPORTED = 229 REALM_IMPORTED = 229
SUBSCRIPTION_CREATED = 301
SUBSCRIPTION_ACTIVATED = 302
SUBSCRIPTION_DEACTIVATED = 303
SUBSCRIPTION_PROPERTY_CHANGED = 304
class AbstractRealmAuditLog(models.Model): class AbstractRealmAuditLog(models.Model):
"""Defines fields common to RealmAuditLog and RemoteRealmAuditLog.""" """Defines fields common to RealmAuditLog and RemoteRealmAuditLog."""
@ -86,11 +91,6 @@ class AbstractRealmAuditLog(models.Model):
extra_data = models.JSONField(default=dict, encoder=DjangoJSONEncoder) extra_data = models.JSONField(default=dict, encoder=DjangoJSONEncoder)
# Event types # Event types
SUBSCRIPTION_CREATED = 301
SUBSCRIPTION_ACTIVATED = 302
SUBSCRIPTION_DEACTIVATED = 303
SUBSCRIPTION_PROPERTY_CHANGED = 304
USER_MUTED = 350 USER_MUTED = 350
USER_UNMUTED = 351 USER_UNMUTED = 351
@ -239,9 +239,9 @@ class RealmAuditLog(AbstractRealmAuditLog):
fields=["modified_user", "modified_stream"], fields=["modified_user", "modified_stream"],
condition=Q( condition=Q(
event_type__in=[ event_type__in=[
AbstractRealmAuditLog.SUBSCRIPTION_CREATED, AuditLogEventType.SUBSCRIPTION_CREATED,
AbstractRealmAuditLog.SUBSCRIPTION_ACTIVATED, AuditLogEventType.SUBSCRIPTION_ACTIVATED,
AbstractRealmAuditLog.SUBSCRIPTION_DEACTIVATED, AuditLogEventType.SUBSCRIPTION_DEACTIVATED,
] ]
), ),
), ),

View File

@ -393,7 +393,7 @@ class TestRealmAuditLog(ZulipTestCase):
acting_user = self.example_user("iago") acting_user = self.example_user("iago")
bulk_add_subscriptions(user.realm, [stream], [user], acting_user=acting_user) bulk_add_subscriptions(user.realm, [stream], [user], acting_user=acting_user)
subscription_creation_logs = RealmAuditLog.objects.filter( subscription_creation_logs = RealmAuditLog.objects.filter(
event_type=RealmAuditLog.SUBSCRIPTION_CREATED, event_type=AuditLogEventType.SUBSCRIPTION_CREATED,
event_time__gte=now, event_time__gte=now,
acting_user=acting_user, acting_user=acting_user,
modified_user=user, modified_user=user,
@ -407,7 +407,7 @@ class TestRealmAuditLog(ZulipTestCase):
bulk_remove_subscriptions(realm, [user], [stream], acting_user=acting_user) bulk_remove_subscriptions(realm, [user], [stream], acting_user=acting_user)
subscription_deactivation_logs = RealmAuditLog.objects.filter( subscription_deactivation_logs = RealmAuditLog.objects.filter(
event_type=RealmAuditLog.SUBSCRIPTION_DEACTIVATED, event_type=AuditLogEventType.SUBSCRIPTION_DEACTIVATED,
event_time__gte=now, event_time__gte=now,
acting_user=acting_user, acting_user=acting_user,
modified_user=user, modified_user=user,
@ -687,7 +687,7 @@ class TestRealmAuditLog(ZulipTestCase):
self.assertEqual( self.assertEqual(
RealmAuditLog.objects.filter( RealmAuditLog.objects.filter(
realm=user.realm, realm=user.realm,
event_type=RealmAuditLog.SUBSCRIPTION_PROPERTY_CHANGED, event_type=AuditLogEventType.SUBSCRIPTION_PROPERTY_CHANGED,
event_time__gte=now, event_time__gte=now,
acting_user=user, acting_user=user,
modified_user=user, modified_user=user,

View File

@ -1355,7 +1355,7 @@ class SlackImporter(ZulipTestCase):
self.assertEqual( self.assertEqual(
realmauditlog_event_type, realmauditlog_event_type,
{ {
RealmAuditLog.SUBSCRIPTION_CREATED, AuditLogEventType.SUBSCRIPTION_CREATED,
AuditLogEventType.REALM_PLAN_TYPE_CHANGED, AuditLogEventType.REALM_PLAN_TYPE_CHANGED,
AuditLogEventType.REALM_PROPERTY_CHANGED, AuditLogEventType.REALM_PROPERTY_CHANGED,
AuditLogEventType.REALM_CREATED, AuditLogEventType.REALM_CREATED,

View File

@ -71,6 +71,7 @@ from zerver.models import (
from zerver.models.alert_words import flush_alert_word from zerver.models.alert_words import flush_alert_word
from zerver.models.clients import get_client from zerver.models.clients import get_client
from zerver.models.onboarding_steps import OnboardingStep from zerver.models.onboarding_steps import OnboardingStep
from zerver.models.realm_audit_logs import AuditLogEventType
from zerver.models.realms import WildcardMentionPolicyEnum, get_realm from zerver.models.realms import WildcardMentionPolicyEnum, get_realm
from zerver.models.recipients import get_or_create_direct_message_group from zerver.models.recipients import get_or_create_direct_message_group
from zerver.models.streams import get_stream from zerver.models.streams import get_stream
@ -171,7 +172,7 @@ def subscribe_users_to_streams(realm: Realm, stream_dict: dict[str, dict[str, An
modified_user=profile, modified_user=profile,
modified_stream=stream, modified_stream=stream,
event_last_message_id=0, event_last_message_id=0,
event_type=RealmAuditLog.SUBSCRIPTION_CREATED, event_type=AuditLogEventType.SUBSCRIPTION_CREATED,
event_time=event_time, event_time=event_time,
) )
all_subscription_logs.append(log) all_subscription_logs.append(log)
@ -751,7 +752,7 @@ class Command(ZulipBaseCommand):
modified_user=profile, modified_user=profile,
modified_stream_id=recipient.type_id, modified_stream_id=recipient.type_id,
event_last_message_id=0, event_last_message_id=0,
event_type=RealmAuditLog.SUBSCRIPTION_CREATED, event_type=AuditLogEventType.SUBSCRIPTION_CREATED,
event_time=event_time, event_time=event_time,
) )
all_subscription_logs.append(log) all_subscription_logs.append(log)