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.

hash_util: Canonicalize operator before processing.

Aman Agrawal4903a7e5344e
diff --git a/web/src/hash_util.ts b/web/src/hash_util.ts
index 66ae8462456..ea550e9d4ca 100644
--- a/web/src/hash_util.ts
+++ b/web/src/hash_util.ts
@@ -3,13 +3,19 @@ import * as z from "zod/mini";
import * as blueslip from "./blueslip.ts";
import * as channel_folders from "./channel_folders.ts";
+import {Filter} from "./filter.ts";
import * as internal_url from "./internal_url.ts";
import type {Message} from "./message_store.ts";
import * as people from "./people.ts";
import {web_channel_default_view_values} from "./settings_config.ts";
import * as settings_data from "./settings_data.ts";
-import {current_user, narrow_term_schema, realm} from "./state_data.ts";
-import type {NarrowTerm} from "./state_data.ts";
+import {
+ current_user,
+ narrow_canonical_term_schema,
+ narrow_operator_schema,
+ realm,
+} from "./state_data.ts";
+import type {NarrowCanonicalTerm, NarrowTerm} from "./state_data.ts";
import * as stream_data from "./stream_data.ts";
import * as stream_topic_history from "./stream_topic_history.ts";
import * as sub_store from "./sub_store.ts";
@@ -57,14 +63,11 @@ export function encode_stream_id(stream_id: number): string {
return internal_url.encodeHashComponent(slug);
}
-export function decode_operand(operator: string, operand: string): string {
- if (
- operator === "group-pm-with" ||
- operator === "dm-including" ||
- operator === "dm" ||
- operator === "sender" ||
- operator === "pm-with"
- ) {
+export function decode_operand(
+ operator: NarrowCanonicalTerm["operator"],
+ operand: NarrowCanonicalTerm["operand"],
+): string {
+ if (operator === "dm-including" || operator === "dm" || operator === "sender") {
const emails = people.slug_to_emails(operand);
if (emails) {
return emails;
@@ -73,7 +76,7 @@ export function decode_operand(operator: string, operand: string): string {
operand = internal_url.decodeHashComponent(operand);
- if (util.canonicalize_channel_synonyms(operator) === "channel") {
+ if (operator === "channel") {
return stream_data.slug_to_stream_id(operand)?.toString() ?? "";
}
@@ -201,7 +204,7 @@ export function search_public_streams_notice_url(terms: NarrowTerm[]): string {
return search_terms_to_hash([public_operator, ...terms]);
}
-export function parse_narrow(hash: string[]): NarrowTerm[] | undefined {
+export function parse_narrow(hash: string[]): NarrowCanonicalTerm[] | undefined {
// There's a Python copy of this function in `zerver/lib/url_decoding.py`
// called `parse_narrow_url`, the two should be kept roughly in sync.
@@ -236,14 +239,17 @@ export function parse_narrow(hash: string[]): NarrowTerm[] | undefined {
return undefined;
}
- const operand = decode_operand(operator, raw_operand);
+ const canonical_operator = Filter.canonicalize_operator(
+ narrow_operator_schema.parse(operator),
+ );
+ const operand = decode_operand(canonical_operator, raw_operand);
terms.push({
negated,
- operator,
+ operator: canonical_operator,
operand,
});
}
- return z.array(narrow_term_schema).parse(terms);
+ return z.array(narrow_canonical_term_schema).parse(terms);
}
export function channels_settings_edit_url(
@@ -422,7 +428,7 @@ export function decode_stream_topic_from_url(
}
// This check is important as a malformed url
// may have `stream` / `channel`, `topic` or `near:` in a wrong order
- if (terms[0]?.operator !== "stream" && terms[0]?.operator !== "channel") {
+ if (terms[0]?.operator !== "channel") {
return null;
}
const stream_id = Number.parseInt(terms[0].operand, 10);
diff --git a/web/tests/hash_util.test.cjs b/web/tests/hash_util.test.cjs
index 2bdb05ce212..8b4802b77db 100644
--- a/web/tests/hash_util.test.cjs
+++ b/web/tests/hash_util.test.cjs
@@ -44,7 +44,7 @@ run_test("hash_util", () => {
encode_decode_operand(operator, operand, "15-Hamlet");
- operator = "stream";
+ operator = "channel";
operand = frontend_id.toString();
encode_decode_operand(operator, operand, "99-frontend");
@@ -176,11 +176,11 @@ run_test("test_is_in_specified_hash_category", () => {
run_test("test_parse_narrow", () => {
assert.deepEqual(hash_util.parse_narrow(["narrow", "stream", "99-frontend"]), [
- {negated: false, operator: "stream", operand: frontend_id.toString()},
+ {negated: false, operator: "channel", operand: frontend_id.toString()},
]);
assert.deepEqual(hash_util.parse_narrow(["narrow", "-stream", "99-frontend"]), [
- {negated: true, operator: "stream", operand: frontend_id.toString()},
+ {negated: true, operator: "channel", operand: frontend_id.toString()},
]);
assert.equal(hash_util.parse_narrow(["narrow", "BOGUS"]), undefined);
@@ -188,12 +188,12 @@ run_test("test_parse_narrow", () => {
// For unknown channel IDs, we still parse it; it could be a valid channel we do
// not have access to. We'll end up showing "Invalid stream" in the navbar.
assert.deepEqual(hash_util.parse_narrow(["narrow", "stream", "42-bogus"]), [
- {negated: false, operator: "stream", operand: "42"},
+ {negated: false, operator: "channel", operand: "42"},
]);
// Empty string as a topic name is valid.
assert.deepEqual(hash_util.parse_narrow(["narrow", "stream", "99-frontend", "topic", ""]), [
- {negated: false, operator: "stream", operand: frontend_id.toString()},
+ {negated: false, operator: "channel", operand: frontend_id.toString()},
{negated: false, operator: "topic", operand: ""},
]);
diff --git a/web/tests/hashchange.test.cjs b/web/tests/hashchange.test.cjs
index e116ef7a1e1..1e51aedb620 100644
--- a/web/tests/hashchange.test.cjs
+++ b/web/tests/hashchange.test.cjs
@@ -185,7 +185,7 @@ run_test("people_slugs", () => {
hash = hash_util.search_terms_to_hash(terms);
assert.equal(hash, "#narrow/pm-with/42-Alice-Smith");
narrow = hash_util.parse_narrow(hash.split("/"));
- assert.deepEqual(narrow, [{operator: "pm-with", operand: "[email protected]", negated: false}]);
+ assert.deepEqual(narrow, [{operator: "dm", operand: "[email protected]", negated: false}]);
});
function test_helper({override, override_rewire, change_tab}) {
diff --git a/web/tests/message_view.test.cjs b/web/tests/message_view.test.cjs
index 3af9cf9ddf5..b4b9ef7cb38 100644
--- a/web/tests/message_view.test.cjs
+++ b/web/tests/message_view.test.cjs
@@ -222,17 +222,6 @@ run_test("urls", () => {
emails = hash_util.decode_operand("dm", "5-group");
assert.equal(emails, "[email protected]");
-
- // Even though we renamed "pm-with" to "dm", preexisting
- // links/URLs with "pm-with" operator are decoded correctly.
- emails = hash_util.decode_operand("pm-with", "22,23-group");
- assert.equal(emails, "[email protected],[email protected]");
-
- emails = hash_util.decode_operand("pm-with", "5,22,23-group");
- assert.equal(emails, "[email protected],[email protected]");
-
- emails = hash_util.decode_operand("pm-with", "5-group");
- assert.equal(emails, "[email protected]");
});
run_test("show_empty_narrow_message", ({mock_template, override}) => {