streams: Add get_default_value_for_history_public_to_subscribers().

This commit adds a function that makes it easier to get a default
value for Stream.history_public_to_subscribers when one isn't
explicitly provided.
This commit is contained in:
Eeshan Garg 2018-05-02 19:37:08 -02:30 committed by Tim Abbott
parent 7e379bbb76
commit d28d08e7da
2 changed files with 26 additions and 40 deletions

View File

@ -1561,12 +1561,11 @@ def send_stream_creation_event(stream: Stream, user_ids: List[int]) -> None:
streams=[stream.to_dict()]) streams=[stream.to_dict()])
send_event(event, user_ids) send_event(event, user_ids)
def create_stream_if_needed(realm: Realm, def get_default_value_for_history_public_to_subscribers(
stream_name: Text, realm: Realm,
*, invite_only: bool,
invite_only: bool=False, history_public_to_subscribers: Optional[bool]
history_public_to_subscribers: Optional[bool]=None, ) -> bool:
stream_description: Text="") -> Tuple[Stream, bool]:
if invite_only: if invite_only:
if history_public_to_subscribers is None: if history_public_to_subscribers is None:
# TODO: Once we have a UI for this feature, we'll remove # TODO: Once we have a UI for this feature, we'll remove
@ -1583,6 +1582,18 @@ def create_stream_if_needed(realm: Realm,
# not public to subscribers, even for public streams. # not public to subscribers, even for public streams.
history_public_to_subscribers = False history_public_to_subscribers = False
return history_public_to_subscribers
def create_stream_if_needed(realm: Realm,
stream_name: Text,
*,
invite_only: bool=False,
history_public_to_subscribers: Optional[bool]=None,
stream_description: Text="") -> Tuple[Stream, bool]:
history_public_to_subscribers = get_default_value_for_history_public_to_subscribers(
realm, invite_only, history_public_to_subscribers)
(stream, created) = Stream.objects.get_or_create( (stream, created) = Stream.objects.get_or_create(
realm=realm, realm=realm,
name__iexact=stream_name, name__iexact=stream_name,
@ -2938,22 +2949,11 @@ def do_change_bot_type(user_profile: UserProfile, value: int) -> None:
def do_change_stream_invite_only(stream: Stream, invite_only: bool, def do_change_stream_invite_only(stream: Stream, invite_only: bool,
history_public_to_subscribers: Optional[bool]=None) -> None: history_public_to_subscribers: Optional[bool]=None) -> None:
if invite_only: history_public_to_subscribers = get_default_value_for_history_public_to_subscribers(
if history_public_to_subscribers is None: stream.realm,
# TODO: Once we have a UI for this feature, we'll remove invite_only,
# settings.PRIVATE_STREAM_HISTORY_FOR_SUBSCRIBERS and set history_public_to_subscribers
# this to be False here )
history_public_to_subscribers = settings.PRIVATE_STREAM_HISTORY_FOR_SUBSCRIBERS
else:
# If we later decide to support public streams without
# history, we can remove this code path.
history_public_to_subscribers = True
if stream.realm.is_zephyr_mirror_realm:
# In the Zephyr mirroring model, history is unconditionally
# not public to subscribers, even for public streams.
history_public_to_subscribers = False
stream.invite_only = invite_only stream.invite_only = invite_only
stream.history_public_to_subscribers = history_public_to_subscribers stream.history_public_to_subscribers = history_public_to_subscribers
stream.save(update_fields=['invite_only', 'history_public_to_subscribers']) stream.save(update_fields=['invite_only', 'history_public_to_subscribers'])

View File

@ -19,7 +19,8 @@ from zerver.views.users import add_service
from zerver.lib.actions import ( from zerver.lib.actions import (
check_send_message, create_stream_if_needed, bulk_add_subscriptions, check_send_message, create_stream_if_needed, bulk_add_subscriptions,
get_display_recipient, bulk_remove_subscriptions, do_create_user, get_display_recipient, bulk_remove_subscriptions, do_create_user,
check_send_stream_message, gather_subscriptions check_send_stream_message, gather_subscriptions,
get_default_value_for_history_public_to_subscribers,
) )
from zerver.lib.stream_subscription import ( from zerver.lib.stream_subscription import (
@ -530,23 +531,8 @@ class ZulipTestCase(TestCase):
if realm is None: if realm is None:
realm = self.DEFAULT_REALM realm = self.DEFAULT_REALM
if invite_only: history_public_to_subscribers = get_default_value_for_history_public_to_subscribers(
if history_public_to_subscribers is None: realm, invite_only, history_public_to_subscribers)
# TODO: Once we have a UI for this feature, we'll remove
# settings.PRIVATE_STREAM_HISTORY_FOR_SUBSCRIBERS and set
# this to be False here
history_public_to_subscribers = settings.PRIVATE_STREAM_HISTORY_FOR_SUBSCRIBERS
else:
# If we later decide to support public streams without
# history, we can remove this code path.
history_public_to_subscribers = True
else:
history_public_to_subscribers = True
if realm.is_zephyr_mirror_realm:
# In the Zephyr mirroring model, history is unconditionally
# not public to subscribers, even for public streams.
history_public_to_subscribers = False
try: try:
stream = Stream.objects.create( stream = Stream.objects.create(