Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
export: Sync Subscription.is_user_active with exported UserProfile.
Alex Vandivercbf3ba640656
diff --git a/zerver/lib/export.py b/zerver/lib/export.pyindex 4c7e81d3fda..4ad6ed6964b 100644--- a/zerver/lib/export.py+++ b/zerver/lib/export.py@@ -1166,6 +1166,8 @@ def custom_process_subscription_in_realm_config(assert exportable_user_ids_from_context is Noneconsented_user_ids = set()+ exported_inactive_user_ids = context["exported_inactive_user_ids"]+for subscription in subscriptions:if subscription["user_profile"] in consented_user_ids:yield subscription@@ -1173,12 +1175,21 @@ def custom_process_subscription_in_realm_config(# We create a replacement Subscription, setting only the essential fields,# while allowing all the other ones to fall back to the defaults# defined in the model.+ #+ # is_user_active must match the is_active we're emitting on the+ # user's UserProfile row; otherwise the Subscription's+ # denormalized copy of UserProfile.is_active would contradict+ # the exported UserProfile (e.g., when a non-consenting user is+ # mirror-dummied to is_active=False).scrubbed_subscription = Subscription(id=subscription["id"],user_profile_id=subscription["user_profile"],recipient_id=subscription["recipient"],active=subscription["active"],- is_user_active=subscription["is_user_active"],+ is_user_active=(+ subscription["is_user_active"]+ and subscription["user_profile"] not in exported_inactive_user_ids+ ),# Letting the color be the default color for every stream would create a visually# jarring experience. Instead, we can pick colors randomly for a normal-feeling# experience, without leaking any information about the user's preferences.@@ -1399,6 +1410,17 @@ def custom_fetch_user_profile(response: TableData, context: Context) -> None:response["zerver_userprofile"] = normal_rowsresponse["zerver_userprofile_mirrordummy"] = dummy_rows+ # Subscription.is_user_active is a denormalized copy of+ # UserProfile.is_active. The export can force is_active=False on a+ # UserProfile row (mirror-dummying a non-consenting user, or+ # replacing the delivery_email of a NOBODY-visibility user in a+ # public export); custom_process_subscription_in_realm_config+ # consults this set so that scrubbed Subscription rows stay in+ # sync with the UserProfile rows we're emitting.+ context["exported_inactive_user_ids"] = {+ row["id"] for row in normal_rows + dummy_rows if not row["is_active"]+ }+def custom_fetch_user_profile_cross_realm(response: TableData, context: Context) -> None:realm = context["realm"]diff --git a/zerver/tests/test_import_export.py b/zerver/tests/test_import_export.pyindex 97275f28f28..9972f104fbc 100644--- a/zerver/tests/test_import_export.py+++ b/zerver/tests/test_import_export.py@@ -1445,6 +1445,12 @@ class RealmImportExportTest(ExportFile):# "user setting"-type attributes with default values, so the #foo color# will be overwritten in the process.self.assertNotEqual(exported_sub["color"], "#foo")+ # is_user_active on the exported Subscription must match+ # is_active on the exported UserProfile; hamlet (the+ # non-consenting user) is currently active but gets+ # mirror-dummied in a consent export, so his exported+ # Subscription rows must report is_user_active=False.+ self.assertFalse(exported_sub["is_user_active"])# Verify the export/scrubbing of user settings for consenting/non-consenting users.realm_user_default = RealmUserDefault.objects.get(realm=realm)