message booleans: Return new message instead of mutating a raw message.

This commit is contained in:
evykassirer 2024-05-28 13:07:25 -07:00 committed by Tim Abbott
parent 59a743de01
commit 50f5cf9ad8
4 changed files with 36 additions and 21 deletions

View File

@ -23,7 +23,7 @@ export function process_new_message(message) {
return cached_msg;
}
message_store.set_message_booleans(message);
message = message_store.set_message_booleans(message);
message.sent_by_me = people.is_current_user(message.sender_email);
people.extract_people_from_message(message);

View File

@ -1,3 +1,5 @@
import _ from "lodash";
import * as blueslip from "./blueslip";
import * as people from "./people";
import type {RawReaction} from "./reactions";
@ -193,30 +195,44 @@ export function get_pm_full_names(user_ids: number[]): string {
return names.join(", ");
}
export function set_message_booleans(message: Message): void {
export function set_message_booleans(message: RawMessage): MessageWithBooleans {
const flags = message.flags ?? [];
function convert_flag(flag_name: string): boolean {
return flags.includes(flag_name);
}
message.unread = !convert_flag("read");
message.historical = convert_flag("historical");
message.starred = convert_flag("starred");
message.mentioned =
convert_flag("mentioned") ||
convert_flag("stream_wildcard_mentioned") ||
convert_flag("topic_wildcard_mentioned");
message.mentioned_me_directly = convert_flag("mentioned");
message.stream_wildcard_mentioned = convert_flag("stream_wildcard_mentioned");
message.topic_wildcard_mentioned = convert_flag("topic_wildcard_mentioned");
message.collapsed = convert_flag("collapsed");
message.alerted = convert_flag("has_alert_word");
const converted_flags = {
unread: !convert_flag("read"),
historical: convert_flag("historical"),
starred: convert_flag("starred"),
mentioned:
convert_flag("mentioned") ||
convert_flag("stream_wildcard_mentioned") ||
convert_flag("topic_wildcard_mentioned"),
mentioned_me_directly: convert_flag("mentioned"),
stream_wildcard_mentioned: convert_flag("stream_wildcard_mentioned"),
topic_wildcard_mentioned: convert_flag("topic_wildcard_mentioned"),
collapsed: convert_flag("collapsed"),
alerted: convert_flag("has_alert_word"),
};
// Once we have set boolean flags here, the `flags` attribute is
// just a distraction, so we delete it. (All the downstream code
// uses booleans.)
delete message.flags;
// We have to return these separately because of how the `MessageWithBooleans`
// type is set up.
if (message.type === "private") {
return {
..._.omit(message, "flags"),
...converted_flags,
};
}
return {
..._.omit(message, "flags"),
...converted_flags,
};
}
export function update_booleans(message: Message, flags: string[]): void {

View File

@ -70,8 +70,7 @@ run_test("message_store", () => {
assert.equal(message_store.get(in_message.id), undefined);
message_helper.process_new_message(in_message);
const message = message_store.get(in_message.id);
assert.equal(in_message.alerted, true);
assert.equal(message, in_message);
assert.equal(message.alerted, true);
// There are more side effects.
const topic_names = stream_topic_history.get_recent_topic_names(denmark_stream.stream_id);
@ -91,8 +90,8 @@ run_test("unread", () => {
assert.equal(unread.num_unread_for_topic(stream_id, topic_name), 0);
const in_message = {...messages.isaac_to_denmark_stream};
message_store.set_message_booleans(in_message);
let in_message = {...messages.isaac_to_denmark_stream};
in_message = message_store.set_message_booleans(in_message);
unread.process_loaded_messages([in_message]);
assert.equal(unread.num_unread_for_topic(stream_id, topic_name), 1);

View File

@ -160,9 +160,9 @@ test("message_booleans_parity", () => {
// This test asserts that both have identical behavior for the
// flags common between them.
const assert_bool_match = (flags, expected_message) => {
const set_message = {topic: "set_message_booleans", flags};
let set_message = {topic: "set_message_booleans", flags};
const update_message = {topic: "update_booleans"};
message_store.set_message_booleans(set_message);
set_message = message_store.set_message_booleans(set_message);
message_store.update_booleans(update_message, flags);
for (const key of Object.keys(expected_message)) {
assert.equal(