Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
message_helper: Convert module to typescript.
evykassirer48fc0bc75076
diff --git a/web/src/message_helper.js b/web/src/message_helper.jsdeleted file mode 100644index 0e380d9e436..00000000000--- a/web/src/message_helper.js+++ /dev/null@@ -1,95 +0,0 @@-import * as alert_words from "./alert_words";-import * as message_store from "./message_store";-import * as message_user_ids from "./message_user_ids";-import * as people from "./people";-import * as pm_conversations from "./pm_conversations";-import * as reactions from "./reactions";-import * as recent_senders from "./recent_senders";-import * as stream_topic_history from "./stream_topic_history";-import * as user_status from "./user_status";-import * as util from "./util";--export function process_new_message(message) {- // Call this function when processing a new message. After- // a message is processed and inserted into the message store- // cache, most modules use message_store.get to look at- // messages.- const cached_msg = message_store.get_cached_message(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 = message_store.set_message_booleans(message);- 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.maybe_get_user_by_id(message.sender_id);- if (sender) {- message.sender_full_name = sender.full_name;- message.sender_email = sender.email;- message.status_emoji_info = user_status.get_status_emoji(message.sender_id);- }-- if (!message.reactions) {- message.reactions = [];- }- // TODO: Rather than adding this field to the message object, it- // might be cleaner to create an independent map from message_id- // => clean_reactions data for the message, with care being taken- // to make sure reify_message_id moves the data structure- // properly.- message.clean_reactions = reactions.generate_clean_reactions(message);-- switch (message.type) {- case "stream":- if (message.topic === undefined) {- message.topic = message.subject;- }- delete message.subject;- message.is_stream = true;- 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_stream_message(message);- message_user_ids.add_user_id(message.sender_id);- break;-- case "private":- message.is_private = true;- message.reply_to = util.normalize_recipients(message_store.get_pm_emails(message));- message.display_reply_to = message_store.get_pm_full_names(- people.pm_with_user_ids(message),- );- message.pm_with_url = people.pm_with_url(message);- message.to_user_ids = people.pm_reply_user_string(message);-- pm_conversations.process_message(message);-- recent_senders.process_private_message(message);- if (people.is_my_user_id(message.sender_id)) {- for (const recip of message.display_recipient) {- message_user_ids.add_user_id(recip.id);- }- }- break;- }-- alert_words.process_message(message);- if (!message.reactions) {- message.reactions = [];- }- message_store.update_message_cache(message);- return message;-}diff --git a/web/src/message_helper.ts b/web/src/message_helper.tsnew file mode 100644index 00000000000..fd865548aa7--- /dev/null+++ b/web/src/message_helper.ts@@ -0,0 +1,126 @@+import _ from "lodash";+import assert from "minimalistic-assert";++import * as alert_words from "./alert_words";+import * as message_store from "./message_store";+import type {Message, RawMessage} from "./message_store";+import * as message_user_ids from "./message_user_ids";+import * as people from "./people";+import * as pm_conversations from "./pm_conversations";+import * as reactions from "./reactions";+import * as recent_senders from "./recent_senders";+import * as stream_data from "./stream_data";+import * as stream_topic_history from "./stream_topic_history";+import * as user_status from "./user_status";+import * as util from "./util";++export function process_new_message(raw_message: RawMessage): Message {+ // Call this function when processing a new message. After+ // a message is processed and inserted into the message store+ // cache, most modules use message_store.get to look at+ // messages.+ const cached_msg = message_store.get_cached_message(raw_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(raw_message) !== undefined) {+ util.set_match_data(cached_msg, raw_message);+ }+ return cached_msg;+ }++ const message_with_booleans = message_store.set_message_booleans(raw_message);+ people.extract_people_from_message(message_with_booleans);++ const sent_by_me = people.is_current_user(message_with_booleans.sender_email);+ people.maybe_incr_recipient_count({...message_with_booleans, sent_by_me});++ let status_emoji_info;+ const sender = people.maybe_get_user_by_id(message_with_booleans.sender_id);+ if (sender) {+ message_with_booleans.sender_full_name = sender.full_name;+ message_with_booleans.sender_email = sender.email;+ status_emoji_info = user_status.get_status_emoji(message_with_booleans.sender_id);+ }++ if (!raw_message.reactions) {+ raw_message.reactions = [];+ }+ // TODO: Rather than adding this field to the message object, it+ // might be cleaner to create an independent map from message_id+ // => clean_reactions data for the message, with care being taken+ // to make sure reify_message_id moves the data structure+ // properly.+ const clean_reactions = reactions.generate_clean_reactions(raw_message);++ let message: Message;+ if (message_with_booleans.type === "stream") {+ let topic = message_with_booleans.topic;+ if (topic === undefined) {+ topic = message_with_booleans.subject;+ }+ assert(topic !== undefined);+ stream_topic_history.add_message({+ stream_id: message_with_booleans.stream_id,+ topic_name: topic,+ message_id: message_with_booleans.id,+ });+ recent_senders.process_stream_message({+ stream_id: message_with_booleans.stream_id,+ topic,+ sender_id: message_with_booleans.sender_id,+ id: message_with_booleans.id,+ });++ message = {+ ..._.omit(message_with_booleans, ["reactions", "subject"]),+ sent_by_me,+ status_emoji_info,+ is_private: false,+ is_stream: true,+ reply_to: message_with_booleans.sender_email,+ topic,+ stream: stream_data.get_stream_name_from_id(message_with_booleans.stream_id),+ clean_reactions,+ display_reply_to: undefined,+ };+ message_user_ids.add_user_id(message.sender_id);+ } else {+ const pm_with_user_ids = people.pm_with_user_ids(message_with_booleans);+ assert(pm_with_user_ids !== undefined);+ const pm_with_url = people.pm_with_url(message_with_booleans);+ assert(pm_with_url !== undefined);+ const to_user_ids = people.pm_reply_user_string(message_with_booleans);+ assert(to_user_ids !== undefined);+ message = {+ ..._.omit(message_with_booleans, "reactions"),+ sent_by_me,+ status_emoji_info,+ is_private: true,+ is_stream: false,+ reply_to: util.normalize_recipients(message_store.get_pm_emails(message_with_booleans)),+ display_reply_to: message_store.get_pm_full_names(pm_with_user_ids),+ pm_with_url,+ to_user_ids,+ clean_reactions,+ };++ pm_conversations.process_message(message);++ recent_senders.process_private_message({+ to_user_ids,+ sender_id: message.sender_id,+ id: message.id,+ });+ if (people.is_my_user_id(message.sender_id)) {+ assert(typeof message.display_recipient !== "string");+ for (const recip of message.display_recipient) {+ message_user_ids.add_user_id(recip.id);+ }+ }+ }++ alert_words.process_message(message);+ message_store.update_message_cache(message);+ return message;+}diff --git a/web/src/message_store.ts b/web/src/message_store.tsindex 82e42184898..2074dac1ba2 100644--- a/web/src/message_store.ts+++ b/web/src/message_store.ts@@ -4,7 +4,6 @@ import * as blueslip from "./blueslip";import * as people from "./people";import type {Submessage, TopicLink} from "./types";import type {UserStatusEmojiInfo} from "./user_status";-import { RawReaction } from "./reactions";const stored_messages = new Map<number, Message>();@@ -70,7 +69,10 @@ export type RawMessage = {| {type: "stream";stream_id: number;- subject: string;+ // Messages that come from the server use `subject`.+ // Messages that come from `send_message` use `topic`.+ subject?: string;+ topic?: string;topic_links: TopicLink[];}) &@@ -109,11 +111,8 @@ export type MessageCleanReaction = {export type Message = (| Omit<MessageWithBooleans & {type: "private"}, "reactions">- | Omit<MessageWithBooleans & {type: "stream"}, "reactions">+ | Omit<MessageWithBooleans & {type: "stream"}, "reactions" | "subject">) & {- // Replaced by `clean_reactions` in `reactions.set_clean_reactions`.- reactions?: RawReaction[];- // Added in `reactions.set_clean_reactions`.clean_reactions: Map<string, MessageCleanReaction>;locally_echoed?: boolean;@@ -172,7 +171,7 @@ export function get(message_id: number): Message | undefined {return stored_messages.get(message_id);}-export function get_pm_emails(message: Message): string {+export function get_pm_emails(message: Message | MessageWithBooleans): string {const user_ids = people.pm_with_user_ids(message) ?? [];const emails = user_ids.map((user_id) => {diff --git a/web/src/people.ts b/web/src/people.tsindex bf62159db94..219467e873e 100644--- a/web/src/people.ts+++ b/web/src/people.ts@@ -480,7 +480,7 @@ export function get_recipients(user_ids_string: string): string {return names.join(", ");}-export function pm_reply_user_string(message: Message): string | undefined {+export function pm_reply_user_string(message: Message | MessageWithBooleans): string | undefined {const user_ids = pm_with_user_ids(message);if (!user_ids) {@@ -578,9 +578,7 @@ export function all_user_ids_in_pm(message: Message): number[] | undefined {return user_ids;}-export function pm_with_user_ids(- message: Message & {reply_to?: string; url?: string},-): number[] | undefined {+export function pm_with_user_ids(message: Message | MessageWithBooleans): number[] | undefined {if (message.type !== "private") {return undefined;}@@ -620,7 +618,7 @@ export function pm_perma_link(message: Message): string | undefined {return url;}-export function pm_with_url(message: Message): string | undefined {+export function pm_with_url(message: Message | MessageWithBooleans): string | undefined {const user_ids = pm_with_user_ids(message);if (!user_ids) {diff --git a/web/src/recent_senders.ts b/web/src/recent_senders.tsindex 8a84daf29b8..b3961e1bba6 100644--- a/web/src/recent_senders.ts+++ b/web/src/recent_senders.ts@@ -2,7 +2,6 @@ import _ from "lodash";import {FoldDict} from "./fold_dict";import * as message_store from "./message_store";-import type {Message} from "./message_store";import * as people from "./people";import type {User} from "./people";@@ -114,7 +113,12 @@ function add_topic_message(opts: {id_tracker.add(message_id);}-export function process_stream_message(message: Message & {type: "stream"}): void {+export function process_stream_message(message: {+ stream_id: number;+ topic: string;+ sender_id: number;+ id: number;+}): void {const stream_id = message.stream_id;const topic = message.topic;const sender_id = message.sender_id;diff --git a/web/src/util.ts b/web/src/util.tsindex 58f5454eda5..5eb2b1d6e91 100644--- a/web/src/util.ts+++ b/web/src/util.ts@@ -247,7 +247,7 @@ export function set_match_data(target: Message, source: MatchedMessage): void {target.match_content = source.match_content;}-export function get_match_topic(obj: Message): string | undefined {+export function get_match_topic(obj: Message | RawMessage): string | undefined {return obj.match_subject;}diff --git a/web/tests/dispatch.test.js b/web/tests/dispatch.test.jsindex f190decc843..2cb8163c748 100644--- a/web/tests/dispatch.test.js+++ b/web/tests/dispatch.test.js@@ -123,7 +123,6 @@ realm.realm_description = "already set description";// For data-oriented modules, just use them, don't stub them.const alert_words = zrequire("alert_words");const emoji = zrequire("emoji");-const message_helper = zrequire("message_helper");const message_store = zrequire("message_store");const people = zrequire("people");const user_status = zrequire("user_status");@@ -146,7 +145,7 @@ people.add_active_user(me);people.add_active_user(test_user);people.initialize_current_user(me.user_id);-message_helper.process_new_message(test_message);+message_store.update_message_cache(test_message);const realm_emoji = {};const emoji_codes = zrequire("../../static/generated/emoji/emoji_codes.json");diff --git a/web/tests/example5.test.js b/web/tests/example5.test.jsindex d850bdcb064..5abc1d80426 100644--- a/web/tests/example5.test.js+++ b/web/tests/example5.test.js@@ -91,6 +91,7 @@ run_test("insert_message", ({override}) => {id: 1001,content: "example content",topic: "Foo",+ type: "stream",};assert.equal(message_store.get(new_message.id), undefined);diff --git a/web/tests/message_events.test.js b/web/tests/message_events.test.jsindex e6adbf4dafe..fd8b4f49659 100644--- a/web/tests/message_events.test.js+++ b/web/tests/message_events.test.js@@ -121,6 +121,7 @@ run_test("update_messages", () => {assert.deepEqual(rendered_mgs, [{+ display_reply_to: undefined,alerted: false,clean_reactions: new Map(),collapsed: false,@@ -129,13 +130,13 @@ run_test("update_messages", () => {historical: false,id: 111,is_stream: true,+ is_private: false,last_edit_timestamp: undefined,mentioned: false,stream_wildcard_mentioned: false,topic_wildcard_mentioned: false,mentioned_me_directly: false,raw_content: "**new content**",- reactions: [],reply_to: alice.email,sender_email: alice.email,sender_full_name: alice.full_name,@@ -144,6 +145,7 @@ run_test("update_messages", () => {starred: false,status_emoji_info: undefined,stream_id: denmark.stream_id,+ stream: "Denmark",topic: "lunch",type: "stream",unread: true,diff --git a/web/tests/message_store.test.js b/web/tests/message_store.test.jsindex f644fd1d30f..7d58f4e85a4 100644--- a/web/tests/message_store.test.js+++ b/web/tests/message_store.test.js@@ -314,6 +314,7 @@ test("update_property", () => {sender_id: alice.user_id,small_avatar_url: "alice_url",stream_id: devel.stream_id,+ topic: "",display_recipient: devel.name,id: 100,};@@ -323,6 +324,7 @@ test("update_property", () => {sender_id: bob.user_id,small_avatar_url: "bob_url",stream_id: denmark.stream_id,+ topic: "",display_recipient: denmark.name,id: 101,};