Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

message_events_util: Convert module to TypeScript.

Varun Singh8570cd81b142
diff --git a/tools/linter_lib/custom_check.py b/tools/linter_lib/custom_check.py
index 832b45795b0..d752a8d817c 100644
--- a/tools/linter_lib/custom_check.py
+++ b/tools/linter_lib/custom_check.py
@@ -119,6 +119,7 @@ js_rules = RuleList(
"web/src/message_store.ts",
"web/src/types.ts",
"web/src/util.ts",
+ "web/src/message_events_util.ts",
"web/src/message_helper.ts",
"web/src/server_message.ts",
"web/tests/",
diff --git a/tools/test-js-with-node b/tools/test-js-with-node
index 06ca238b272..d3bd6673e6b 100755
--- a/tools/test-js-with-node
+++ b/tools/test-js-with-node
@@ -137,7 +137,7 @@ EXEMPT_FILES = make_set(
"web/src/message_edit.js",
"web/src/message_edit_history.ts",
"web/src/message_events.js",
- "web/src/message_events_util.js",
+ "web/src/message_events_util.ts",
"web/src/message_feed_loading.ts",
"web/src/message_feed_top_notices.ts",
"web/src/message_fetch.js",
diff --git a/web/src/message_events_util.js b/web/src/message_events_util.ts
similarity index 82%
rename from web/src/message_events_util.js
rename to web/src/message_events_util.ts
index 740d3e060f1..0ec149534d2 100644
--- a/web/src/message_events_util.js
+++ b/web/src/message_events_util.ts
@@ -1,39 +1,59 @@
+import {z} from "zod";
+
import * as blueslip from "./blueslip";
import * as channel from "./channel";
import * as compose_notifications from "./compose_notifications";
import * as message_lists from "./message_lists";
+import type {MessageList, RenderInfo} from "./message_lists";
import * as message_store from "./message_store";
+import type {Message} from "./message_store";
import * as narrow_state from "./narrow_state";
import * as unread_ops from "./unread_ops";
import * as util from "./util";
-// TODO: Move this function to 'message_util.ts' once #30702 is merged.
-export function maybe_add_narrowed_messages(messages, msg_list, callback, attempt = 1) {
- const ids = [];
+const msg_match_narrow_api_response_schema = z.object({
+ messages: z.record(
+ z.string(),
+ z.object({
+ match_content: z.string(),
+ match_subject: z.string(),
+ }),
+ ),
+});
+
+export function maybe_add_narrowed_messages(
+ messages: Message[],
+ msg_list: MessageList,
+ callback: (messages: Message[], msg_list: MessageList) => RenderInfo | undefined,
+ attempt = 1,
+): void {
+ const ids: number[] = [];
for (const elem of messages) {
ids.push(elem.id);
}
- channel.get({
+ void channel.get({
url: "/json/messages/matches_narrow",
data: {
msg_ids: JSON.stringify(ids),
narrow: JSON.stringify(narrow_state.public_search_terms()),
},
timeout: 5000,
- success(data) {
+ success(raw_data) {
+ const data = msg_match_narrow_api_response_schema.parse(raw_data);
+
if (!narrow_state.is_message_feed_visible() || msg_list !== message_lists.current) {
// We unnarrowed or moved to Recent Conversations in the meantime.
return;
}
- let new_messages = [];
- const elsewhere_messages = [];
+ let new_messages: Message[] = [];
+ const elsewhere_messages: Message[] = [];
for (const elem of messages) {
if (Object.hasOwn(data.messages, elem.id)) {
- util.set_match_data(elem, data.messages[elem.id]);
+ util.set_match_data(elem, data.messages[elem.id]!);
new_messages.push(elem);
} else {
elsewhere_messages.push(elem);