Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
echo: Update channel-topic link to permalink upon new message arrival.
roanster007f3fbf314e2cc
diff --git a/web/src/browser_history.ts b/web/src/browser_history.tsindex 90a991d2c12..e509346c7df 100644--- a/web/src/browser_history.ts+++ b/web/src/browser_history.ts@@ -173,7 +173,15 @@ export function current_scroll_offset(): number | undefined {return current_state?.narrow_offset;}-export function update_current_history_state_data(new_data: StateData): void {+export function update_current_history_state_data(+ new_data: StateData,+ url: string | undefined = undefined,+): void {+ // The optional url parameter is for those rare situations where+ // we want to adjust the URL without adding a new history entry.+ if (url === undefined) {+ url = window.location.href;+ }const current_state = state_data_schema.nullable().parse(window.history.state);const current_state_data = {narrow_pointer: current_state?.narrow_pointer,@@ -181,7 +189,7 @@ export function update_current_history_state_data(new_data: StateData): void {show_more_topics: current_state?.show_more_topics,};const state_data = {...current_state_data, ...new_data};- window.history.replaceState(state_data, "", window.location.href);+ window.history.replaceState(state_data, "", url);}export function get_current_state_show_more_topics(): boolean | undefined {diff --git a/web/src/echo.ts b/web/src/echo.tsindex f2c3f037fd9..de38f7c3eaa 100644--- a/web/src/echo.ts+++ b/web/src/echo.ts@@ -7,9 +7,11 @@ import render_message_controls_failed_msg from "../templates/message_controls_faimport * as alert_words from "./alert_words.ts";import * as blueslip from "./blueslip.ts";+import * as browser_history from "./browser_history.ts";import * as compose_notifications from "./compose_notifications.ts";import * as compose_ui from "./compose_ui.ts";import * as echo_state from "./echo_state.ts";+import * as hash_util from "./hash_util.ts";import * as local_message from "./local_message.ts";import * as markdown from "./markdown.ts";import * as message_events_util from "./message_events_util.ts";@@ -436,6 +438,32 @@ export function edit_locally(message: Message, request: LocalEditRequest): Messareturn message;}+export function update_topic_hash_to_contain_with_term(message: Message): void {+ // If the current filter consists only of channel and topic terms+ // and the incoming message belongs to same narrow, we try to+ // add the `with` term to the narrow, and update the hash, to+ // convert to permalink.+ if (message.type !== "stream") {+ return;+ }+ const filter = message_lists.current?.data.filter;++ if (!filter?.has_exactly_channel_topic_operators()) {+ return;+ }++ filter.adjust_with_operand_to_message(message.id);++ // Adjust the URL to include the /with/ operator. We update the+ // existing history entry, so that you don't have to hit Back+ // twice to get to where you were before visiting the new topic.+ //+ // Since we're not changing the view, we want to preserve whatever+ // scroll position StateData was already present.+ const new_hash = hash_util.search_terms_to_hash(filter.terms());+ browser_history.update_current_history_state_data({}, new_hash);+}+export let reify_message_id = (local_id: string, server_id: number): void => {const message = echo_state.get_message_waiting_for_id(local_id);echo_state.remove_message_from_waiting_for_id(local_id);@@ -467,6 +495,7 @@ export let reify_message_id = (local_id: string, server_id: number): void => {message_id: message.id,});}+ update_topic_hash_to_contain_with_term(message);};export function rewire_reify_message_id(value: typeof reify_message_id): void {diff --git a/web/src/filter.ts b/web/src/filter.tsindex 254827bb20c..409c546825d 100644--- a/web/src/filter.ts+++ b/web/src/filter.ts@@ -1046,6 +1046,17 @@ export class Filter {return this._terms.length === 1 && this.has_operand("in", "home");}+ has_exactly_channel_topic_operators(): boolean {+ if (+ this.terms().length === 2 &&+ this.has_operator("channel") &&+ this.has_operator("topic")+ ) {+ return true;+ }+ return false;+ }+is_keyword_search(): boolean {return this.has_operator("search");}@@ -1545,6 +1556,14 @@ export class Filter {return terms_mixed_case.map((term: NarrowTerm) => Filter.canonicalize_term(term));}+ adjust_with_operand_to_message(msg_id: number): void {+ const narrow_terms = this._terms.filter((term) => term.operator !== "with");+ const adjusted_with_term = {operator: "with", operand: `${msg_id}`};+ const adjusted_terms = [...narrow_terms, adjusted_with_term];+ this._terms = adjusted_terms;+ this.requires_adjustment_for_moved_with_target = false;+ }+filter_with_new_params(params: NarrowTerm): Filter {const new_params = this.fix_terms([params])[0];assert(new_params !== undefined);diff --git a/web/tests/echo.test.cjs b/web/tests/echo.test.cjsindex 95d4b055f8e..261db3cc61f 100644--- a/web/tests/echo.test.cjs+++ b/web/tests/echo.test.cjs@@ -8,7 +8,9 @@ const {mock_esm, zrequire} = require("./lib/namespace.cjs");const {make_stub} = require("./lib/stub.cjs");const {run_test, noop} = require("./lib/test.cjs");+const browser_history = mock_esm("../src/browser_history");const compose_notifications = mock_esm("../src/compose_notifications");+const hash_util = mock_esm("../src/hash_util");const markdown = mock_esm("../src/markdown");const message_lists = mock_esm("../src/message_lists");const message_events_util = mock_esm("../src/message_events_util");@@ -44,6 +46,11 @@ message_lists.current = {can_apply_locally() {return true;},+ has_exactly_channel_topic_operators() {+ return true;+ },+ adjust_with_operand_to_message: noop,+ terms: noop,},},change_message_id: noop,@@ -370,6 +377,8 @@ run_test("test reify_message_id", ({override}) => {override(markdown, "render", noop);override(markdown, "get_topic_links", noop);+ override(hash_util, "search_terms_to_hash", noop);+ override(browser_history, "update_current_history_state_data", noop);const message_request = {type: "stream",diff --git a/web/tests/filter.test.cjs b/web/tests/filter.test.cjsindex 233be8a9b6d..e7ae53d55de 100644--- a/web/tests/filter.test.cjs+++ b/web/tests/filter.test.cjs@@ -487,6 +487,19 @@ test("basics", () => {assert.ok(!filter.is_personal_filter());assert.ok(filter.is_conversation_view());assert.ok(!filter.is_conversation_view_with_near());++ terms = [+ {operator: "channel", operand: "foo", negated: false},+ {operator: "topic", operand: "bar", negated: false},+ ];+ filter = new Filter(terms);++ assert.equal(filter.has_exactly_channel_topic_operators(), true);++ filter.adjust_with_operand_to_message(12);++ assert.deepEqual(filter.terms(), [...terms, {operator: "with", operand: "12"}]);+ assert.equal(filter.has_exactly_channel_topic_operators(), false);});function assert_not_mark_read_with_has_operands(additional_terms_to_test) {