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.

compose_validate: Simplify away extra stream_data lookups.

Anders Kaseorgdb1f69e72f7b
diff --git a/web/src/compose_banner.ts b/web/src/compose_banner.ts
index 7ae49ecda1d..aab749230e0 100644
--- a/web/src/compose_banner.ts
+++ b/web/src/compose_banner.ts
@@ -1,5 +1,4 @@
import $ from "jquery";
-import assert from "minimalistic-assert";
import render_compose_banner from "../templates/compose_banner/compose_banner.hbs";
import render_stream_does_not_exist_error from "../templates/compose_banner/stream_does_not_exist_error.hbs";
@@ -7,6 +6,7 @@ import render_stream_does_not_exist_error from "../templates/compose_banner/stre
import {$t} from "./i18n";
import * as scroll_util from "./scroll_util";
import * as stream_data from "./stream_data";
+import type {StreamSubscription} from "./sub_store";
export let scroll_to_message_banner_message_id: number | null = null;
export function set_scroll_to_message_banner_message_id(val: number | null): void {
@@ -183,14 +183,11 @@ export function show_stream_does_not_exist_error(stream_name: string): void {
$("#compose_select_recipient_widget").trigger("click");
}
-export function show_stream_not_subscribed_error(stream_name: string): void {
+export function show_stream_not_subscribed_error(sub: StreamSubscription): void {
const $banner_container = $("#compose_banners");
if ($(`#compose_banners .${CSS.escape(CLASSNAMES.user_not_subscribed)}`).length) {
return;
}
- const sub = stream_data.get_sub(stream_name);
- // We expect this to be a does-not-exist error if it was undefined.
- assert(sub !== undefined);
const new_row_html = render_compose_banner({
banner_type: ERROR,
banner_text: $t({
diff --git a/web/src/compose_validate.ts b/web/src/compose_validate.ts
index 379e054bb02..ebb279da34d 100644
--- a/web/src/compose_validate.ts
+++ b/web/src/compose_validate.ts
@@ -540,11 +540,11 @@ export function validate_stream_message_mentions(opts: StreamWildcardOptions): b
return true;
}
-export function validate_stream_message_address_info(stream_name: string): boolean {
- if (stream_data.is_subscribed_by_name(stream_name)) {
+export function validate_stream_message_address_info(sub: StreamSubscription): boolean {
+ if (sub.subscribed) {
return true;
}
- compose_banner.show_stream_not_subscribed_error(stream_name);
+ compose_banner.show_stream_not_subscribed_error(sub);
return false;
}
@@ -598,7 +598,7 @@ function validate_stream_message(scheduling_message: boolean): boolean {
);
if (
- !validate_stream_message_address_info(sub.name) ||
+ !validate_stream_message_address_info(sub) ||
!validate_stream_message_mentions({
stream_id: sub.stream_id,
$banner_container,
diff --git a/web/tests/compose_validate.test.js b/web/tests/compose_validate.test.js
index c56a71593e1..2e1688a3eb0 100644
--- a/web/tests/compose_validate.test.js
+++ b/web/tests/compose_validate.test.js
@@ -90,7 +90,7 @@ test_ui("validate_stream_message_address_info", ({mock_template}) => {
subscribed: true,
};
stream_data.add_sub(party_sub);
- assert.ok(compose_validate.validate_stream_message_address_info("social"));
+ assert.ok(compose_validate.validate_stream_message_address_info(party_sub));
party_sub.subscribed = false;
stream_data.add_sub(party_sub);
@@ -101,14 +101,14 @@ test_ui("validate_stream_message_address_info", ({mock_template}) => {
user_not_subscribed_rendered = true;
return html;
});
- assert.ok(!compose_validate.validate_stream_message_address_info("social"));
+ assert.ok(!compose_validate.validate_stream_message_address_info(party_sub));
assert.ok(user_not_subscribed_rendered);
party_sub.name = "Frontend";
party_sub.stream_id = 102;
stream_data.add_sub(party_sub);
user_not_subscribed_rendered = false;
- assert.ok(!compose_validate.validate_stream_message_address_info("Frontend"));
+ assert.ok(!compose_validate.validate_stream_message_address_info(party_sub));
assert.ok(user_not_subscribed_rendered);
});