refactor: Extract message_helper.process_new_message.

We move the message_store.add_message_metadata function
(and all its dependencies) into a new module called
message_helper and rename the function to process_new_message.
(It does a bit more than adding message metadata, such
as updating our message store.)

We also have a "protected" interface now in message_store,
so that message_helper can access the message store:

    update_message_cache
    get_cached_message

Because update_message_cache is identical to
the former create_mock_message, we just renamed it
in the tests.

Most callers should use these functions:

    message_helper.process_new_message (setting)
    message_store.get (getting)

It's slightly annoying that the setter interface
is in a different module than the getter interface,
but that's how you break a bunch of dependencies.

We also extract the tiny message_user_ids class:

    user_ids()
    add_user_ids()

All the code moves here are pretty trivial, and
the code that was moved maintains 100% line
coverage.

The module name `message_helper` is not ideal, but it's a single
function and it'll save time to just do the topology change now and
leave thinking through the right name to later.
This commit is contained in:
Steve Howell
2021-03-28 15:57:53 +00:00
committed by Tim Abbott
parent 8798b0a134
commit 45d806d9aa
19 changed files with 160 additions and 144 deletions

View File

@@ -1,42 +1,23 @@
import * as alert_words from "./alert_words";
import * as blueslip from "./blueslip";
import * as message_list from "./message_list";
import * as people from "./people";
import * as pm_conversations from "./pm_conversations";
import * as recent_senders from "./recent_senders";
import * as stream_topic_history from "./stream_topic_history";
import * as util from "./util";
const stored_messages = new Map();
/*
We keep a set of user_ids for all people
who have sent stream messages or who have
been on PMs sent by the user.
export function update_message_cache(message) {
// You should only call this from message_helper (or in tests).
stored_messages.set(message.id, message);
}
We will use this in search to prevent really
large result sets for realms that have lots
of users who haven't sent messages recently.
We'll likely eventually want to replace this with
accessing some combination of data from recent_senders
and pm_conversations for better accuracy.
*/
const message_user_ids = new Set();
export function get_cached_message(message_id) {
// You should only call this from message_helper.
// Use the get() wrapper below for most other use cases.
return stored_messages.get(message_id);
}
export function clear_for_testing() {
stored_messages.clear();
message_user_ids.clear();
}
export function user_ids() {
return Array.from(message_user_ids);
}
export function create_mock_message(message) {
// For use in tests only. `id` is a required field,
// everything else is optional, as required in the test.
stored_messages.set(message.id, message);
}
export function get(message_id) {
@@ -149,73 +130,6 @@ export function update_booleans(message, flags) {
message.alerted = convert_flag("has_alert_word");
}
export function add_message_metadata(message) {
const cached_msg = stored_messages.get(message.id);
if (cached_msg !== undefined) {
// Copy the match topic and content over if they exist on
// the new message
if (util.get_match_topic(message) !== undefined) {
util.set_match_data(cached_msg, message);
}
return cached_msg;
}
message.sent_by_me = people.is_current_user(message.sender_email);
people.extract_people_from_message(message);
people.maybe_incr_recipient_count(message);
const sender = people.get_by_user_id(message.sender_id);
if (sender) {
message.sender_full_name = sender.full_name;
message.sender_email = sender.email;
}
// Convert topic even for PMs, as legacy code
// wants the empty field.
util.convert_message_topic(message);
switch (message.type) {
case "stream":
message.is_stream = true;
message.stream = message.display_recipient;
message.reply_to = message.sender_email;
stream_topic_history.add_message({
stream_id: message.stream_id,
topic_name: message.topic,
message_id: message.id,
});
recent_senders.process_message_for_senders(message);
message_user_ids.add(message.sender_id);
break;
case "private":
message.is_private = true;
message.reply_to = util.normalize_recipients(get_pm_emails(message));
message.display_reply_to = get_pm_full_names(message);
message.pm_with_url = people.pm_with_url(message);
message.to_user_ids = people.pm_reply_user_string(message);
process_message_for_recent_private_messages(message);
if (people.is_my_user_id(message.sender_id)) {
for (const recip of message.display_recipient) {
message_user_ids.add(recip.id);
}
}
break;
}
alert_words.process_message(message);
if (!message.reactions) {
message.reactions = [];
}
stored_messages.set(message.id, message);
return message;
}
export function update_property(property, value, info) {
switch (property) {
case "sender_full_name":