compose_validate: Simplify away extra stream_data lookups.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2024-03-27 16:59:31 -07:00
committed by Tim Abbott
parent 48e836f01c
commit db1f69e72f
3 changed files with 9 additions and 12 deletions

View File

@@ -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({

View File

@@ -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,

View File

@@ -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);
});