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 flags: Convert module to typescript.

evykassirer08bee0f1809a
diff --git a/web/src/message_flags.js b/web/src/message_flags.ts
similarity index 68%
rename from web/src/message_flags.js
rename to web/src/message_flags.ts
index cbcbfe04ac2..e657eb54bae 100644
--- a/web/src/message_flags.js
+++ b/web/src/message_flags.ts
@@ -1,10 +1,13 @@
+import type {DebouncedFunc} from "lodash";
import _ from "lodash";
+import {z} from "zod";
import * as channel from "./channel";
+import type {Message} from "./message_store";
import * as starred_messages from "./starred_messages";
-export function send_flag_update_for_messages(msg_ids, flag, op) {
- channel.post({
+export function send_flag_update_for_messages(msg_ids: number[], flag: string, op: string): void {
+ void channel.post({
url: "/json/messages/flags",
data: {
messages: JSON.stringify(msg_ids),
@@ -16,9 +19,9 @@ export function send_flag_update_for_messages(msg_ids, flag, op) {
export const _unread_batch_size = 1000;
export const send_read = (function () {
- let queue = [];
- let start;
- function server_request() {
+ let queue: Message[] = [];
+ let start: DebouncedFunc<() => void>;
+ function server_request(): void {
// Wait for server IDs before sending flags
const real_msgs = queue.filter((msg) => !msg.locally_echoed);
const real_msg_ids = real_msgs.map((msg) => msg.id);
@@ -33,7 +36,7 @@ export const send_read = (function () {
// We have some real IDs. If there are any left in the queue when this
// call finishes, they will be handled in the success callback.
- channel.post({
+ void channel.post({
url: "/json/messages/flags",
data: {messages: JSON.stringify(real_msg_ids_batch), op: "add", flag: "read"},
success() {
@@ -49,7 +52,7 @@ export const send_read = (function () {
start = _.throttle(server_request, 1000);
- function add(messages) {
+ function add(messages: Message[]): void {
queue = [...queue, ...messages];
start();
}
@@ -57,28 +60,39 @@ export const send_read = (function () {
return add;
})();
-export function mark_as_read(message_ids) {
+export function mark_as_read(message_ids: number[]): void {
send_flag_update_for_messages(message_ids, "read", "add");
}
-export function mark_as_unread(message_ids) {
+export function mark_as_unread(message_ids: number[]): void {
send_flag_update_for_messages(message_ids, "read", "remove");
}
-export function save_collapsed(message) {
+export function save_collapsed(message: Message): void {
send_flag_update_for_messages([message.id], "collapsed", "add");
}
-export function save_uncollapsed(message) {
+export function save_uncollapsed(message: Message): void {
send_flag_update_for_messages([message.id], "collapsed", "remove");
}
-export function unstar_all_messages() {
+export function unstar_all_messages(): void {
const starred_msg_ids = starred_messages.get_starred_msg_ids();
send_flag_update_for_messages(starred_msg_ids, "starred", "remove");
}
-export function unstar_all_messages_in_topic(stream_id, topic) {
+// While we're parsing message objects, our code only looks at the
+// IDs. TODO: Use a shared zod schema for parsing messages if/when
+// message_fetch.ts parses message objects using zod.
+const message_response_schema = z.object({
+ messages: z.array(
+ z.object({
+ id: z.number(),
+ }),
+ ),
+});
+
+export function unstar_all_messages_in_topic(stream_id: number, topic: string): void {
const data = {
anchor: "newest",
// In the unlikely event the user has >1000 starred messages
@@ -94,10 +108,11 @@ export function unstar_all_messages_in_topic(stream_id, topic) {
]),
};
- channel.get({
+ void channel.get({
url: "/json/messages",
data,
- success(data) {
+ success(raw_data) {
+ const data = message_response_schema.parse(raw_data);
const messages = data.messages;
const starred_message_ids = messages.map((message) => message.id);
send_flag_update_for_messages(starred_message_ids, "starred", "remove");
diff --git a/web/src/message_store.ts b/web/src/message_store.ts
index 726f96bd1a9..ca67c216cee 100644
--- a/web/src/message_store.ts
+++ b/web/src/message_store.ts
@@ -109,6 +109,8 @@ export type Message = (
// Added in `reactions.set_clean_reactions`.
clean_reactions: Map<string, MessageCleanReaction>;
+ locally_echoed?: boolean;
+
// Added in `message_helper.process_new_message`.
sent_by_me: boolean;
reply_to: string;