refactor: Extract get_hot_topics.

This extraction will make a bit more sense when
we start doing bulk operations on a realm to
get digests, but even now, it encapsulates the
slightly complex way we cherry-pick the top 4
topics for a user.
This commit is contained in:
Steve Howell
2020-11-03 17:03:42 +00:00
committed by Tim Abbott
parent 5a6d6f81ff
commit 217967f743

View File

@@ -137,18 +137,13 @@ def get_recent_topic_activity(
topic_messages=topic_messages, topic_messages=topic_messages,
) )
def gather_hot_topics( def get_hot_topics(
user_profile: UserProfile,
topic_activity: TopicActivity, topic_activity: TopicActivity,
) -> List[Dict[str, Any]]: ) -> List[TopicKey]:
# Returns a list of dictionaries containing the templating # Get out top 4 hottest topics
# information for each hot topic.
topics_by_diversity = topic_activity.topics_by_diversity topics_by_diversity = topic_activity.topics_by_diversity
topics_by_length = topic_activity.topics_by_length topics_by_length = topic_activity.topics_by_length
topic_senders = topic_activity.topic_senders
topic_length = topic_activity.topic_length
topic_messages = topic_activity.topic_messages
# Get up to the 4 best topics from the diversity list # Get up to the 4 best topics from the diversity list
# and length list, filtering out overlapping topics. # and length list, filtering out overlapping topics.
@@ -166,6 +161,20 @@ def gather_hot_topics(
if num_convos < 4: if num_convos < 4:
hot_topics.extend(topics_by_diversity[num_convos:4]) hot_topics.extend(topics_by_diversity[num_convos:4])
return hot_topics
def gather_hot_topics(
user_profile: UserProfile,
hot_topics: List[TopicKey],
topic_activity: TopicActivity,
) -> List[Dict[str, Any]]:
# Returns a list of dictionaries containing the templating
# information for each hot topic.
topic_senders = topic_activity.topic_senders
topic_length = topic_activity.topic_length
topic_messages = topic_activity.topic_messages
hot_topic_render_payloads = [] hot_topic_render_payloads = []
for h in hot_topics: for h in hot_topics:
users = list(topic_senders[h]) users = list(topic_senders[h])
@@ -235,9 +244,10 @@ def handle_digest_email(user_profile_id: int, cutoff: float,
stream_ids = exclude_subscription_modified_streams(user_profile, home_view_streams, cutoff_date) stream_ids = exclude_subscription_modified_streams(user_profile, home_view_streams, cutoff_date)
topic_activity = get_recent_topic_activity(stream_ids, cutoff_date) topic_activity = get_recent_topic_activity(stream_ids, cutoff_date)
hot_topics = get_hot_topics(topic_activity)
# Gather hot conversations. # Gather hot conversations.
context["hot_conversations"] = gather_hot_topics(user_profile, topic_activity) context["hot_conversations"] = gather_hot_topics(user_profile, hot_topics, topic_activity)
# Gather new streams. # Gather new streams.
new_streams_count, new_streams = gather_new_streams( new_streams_count, new_streams = gather_new_streams(