streams: Fix leaking private streams after last user is removed.

When the last user on a private stream is removed, the stream is no
longer possible to administer, and thus should be marked as
deactivated, so that default streams entries are removed and it no
longer appears in the UI as a non-administerable broken stream.
This commit is contained in:
Tim Abbott 2017-06-04 10:32:41 -07:00
parent d21756c396
commit d2079cbb2e
2 changed files with 23 additions and 4 deletions

View File

@ -1791,13 +1791,20 @@ def bulk_remove_subscriptions(users, streams):
occupied_streams_after = list(get_occupied_streams(user_profile.realm))
new_vacant_streams = [stream for stream in
set(occupied_streams_before) - set(occupied_streams_after)
set(occupied_streams_before) - set(occupied_streams_after)]
new_vacant_private_streams = [stream for stream in new_vacant_streams
if stream.invite_only]
new_vacant_public_streams = [stream for stream in new_vacant_streams
if not stream.invite_only]
if new_vacant_streams:
if new_vacant_public_streams:
event = dict(type="stream", op="vacate",
streams=[stream.to_dict()
for stream in new_vacant_streams])
for stream in new_vacant_public_streams])
send_event(event, active_user_ids(user_profile.realm))
if new_vacant_private_streams:
# Deactivate any newly-vacant private streams
for stream in new_vacant_private_streams:
do_deactivate_stream(stream)
altered_user_dict = defaultdict(list) # type: Dict[int, List[UserProfile]]
streams_by_user = defaultdict(list) # type: Dict[int, List[Stream]]

View File

@ -218,6 +218,18 @@ class StreamAdminTest(ZulipTestCase):
do_deactivate_stream(stream)
self.assertEqual(0, DefaultStream.objects.filter(stream=stream).count())
def test_vacate_private_stream_removes_default_stream(self):
# type: () -> None
stream = self.make_stream('new_stream', invite_only=True)
self.subscribe_to_stream(self.example_email("hamlet"), stream.name)
do_add_default_stream(stream)
self.assertEqual(1, DefaultStream.objects.filter(stream=stream).count())
self.unsubscribe_from_stream(self.example_email("hamlet"), stream.name)
self.assertEqual(0, DefaultStream.objects.filter(stream=stream).count())
# Fetch stream again from database.
stream = Stream.objects.get(id=stream.id)
self.assertTrue(stream.deactivated)
def test_deactivate_stream_backend_requires_existing_stream(self):
# type: () -> None
user_profile = self.example_user('hamlet')