From 8f99894302f1e2f12717af8bb4f20ca41fcc528d Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sat, 12 Mar 2022 12:46:20 +0000 Subject: [PATCH] streams: Extract stream_color library. This is a pure code move. --- .../commands/populate_analytics_db.py | 3 +- zerver/data_import/import_util.py | 2 +- zerver/lib/actions.py | 58 +------------------ zerver/lib/stream_color.py | 58 +++++++++++++++++++ zerver/tests/test_subs.py | 3 +- zilencer/management/commands/populate_db.py | 2 +- 6 files changed, 64 insertions(+), 62 deletions(-) create mode 100644 zerver/lib/stream_color.py diff --git a/analytics/management/commands/populate_analytics_db.py b/analytics/management/commands/populate_analytics_db.py index 6cf44b4bbc..99b73f72cc 100644 --- a/analytics/management/commands/populate_analytics_db.py +++ b/analytics/management/commands/populate_analytics_db.py @@ -16,8 +16,9 @@ from analytics.models import ( StreamCount, UserCount, ) -from zerver.lib.actions import STREAM_ASSIGNMENT_COLORS, do_change_user_role, do_create_realm +from zerver.lib.actions import do_change_user_role, do_create_realm from zerver.lib.create_user import create_user +from zerver.lib.stream_color import STREAM_ASSIGNMENT_COLORS from zerver.lib.timestamp import floor_to_day from zerver.models import Client, Realm, Recipient, Stream, Subscription, UserProfile diff --git a/zerver/data_import/import_util.py b/zerver/data_import/import_util.py index 6b818101ae..90d5ac1c27 100644 --- a/zerver/data_import/import_util.py +++ b/zerver/data_import/import_util.py @@ -12,8 +12,8 @@ from django.forms.models import model_to_dict from typing_extensions import Protocol from zerver.data_import.sequencer import NEXT_ID -from zerver.lib.actions import STREAM_ASSIGNMENT_COLORS as stream_colors from zerver.lib.avatar_hash import user_avatar_path_from_ids +from zerver.lib.stream_color import STREAM_ASSIGNMENT_COLORS as stream_colors from zerver.models import ( Attachment, Huddle, diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index d6de57a628..a0c74ab7b2 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -129,6 +129,7 @@ from zerver.lib.send_email import ( from zerver.lib.server_initialization import create_internal_realm, server_initialized from zerver.lib.sessions import delete_user_sessions from zerver.lib.storage import static_path +from zerver.lib.stream_color import STREAM_ASSIGNMENT_COLORS, pick_colors from zerver.lib.stream_subscription import ( SubInfo, bulk_get_private_peers, @@ -291,33 +292,6 @@ ONBOARDING_TOTAL_MESSAGES = 1000 ONBOARDING_UNREAD_MESSAGES = 20 ONBOARDING_RECENT_TIMEDELTA = datetime.timedelta(weeks=1) -STREAM_ASSIGNMENT_COLORS = [ - "#76ce90", - "#fae589", - "#a6c7e5", - "#e79ab5", - "#bfd56f", - "#f4ae55", - "#b0a5fd", - "#addfe5", - "#f5ce6e", - "#c2726a", - "#94c849", - "#bd86e5", - "#ee7e4a", - "#a6dcbf", - "#95a5fd", - "#53a063", - "#9987e1", - "#e4523d", - "#c2c2c2", - "#4f8de4", - "#c6a8ad", - "#e7cc4d", - "#c8bebf", - "#a47462", -] - def create_historical_user_messages(*, user_id: int, message_ids: List[int]) -> None: # Users can see and interact with messages sent to streams with @@ -3710,36 +3684,6 @@ def internal_send_huddle_message( return message_ids[0] -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] - - for i, recipient_id in enumerate(other_recipient_ids): - if i < len(available_colors): - color = available_colors[i] - else: - # We have to start re-using old colors, and we use recipient_id - # to choose the color. - color = STREAM_ASSIGNMENT_COLORS[recipient_id % len(STREAM_ASSIGNMENT_COLORS)] - result[recipient_id] = color - - return result - - def validate_user_access_to_subscribers( user_profile: Optional[UserProfile], stream: Stream ) -> None: diff --git a/zerver/lib/stream_color.py b/zerver/lib/stream_color.py new file mode 100644 index 0000000000..9dfcd4971a --- /dev/null +++ b/zerver/lib/stream_color.py @@ -0,0 +1,58 @@ +from typing import Dict, List, Set + +STREAM_ASSIGNMENT_COLORS = [ + "#76ce90", + "#fae589", + "#a6c7e5", + "#e79ab5", + "#bfd56f", + "#f4ae55", + "#b0a5fd", + "#addfe5", + "#f5ce6e", + "#c2726a", + "#94c849", + "#bd86e5", + "#ee7e4a", + "#a6dcbf", + "#95a5fd", + "#53a063", + "#9987e1", + "#e4523d", + "#c2c2c2", + "#4f8de4", + "#c6a8ad", + "#e7cc4d", + "#c8bebf", + "#a47462", +] + + +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] + + for i, recipient_id in enumerate(other_recipient_ids): + if i < len(available_colors): + color = available_colors[i] + else: + # We have to start re-using old colors, and we use recipient_id + # to choose the color. + color = STREAM_ASSIGNMENT_COLORS[recipient_id % len(STREAM_ASSIGNMENT_COLORS)] + result[recipient_id] = color + + return result diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py index 2231342033..a303f78a51 100644 --- a/zerver/tests/test_subs.py +++ b/zerver/tests/test_subs.py @@ -11,7 +11,6 @@ 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,12 +37,12 @@ from zerver.lib.actions import ( get_default_streams_for_realm, get_topic_messages, lookup_default_stream_groups, - pick_colors, validate_user_access_to_subscribers_helper, ) from zerver.lib.exceptions import JsonableError from zerver.lib.message import UnreadStreamInfo, aggregate_unread_data, get_raw_unread_data from zerver.lib.response import json_success +from zerver.lib.stream_color import STREAM_ASSIGNMENT_COLORS, pick_colors from zerver.lib.stream_subscription import ( get_active_subscriptions_for_stream_id, num_subscribers_for_stream_id, diff --git a/zilencer/management/commands/populate_db.py b/zilencer/management/commands/populate_db.py index a8405389cd..cf7dde2c16 100644 --- a/zilencer/management/commands/populate_db.py +++ b/zilencer/management/commands/populate_db.py @@ -19,7 +19,6 @@ from django.utils.timezone import timedelta as timezone_timedelta from scripts.lib.zulip_tools import get_or_create_dev_uuid_var_path from zerver.lib.actions import ( - STREAM_ASSIGNMENT_COLORS, build_message_send_dict, check_add_realm_emoji, do_change_user_role, @@ -36,6 +35,7 @@ from zerver.lib.onboarding import create_if_missing_realm_internal_bots from zerver.lib.push_notifications import logger as push_notifications_logger from zerver.lib.server_initialization import create_internal_realm, create_users from zerver.lib.storage import static_path +from zerver.lib.stream_color import STREAM_ASSIGNMENT_COLORS from zerver.lib.types import ProfileFieldData from zerver.lib.url_preview.preview import CACHE_NAME as PREVIEW_CACHE_NAME from zerver.lib.user_groups import create_user_group