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.

transmit: Convert module to typescript.

Evy Kassirer1b9da1e3c76f
diff --git a/web/src/compose.js b/web/src/compose.js
index 841fdb1a6df..d6ee3e7e848 100644
--- a/web/src/compose.js
+++ b/web/src/compose.js
@@ -20,7 +20,7 @@ import * as scheduled_messages from "./scheduled_messages.ts";
import * as sent_messages from "./sent_messages.ts";
import * as server_events_state from "./server_events_state.ts";
import {current_user} from "./state_data.ts";
-import * as transmit from "./transmit.js";
+import * as transmit from "./transmit.ts";
import {user_settings} from "./user_settings.ts";
import * as util from "./util.ts";
import * as zcommand from "./zcommand.ts";
diff --git a/web/src/echo.ts b/web/src/echo.ts
index cccc76028db..ccba3c51039 100644
--- a/web/src/echo.ts
+++ b/web/src/echo.ts
@@ -80,7 +80,7 @@ type LocalEditRequest = Partial<{
mentioned_me_directly: boolean;
}>;
-type LocalMessage = MessageRequestObject & {
+export type LocalMessage = MessageRequestObject & {
raw_content: string;
flags: string[];
is_me_message: boolean;
diff --git a/web/src/transmit.js b/web/src/transmit.ts
similarity index 74%
rename from web/src/transmit.js
rename to web/src/transmit.ts
index 2cb5919e0ed..5bfa9b2824f 100644
--- a/web/src/transmit.js
+++ b/web/src/transmit.ts
@@ -1,5 +1,9 @@
+import assert from "minimalistic-assert";
+import * as z from "zod/mini";
+
import * as blueslip from "./blueslip.ts";
import * as channel from "./channel.ts";
+import type {Message} from "./message_store.ts";
import * as people from "./people.ts";
import * as reload from "./reload.ts";
import * as reload_state from "./reload_state.ts";
@@ -8,11 +12,33 @@ import * as server_events_state from "./server_events_state.ts";
import {current_user} from "./state_data.ts";
import * as stream_data from "./stream_data.ts";
-export function send_message(request, on_success, error) {
+type SendMessageData = {
+ local_id: string;
+ sender_id: number;
+ queue_id: string | null;
+ to: string;
+ content: string;
+ resend?: boolean;
+ locally_echoed?: boolean;
+} & (
+ | {
+ type: "stream";
+ topic: string;
+ }
+ | {
+ type: "private";
+ }
+);
+
+export function send_message(
+ request: SendMessageData,
+ on_success: (raw_data: unknown) => void,
+ error: (response: string, code: string) => void,
+): void {
if (!request.resend) {
sent_messages.start_tracking_message({
local_id: request.local_id,
- locally_echoed: request.locally_echoed,
+ locally_echoed: request.locally_echoed ?? false,
});
}
sent_messages.wrap_send(request.local_id, () => {
@@ -65,13 +91,14 @@ export function send_message(request, on_success, error) {
}
const response = channel.xhr_error_message("Error sending message", xhr);
- error(response, xhr.responseJSON?.code);
+ const parsed = z.object({code: z.string()}).safeParse(xhr.responseJSON);
+ error(response, parsed.success ? parsed.data.code : "");
},
});
});
}
-export function reply_message(opts) {
+export function reply_message(opts: {message: Message; content: string}): void {
// This code does an application-triggered reply to a message (as
// opposed to the user themselves doing it). Its only use case
// for now is experimental widget-aware bots, so treat this as
@@ -82,14 +109,14 @@ export function reply_message(opts) {
const message = opts.message;
let content = opts.content;
- function success() {
+ function success(): void {
// TODO: If server response comes back before the message event,
// we could show it earlier, although that creates some
// complexity. For now do nothing. (Note that send_message
// already handles things like reporting times to the server.)
}
- function error(_response, _server_error_code) {
+ function error(_response: string, _server_error_code: string): void {
// TODO: In our current use case, which is widgets, to meaningfully
// handle errors, we would want the widget to provide some
// kind of callback to us so it can do some appropriate UI.
@@ -99,11 +126,8 @@ export function reply_message(opts) {
const locally_echoed = false;
const local_id = sent_messages.get_new_local_id();
- const reply = {
- sender_id: current_user.user_id,
- queue_id: server_events_state.queue_id,
- local_id,
- };
+ const sender_id = current_user.user_id;
+ const queue_id = server_events_state.queue_id;
sent_messages.start_tracking_message({
local_id,
@@ -117,10 +141,15 @@ export function reply_message(opts) {
content = mention + " " + content;
- reply.type = "stream";
- reply.to = stream_name;
- reply.content = content;
- reply.topic = message.topic;
+ const reply: SendMessageData = {
+ type: "stream",
+ local_id,
+ sender_id,
+ queue_id,
+ to: stream_name,
+ content,
+ topic: message.topic,
+ };
send_message(reply, success, error);
return;
@@ -128,10 +157,16 @@ export function reply_message(opts) {
if (message.type === "private") {
const pm_recipient = people.pm_reply_to(message);
-
- reply.type = "private";
- reply.to = JSON.stringify(pm_recipient.split(","));
- reply.content = content;
+ assert(pm_recipient !== undefined);
+
+ const reply: SendMessageData = {
+ type: "private",
+ local_id,
+ sender_id,
+ queue_id,
+ to: JSON.stringify(pm_recipient.split(",")),
+ content,
+ };
send_message(reply, success, error);
return;
diff --git a/web/src/ui_init.js b/web/src/ui_init.js
index a38d77e78ca..2ba087c24d5 100644
--- a/web/src/ui_init.js
+++ b/web/src/ui_init.js
@@ -155,7 +155,7 @@ import * as timerender from "./timerender.ts";
import * as tippyjs from "./tippyjs.ts";
import * as topic_list from "./topic_list.ts";
import * as topic_popover from "./topic_popover.ts";
-import * as transmit from "./transmit.js";
+import * as transmit from "./transmit.ts";
import * as typeahead_helper from "./typeahead_helper.ts";
import * as typing from "./typing.ts";
import * as unread from "./unread.ts";
diff --git a/web/src/zform.js b/web/src/zform.js
index 5d087e55b6d..1194fbd3a11 100644
--- a/web/src/zform.js
+++ b/web/src/zform.js
@@ -4,7 +4,7 @@ import render_widgets_zform_choices from "../templates/widgets/zform_choices.hbs
import * as blueslip from "./blueslip.ts";
import {zform_widget_extra_data_schema} from "./submessage.ts";
-import * as transmit from "./transmit.js";
+import * as transmit from "./transmit.ts";
export function activate(opts) {
const self = {};