import_utils: Fix history_public_to_subscribers being set incorrectly.

history_public_to_subscribers wasn't explicitly set when creating
streams via build_stream, thus relying on the model's default of False.
This lead to public streams being created with that value set to False,
which doesn't make sense.

We can solve this by inferring the correct value based on invite_only in
the build_stream funtion itself - rather than needing to add a flag
argument to it.

This commit also includes a migration to fix public stream with the
wrong history_public_to_subscribers value.

Fixes #21784.
This commit is contained in:
Mateusz Mandera 2022-04-27 18:05:48 +02:00 committed by Tim Abbott
parent bd072d79a4
commit 04fdf3e4d9
3 changed files with 32 additions and 0 deletions

View File

@ -441,6 +441,13 @@ def build_stream(
invite_only: bool = False,
stream_post_policy: int = 1,
) -> ZerverFieldsT:
# Other applications don't have the distinction of "private stream with public history"
# vs "private stream with hidden history" - and we've traditionally imported private "streams"
# of other products as private streams with hidden history.
# So we can set the history_public_to_subscribers value based on the invite_only flag.
history_public_to_subscribers = not invite_only
stream = Stream(
name=name,
deactivated=deactivated,
@ -450,6 +457,7 @@ def build_stream(
invite_only=invite_only,
id=stream_id,
stream_post_policy=stream_post_policy,
history_public_to_subscribers=history_public_to_subscribers,
)
stream_dict = model_to_dict(stream, exclude=["realm"])
stream_dict["realm"] = realm_id

View File

@ -0,0 +1,23 @@
from django.db import migrations
from django.db.backends.postgresql.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps
def fix_stream_history_public_to_subscribers(
apps: StateApps, schema_editor: DatabaseSchemaEditor
) -> None:
Stream = apps.get_model("zerver", "Stream")
Stream.objects.filter(
invite_only=False, is_in_zephyr_realm=False, history_public_to_subscribers=False
).update(history_public_to_subscribers=True)
class Migration(migrations.Migration):
dependencies = [
("zerver", "0389_userprofile_display_emoji_reaction_users"),
]
operations = [
migrations.RunPython(fix_stream_history_public_to_subscribers, elidable=True),
]

View File

@ -684,6 +684,7 @@ class SlackImporter(ZulipTestCase):
self.assertEqual(zerver_stream[0]["deactivated"], True)
self.assertEqual(zerver_stream[0]["description"], "no purpose")
self.assertEqual(zerver_stream[0]["invite_only"], False)
self.assertEqual(zerver_stream[0]["history_public_to_subscribers"], True)
self.assertEqual(zerver_stream[0]["realm"], realm_id)
self.assertEqual(zerver_stream[2]["id"], test_added_channels[zerver_stream[2]["name"]][1])