streams: Extract stream_color library.

This is a pure code move.
This commit is contained in:
Steve Howell 2022-03-12 12:46:20 +00:00 committed by Tim Abbott
parent 200a89cef0
commit 8f99894302
6 changed files with 64 additions and 62 deletions

View File

@ -16,8 +16,9 @@ from analytics.models import (
StreamCount, StreamCount,
UserCount, 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.create_user import create_user
from zerver.lib.stream_color import STREAM_ASSIGNMENT_COLORS
from zerver.lib.timestamp import floor_to_day from zerver.lib.timestamp import floor_to_day
from zerver.models import Client, Realm, Recipient, Stream, Subscription, UserProfile from zerver.models import Client, Realm, Recipient, Stream, Subscription, UserProfile

View File

@ -12,8 +12,8 @@ from django.forms.models import model_to_dict
from typing_extensions import Protocol from typing_extensions import Protocol
from zerver.data_import.sequencer import NEXT_ID 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.avatar_hash import user_avatar_path_from_ids
from zerver.lib.stream_color import STREAM_ASSIGNMENT_COLORS as stream_colors
from zerver.models import ( from zerver.models import (
Attachment, Attachment,
Huddle, Huddle,

View File

@ -129,6 +129,7 @@ from zerver.lib.send_email import (
from zerver.lib.server_initialization import create_internal_realm, server_initialized from zerver.lib.server_initialization import create_internal_realm, server_initialized
from zerver.lib.sessions import delete_user_sessions from zerver.lib.sessions import delete_user_sessions
from zerver.lib.storage import static_path from zerver.lib.storage import static_path
from zerver.lib.stream_color import STREAM_ASSIGNMENT_COLORS, pick_colors
from zerver.lib.stream_subscription import ( from zerver.lib.stream_subscription import (
SubInfo, SubInfo,
bulk_get_private_peers, bulk_get_private_peers,
@ -291,33 +292,6 @@ ONBOARDING_TOTAL_MESSAGES = 1000
ONBOARDING_UNREAD_MESSAGES = 20 ONBOARDING_UNREAD_MESSAGES = 20
ONBOARDING_RECENT_TIMEDELTA = datetime.timedelta(weeks=1) 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: def create_historical_user_messages(*, user_id: int, message_ids: List[int]) -> None:
# Users can see and interact with messages sent to streams with # Users can see and interact with messages sent to streams with
@ -3710,36 +3684,6 @@ def internal_send_huddle_message(
return message_ids[0] 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( def validate_user_access_to_subscribers(
user_profile: Optional[UserProfile], stream: Stream user_profile: Optional[UserProfile], stream: Stream
) -> None: ) -> None:

View File

@ -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

View File

@ -11,7 +11,6 @@ from django.http import HttpResponse
from django.utils.timezone import now as timezone_now from django.utils.timezone import now as timezone_now
from zerver.lib.actions import ( from zerver.lib.actions import (
STREAM_ASSIGNMENT_COLORS,
bulk_add_subscriptions, bulk_add_subscriptions,
bulk_get_subscriber_user_ids, bulk_get_subscriber_user_ids,
bulk_remove_subscriptions, bulk_remove_subscriptions,
@ -38,12 +37,12 @@ from zerver.lib.actions import (
get_default_streams_for_realm, get_default_streams_for_realm,
get_topic_messages, get_topic_messages,
lookup_default_stream_groups, lookup_default_stream_groups,
pick_colors,
validate_user_access_to_subscribers_helper, validate_user_access_to_subscribers_helper,
) )
from zerver.lib.exceptions import JsonableError from zerver.lib.exceptions import JsonableError
from zerver.lib.message import UnreadStreamInfo, aggregate_unread_data, get_raw_unread_data from zerver.lib.message import UnreadStreamInfo, aggregate_unread_data, get_raw_unread_data
from zerver.lib.response import json_success from zerver.lib.response import json_success
from zerver.lib.stream_color import STREAM_ASSIGNMENT_COLORS, pick_colors
from zerver.lib.stream_subscription import ( from zerver.lib.stream_subscription import (
get_active_subscriptions_for_stream_id, get_active_subscriptions_for_stream_id,
num_subscribers_for_stream_id, num_subscribers_for_stream_id,

View File

@ -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 scripts.lib.zulip_tools import get_or_create_dev_uuid_var_path
from zerver.lib.actions import ( from zerver.lib.actions import (
STREAM_ASSIGNMENT_COLORS,
build_message_send_dict, build_message_send_dict,
check_add_realm_emoji, check_add_realm_emoji,
do_change_user_role, 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.push_notifications import logger as push_notifications_logger
from zerver.lib.server_initialization import create_internal_realm, create_users from zerver.lib.server_initialization import create_internal_realm, create_users
from zerver.lib.storage import static_path 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.types import ProfileFieldData
from zerver.lib.url_preview.preview import CACHE_NAME as PREVIEW_CACHE_NAME from zerver.lib.url_preview.preview import CACHE_NAME as PREVIEW_CACHE_NAME
from zerver.lib.user_groups import create_user_group from zerver.lib.user_groups import create_user_group