Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
mandatory_topics: Refactor mandatory_topics check into a function.
Vector738fb1eeeb0985
diff --git a/web/src/compose_actions.ts b/web/src/compose_actions.tsindex 4b9fc7a4335..acb4683cf78 100644--- a/web/src/compose_actions.ts+++ b/web/src/compose_actions.ts@@ -27,7 +27,6 @@ import * as reload_state from "./reload_state.ts";import * as resize from "./resize.ts";import * as saved_snippets_ui from "./saved_snippets_ui.ts";import * as spectators from "./spectators.ts";-import {realm} from "./state_data.ts";import * as stream_data from "./stream_data.ts";import * as util from "./util.ts";@@ -519,7 +518,7 @@ export let on_topic_narrow = (): void => {}if (- ((compose_state.topic() || !realm.realm_mandatory_topics) &&+ ((compose_state.topic() || stream_data.can_use_empty_topic(compose_state.stream_id())) &&compose_state.has_message_content()) ||compose_state.is_recipient_edited_manually()) {diff --git a/web/src/compose_fade_helper.ts b/web/src/compose_fade_helper.tsindex 18eddd7b811..5bea9baa04d 100644--- a/web/src/compose_fade_helper.ts+++ b/web/src/compose_fade_helper.ts@@ -1,7 +1,7 @@import $ from "jquery";import type {Message} from "./message_store.ts";-import {realm} from "./state_data.ts";+import * as stream_data from "./stream_data.ts";import * as sub_store from "./sub_store.ts";import type {Recipient} from "./util.ts";import * as util from "./util.ts";@@ -40,7 +40,8 @@ export function want_normal_display(): boolean {// means user is still configuring topic.if (focused_recipient.topic === "" &&- (realm.realm_mandatory_topics || $("input#stream_message_recipient_topic").is(":focus"))+ (!stream_data.can_use_empty_topic(focused_recipient.stream_id) ||+ $("input#stream_message_recipient_topic").is(":focus"))) {return true;}diff --git a/web/src/compose_recipient.ts b/web/src/compose_recipient.tsindex 46e24cc65cf..333e4c60226 100644--- a/web/src/compose_recipient.ts+++ b/web/src/compose_recipient.ts@@ -329,8 +329,8 @@ export function initialize(): void {$("#private_message_recipient").on("input", restore_placeholder_in_firefox_for_no_input);}-export function update_topic_inputbox_on_mandatory_topics_change(): void {- if (realm.realm_mandatory_topics) {+export function update_topic_inputbox_on_topics_policy_change(): void {+ if (!stream_data.can_use_empty_topic(compose_state.stream_id())) {const $input = $("input#stream_message_recipient_topic");$input.attr("placeholder", $t({defaultMessage: "Topic"}));$input.removeClass("empty-topic-display");@@ -346,7 +346,9 @@ export function update_topic_displayed_text(topic_name = "", has_topic_focus = fcompose_state.topic(topic_name);// When topics are mandatory, no additional adjustments are needed.- if (realm.realm_mandatory_topics) {+ // Also, if the recipient in the compose box is not selected, the+ // placeholder will always be "Topic" and never "general chat".+ if (!stream_data.can_use_empty_topic(compose_state.stream_id())) {return;}// Otherwise, we have some adjustments to make to display:diff --git a/web/src/compose_state.ts b/web/src/compose_state.tsindex c7851a8e104..1a058ce3d8b 100644--- a/web/src/compose_state.ts+++ b/web/src/compose_state.ts@@ -2,7 +2,7 @@ import $ from "jquery";import * as compose_pm_pill from "./compose_pm_pill.ts";import * as people from "./people.ts";-import {realm} from "./state_data.ts";+import * as stream_data from "./stream_data.ts";import * as sub_store from "./sub_store.ts";let message_type: "stream" | "private" | undefined;@@ -260,7 +260,7 @@ export function has_savable_message_content(): boolean {export function has_full_recipient(): boolean {if (message_type === "stream") {- const has_topic = topic() !== "" || !realm.realm_mandatory_topics;+ const has_topic = topic() !== "" || stream_data.can_use_empty_topic(stream_id());return stream_id() !== undefined && has_topic;}return private_message_recipient_ids().length > 0;diff --git a/web/src/compose_ui.ts b/web/src/compose_ui.tsindex b0c5f74c5aa..dc38ee351c0 100644--- a/web/src/compose_ui.ts+++ b/web/src/compose_ui.ts@@ -26,7 +26,7 @@ import * as people from "./people.ts";import {postprocess_content} from "./postprocess_content.ts";import * as rendered_markdown from "./rendered_markdown.ts";import * as rtl from "./rtl.ts";-import {current_user, realm} from "./state_data.ts";+import {current_user} from "./state_data.ts";import * as stream_data from "./stream_data.ts";import * as user_status from "./user_status.ts";import * as util from "./util.ts";@@ -207,7 +207,7 @@ function get_focus_area(opts: ComposeTriggeredOptions): string {opts.message_type === "stream" &&opts.stream_id &&!opts.topic &&- realm.realm_mandatory_topics+ !stream_data.can_use_empty_topic(opts.stream_id)) {return "input#stream_message_recipient_topic";} else if (@@ -432,11 +432,13 @@ export function compute_placeholder_text(opts: ComposePlaceholderOptions): strin}}+ // The following block of code will do nothing if the channel is+ // not selected as the placeholder in that case will be "Compose your message here".let topic_display_name: string | undefined;if (opts.topic !== "") {topic_display_name = opts.topic;} else if (- !realm.realm_mandatory_topics &&+ stream_data.can_use_empty_topic(opts.stream_id) &&!$("input#stream_message_recipient_topic").is(":focus")) {topic_display_name = util.get_final_topic_display_name(opts.topic);diff --git a/web/src/compose_validate.ts b/web/src/compose_validate.tsindex bc63b05f4aa..a0a48fc2ee6 100644--- a/web/src/compose_validate.ts+++ b/web/src/compose_validate.ts@@ -837,7 +837,7 @@ function validate_stream_message(scheduling_message: boolean, show_banner = truereturn false;}- if (realm.realm_mandatory_topics) {+ if (!stream_data.can_use_empty_topic(compose_state.stream_id())) {const topic = compose_state.topic();const missing_topic = util.is_topic_name_considered_empty(topic);if (missing_topic) {diff --git a/web/src/drafts.ts b/web/src/drafts.tsindex 4a495675171..a7d4d0b70e4 100644--- a/web/src/drafts.ts+++ b/web/src/drafts.ts@@ -14,7 +14,6 @@ import {localstorage} from "./localstorage.ts";import * as markdown from "./markdown.ts";import * as narrow_state from "./narrow_state.ts";import * as people from "./people.ts";-import {realm} from "./state_data.ts";import * as stream_color from "./stream_color.ts";import * as stream_data from "./stream_data.ts";import * as sub_store from "./sub_store.ts";@@ -642,7 +641,12 @@ export function format_draft(draft: LocalStorageDraftWithId): FormattedDraft | ulet draft_topic_display_name = draft.topic;let is_empty_string_topic = false;- if (draft.topic === "" && !realm.realm_mandatory_topics) {++ // If the channel is not known (recipient was not specified while the creation of the+ // draft) and the topic is empty, the draft_topic_display_name will always be empty string+ // and the draft title will appear as "# >". We don't use the term "general chat" until+ // the channel is known.+ if (sub && draft.topic === "" && stream_data.can_use_empty_topic(draft.stream_id)) {draft_topic_display_name = util.get_final_topic_display_name("");is_empty_string_topic = true;}diff --git a/web/src/drafts_overlay_ui.ts b/web/src/drafts_overlay_ui.tsindex ec7f4b6704a..2522ebeafcc 100644--- a/web/src/drafts_overlay_ui.ts+++ b/web/src/drafts_overlay_ui.ts@@ -18,7 +18,7 @@ import * as messages_overlay_ui from "./messages_overlay_ui.ts";import * as overlays from "./overlays.ts";import * as people from "./people.ts";import * as rendered_markdown from "./rendered_markdown.ts";-import {realm} from "./state_data.ts";+import * as stream_data from "./stream_data.ts";import * as user_card_popover from "./user_card_popover.ts";import * as user_group_popover from "./user_group_popover.ts";@@ -82,7 +82,7 @@ function restore_draft(draft_id: string): void {if (compose_args.type === "stream") {if (compose_args.stream_id !== undefined &&- (compose_args.topic !== "" || !realm.realm_mandatory_topics)+ (compose_args.topic !== "" || stream_data.can_use_empty_topic(compose_args.stream_id))) {message_view.show([diff --git a/web/src/message_edit.ts b/web/src/message_edit.tsindex 4b2c4ae52e0..79f121426ec 100644--- a/web/src/message_edit.ts+++ b/web/src/message_edit.ts@@ -490,8 +490,8 @@ function update_inline_topic_edit_input_max_width(}}-function handle_inline_topic_edit_change(this: HTMLInputElement): void {- const $inline_topic_edit_input = $(this);+function handle_inline_topic_edit_change(elem: HTMLInputElement, stream_id: number): void {+ const $inline_topic_edit_input = $(elem);update_inline_topic_edit_input_max_width($inline_topic_edit_input);@@ -505,7 +505,10 @@ function handle_inline_topic_edit_change(this: HTMLInputElement): void {const $topic_edit_save_button = $inline_topic_edit_input.closest(".topic_edit_form").find(".topic_edit_save");- if (realm.realm_mandatory_topics && util.is_topic_name_considered_empty(this.value)) {+ if (+ !stream_data.can_use_empty_topic(stream_id) &&+ util.is_topic_name_considered_empty(elem.value)+ ) {// When the topic is mandatory in a realm and the new topic is considered empty,// we disable the save button and show a tooltip with an error message.ui_util.disable_element_and_add_tooltip(@@ -521,7 +524,7 @@ function handle_inline_topic_edit_change(this: HTMLInputElement): void {ui_util.enable_element_and_remove_tooltip($topic_edit_save_button);}- if (!realm.realm_mandatory_topics) {+ if (stream_data.can_use_empty_topic(stream_id)) {const $topic_not_mandatory_placeholder = $(".inline-topic-edit-placeholder");$topic_not_mandatory_placeholder.toggleClass("inline-topic-edit-placeholder-visible",@@ -983,18 +986,18 @@ function do_toggle_resolve_topic(export function start_inline_topic_edit($recipient_row: JQuery): void {assert(message_lists.current !== undefined);+ const msg_id = rows.id_for_recipient_row($recipient_row);+ const message = message_lists.current.get(msg_id);+ assert(message?.type === "stream");const $form = $(render_topic_edit_form({max_topic_length: realm.max_topic_length,- is_mandatory_topics: realm.realm_mandatory_topics,+ is_mandatory_topics: !stream_data.can_use_empty_topic(message.stream_id),empty_string_topic_display_name: util.get_final_topic_display_name(""),}),);message_lists.current.show_edit_topic_on_recipient_row($recipient_row, $form);$(".topic_edit_spinner").hide();- const msg_id = rows.id_for_recipient_row($recipient_row);- const message = message_lists.current.get(msg_id);- assert(message?.type === "stream");const topic = message.topic;const $inline_topic_edit_input = $form.find<HTMLInputElement>("input.inline_topic_edit");$inline_topic_edit_input.val(topic).trigger("select").trigger("focus");@@ -1010,9 +1013,11 @@ export function start_inline_topic_edit($recipient_row: JQuery): void {handle_inline_topic_edit_keydown($form, typeahead, event);});- $inline_topic_edit_input.on("input", handle_inline_topic_edit_change);+ $inline_topic_edit_input.on("input", function (this: HTMLInputElement) {+ handle_inline_topic_edit_change(this, message.stream_id);+ });- if (!realm.realm_mandatory_topics) {+ if (stream_data.can_use_empty_topic(message.stream_id)) {const $topic_not_mandatory_placeholder = $(".inline-topic-edit-placeholder");if (topic === "") {@@ -1108,7 +1113,10 @@ export function try_save_inline_topic_edit($row: JQuery): void {return;}- if (realm.realm_mandatory_topics && util.is_topic_name_considered_empty(new_topic)) {+ if (+ !stream_data.can_use_empty_topic(message.stream_id) &&+ util.is_topic_name_considered_empty(new_topic)+ ) {// When the topic is mandatory in a realm and the new topic is considered// empty, we don't allow the user to save the topic. Instead, we show the// error visually via the invalid-input class and focus on the input field.diff --git a/web/src/message_view.ts b/web/src/message_view.tsindex 0f51504ddf2..09e7c4c4b16 100644--- a/web/src/message_view.ts+++ b/web/src/message_view.ts@@ -1461,7 +1461,7 @@ export function to_compose_target(): void {// grey-out the message view instead of narrowing to an empty view.const terms = [{operator: "channel", operand: stream_id.toString()}];const topic = compose_state.topic();- if (topic !== "" || !realm.realm_mandatory_topics) {+ if (topic !== "" || stream_data.can_use_empty_topic(stream_id)) {terms.push({operator: "topic", operand: topic});}show(terms, opts);diff --git a/web/src/stream_data.ts b/web/src/stream_data.tsindex c7e16d27371..61496f53edd 100644--- a/web/src/stream_data.ts+++ b/web/src/stream_data.ts@@ -992,6 +992,20 @@ export function get_streams_for_admin(): StreamSubscription[] {return subs;}+// Since whether or not you can use general chat depends on the+// channel, if we don't know what channel is involved, we do not+// consider general chat permitted. This generally comes up in+// situations like drafts without a specified recipient or compose box+// placeholders when looking at a view that does not indicate a specific+// channel.+export function can_use_empty_topic(stream_id: number | undefined): boolean {+ if (stream_id === undefined) {+ return false;+ }++ return !realm.realm_mandatory_topics;+}+/*This module provides a common helper for finding the notificationstream, but we don't own the data. The `realm` structurediff --git a/web/src/stream_popover.ts b/web/src/stream_popover.tsindex d01d218ecf5..af44eccf7cd 100644--- a/web/src/stream_popover.ts+++ b/web/src/stream_popover.ts@@ -487,7 +487,7 @@ export async function build_move_topic_to_stream_popover(function update_submit_button_disabled_state(select_stream_id: number): void {const params = get_params_from_form();- const current_stream_id = params.current_stream_id;+ const current_stream_id = Number.parseInt(params.current_stream_id, 10);const new_topic_name = params.new_topic_name?.trim();const old_topic_name = params.old_topic_name.trim();@@ -500,13 +500,13 @@ export async function build_move_topic_to_stream_popover(// not changed.let is_disabled = false;if (- realm.realm_mandatory_topics &&+ !stream_data.can_use_empty_topic(select_stream_id) &&(new_topic_name === "" || new_topic_name === "(no topic)")) {is_disabled = true;}if (- Number.parseInt(current_stream_id, 10) === select_stream_id &&+ current_stream_id === select_stream_id &&(new_topic_name === undefined || new_topic_name === old_topic_name)) {is_disabled = true;@@ -886,7 +886,7 @@ export async function build_move_topic_to_stream_popover(}function update_topic_input_placeholder_visibility(topic_input_value: string): void {- if (!realm.realm_mandatory_topics) {+ if (stream_data.can_use_empty_topic(current_stream_id)) {const $topic_not_mandatory_placeholder = $(".move-topic-new-topic-placeholder");$topic_not_mandatory_placeholder.toggleClass("move-topic-new-topic-placeholder-visible",@@ -930,7 +930,7 @@ export async function build_move_topic_to_stream_popover(false,);- if (!realm.realm_mandatory_topics) {+ if (stream_data.can_use_empty_topic(current_stream_id)) {const $topic_not_mandatory_placeholder = $(".move-topic-new-topic-placeholder");if (topic_name === "") {diff --git a/web/src/typing.ts b/web/src/typing.tsindex b35a201d95c..c0ecc22afb8 100644--- a/web/src/typing.ts+++ b/web/src/typing.ts@@ -186,7 +186,7 @@ export function get_recipient(): Recipient | null {return null;}const topic = compose_state.topic();- if (realm.realm_mandatory_topics && topic === "") {+ if (!stream_data.can_use_empty_topic(stream_id) && topic === "") {// compose box with empty topic string.return null;}diff --git a/web/tests/typing_status.test.cjs b/web/tests/typing_status.test.cjsindex e3831b46d31..6b9c3309a20 100644--- a/web/tests/typing_status.test.cjs+++ b/web/tests/typing_status.test.cjs@@ -282,6 +282,7 @@ run_test("basics", ({override, override_rewire}) => {override(compose_state, "get_message_type", () => "stream");override(compose_state, "stream_name", () => "Verona");override(stream_data, "get_stream_id", () => "2");+ override(stream_data, "can_use_empty_topic", () => false);override(compose_state, "topic", () => "");assert.equal(typing.get_recipient(), null);