Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

stream colors: Fix stream color assignment.

Steve Howell966d88a78a6c
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py
index a5bc9cd8803..7b8e108a112 100644
--- a/zerver/lib/actions.py
+++ b/zerver/lib/actions.py
@@ -3596,14 +3596,32 @@ def internal_send_huddle_message(
return message_ids[0]
-def pick_color(used_colors: Set[str]) -> str:
- # These colors are shared with the palette in stream_settings_ui.js.
+def pick_colors(
+ used_colors: Set[str], color_map: Dict[int, str], recipient_ids: List[int]
+) -> Dict[int, str]:
+ used_colors = set(used_colors)
+ recipient_ids = sorted(recipient_ids)
+ result = {}
+
+ other_recipient_ids = []
+ for recipient_id in recipient_ids:
+ if recipient_id in color_map:
+ color = color_map[recipient_id]
+ result[recipient_id] = color
+ used_colors.add(color)
+ else:
+ other_recipient_ids.append(recipient_id)
+
available_colors = [s for s in STREAM_ASSIGNMENT_COLORS if s not in used_colors]
- if available_colors:
- return available_colors[0]
- else:
- return STREAM_ASSIGNMENT_COLORS[len(used_colors) % len(STREAM_ASSIGNMENT_COLORS)]
+ for i, recipient_id in enumerate(other_recipient_ids):
+ if i < len(available_colors):
+ color = available_colors[i]
+ else:
+ color = STREAM_ASSIGNMENT_COLORS[i % len(STREAM_ASSIGNMENT_COLORS)]
+ result[recipient_id] = color
+
+ return result
def validate_user_access_to_subscribers(
@@ -3858,6 +3876,12 @@ def bulk_add_subscriptions(
recipient_ids = [stream.recipient_id for stream in streams]
recipient_id_to_stream = {stream.recipient_id: stream for stream in streams}
+ recipient_color_map = {}
+ for stream in streams:
+ color: Optional[str] = color_map.get(stream.name, None)
+ if color is not None:
+ recipient_color_map[stream.recipient_id] = color
+
used_colors_for_user_ids: Dict[int, Set[str]] = get_used_colors_for_user_ids(user_ids)
existing_subs = Subscription.objects.filter(
@@ -3875,12 +3899,11 @@ def bulk_add_subscriptions(
subs_to_add: List[SubInfo] = []
for user_profile in users:
my_subs = subs_by_user[user_profile.id]
- used_colors = used_colors_for_user_ids.get(user_profile.id, set())
# Make a fresh set of all new recipient ids, and then we will
# remove any for which our user already has a subscription
# (and we'll re-activate any subscriptions as needed).
- new_recipient_ids = {stream.recipient_id for stream in streams}
+ new_recipient_ids: Set[int] = {stream.recipient_id for stream in streams}
for sub in my_subs:
if sub.recipient_id in new_recipient_ids:
@@ -3892,14 +3915,12 @@ def bulk_add_subscriptions(
else:
subs_to_activate.append(sub_info)
+ used_colors = used_colors_for_user_ids.get(user_profile.id, set())
+ user_color_map = pick_colors(used_colors, recipient_color_map, list(new_recipient_ids))
+
for recipient_id in new_recipient_ids:
stream = recipient_id_to_stream[recipient_id]
-
- if stream.name in color_map:
- color = color_map[stream.name]
- else:
- color = pick_color(used_colors)
- used_colors.add(color)
+ color = user_color_map[recipient_id]
sub = Subscription(
user_profile=user_profile,
diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py
index fbdfd289c12..d012b6a9b3a 100644
--- a/zerver/tests/test_subs.py
+++ b/zerver/tests/test_subs.py
@@ -11,6 +11,7 @@ from django.http import HttpResponse
from django.utils.timezone import now as timezone_now
from zerver.lib.actions import (
+ STREAM_ASSIGNMENT_COLORS,
bulk_add_subscriptions,
bulk_get_subscriber_user_ids,
bulk_remove_subscriptions,
@@ -38,6 +39,7 @@ from zerver.lib.actions import (
get_default_streams_for_realm,
get_topic_messages,
lookup_default_stream_groups,
+ pick_colors,
round_to_2_significant_digits,
validate_user_access_to_subscribers_helper,
)
@@ -97,6 +99,57 @@ class TestMiscStuff(ZulipTestCase):
self.assertIn("* Verona", s)
self.assertNotIn("* Denmark", s)
+ def test_pick_colors(self) -> None:
+ used_colors: Set[str] = set()
+ color_map: Dict[int, str] = {}
+ recipient_ids = list(range(30))
+ user_color_map = pick_colors(used_colors, color_map, recipient_ids)
+ self.assertEqual(
+ user_color_map,
+ {
+ 0: "#76ce90",
+ 1: "#fae589",
+ 2: "#a6c7e5",
+ 3: "#e79ab5",
+ 4: "#bfd56f",
+ 5: "#f4ae55",
+ 6: "#b0a5fd",
+ 7: "#addfe5",
+ 8: "#f5ce6e",
+ 9: "#c2726a",
+ 10: "#94c849",
+ 11: "#bd86e5",
+ 12: "#ee7e4a",
+ 13: "#a6dcbf",
+ 14: "#95a5fd",
+ 15: "#53a063",
+ 16: "#9987e1",
+ 17: "#e4523d",
+ 18: "#c2c2c2",
+ 19: "#4f8de4",
+ 20: "#c6a8ad",
+ 21: "#e7cc4d",
+ 22: "#c8bebf",
+ 23: "#a47462",
+ # start repeating
+ 24: "#76ce90",
+ 25: "#fae589",
+ 26: "#a6c7e5",
+ 27: "#e79ab5",
+ 28: "#bfd56f",
+ 29: "#f4ae55",
+ },
+ )
+
+ color_map = {98: "color98", 99: "color99"}
+ used_colors = set(STREAM_ASSIGNMENT_COLORS) - {"#c6a8ad", "#9987e1"}
+ recipient_ids = [99, 98, 1, 2, 3, 4]
+ user_color_map = pick_colors(used_colors, color_map, recipient_ids)
+ self.assertEqual(
+ user_color_map,
+ {98: "color98", 99: "color99", 1: "#9987e1", 2: "#c6a8ad", 3: "#a6c7e5", 4: "#e79ab5"},
+ )
+
def test_empty_results(self) -> None:
# These are essentially just tests to ensure line
# coverage for codepaths that won't ever really be