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.

resolved_topic: Implement resolve, unresolve, and display.

Greg Price760cfcc603d3
diff --git a/frontend_tests/node_tests/compose_validate.js b/frontend_tests/node_tests/compose_validate.js
index d4cbe5991a3..69defae816d 100644
--- a/frontend_tests/node_tests/compose_validate.js
+++ b/frontend_tests/node_tests/compose_validate.js
@@ -786,7 +786,7 @@ test_ui("test warn_if_topic_resolved", ({override, mock_template}) => {
compose_state.set_message_type("stream");
compose_state.stream_name("Do not exist");
- compose_state.topic(resolved_topic.RESOLVED_TOPIC_PREFIX + "hello");
+ compose_state.topic(resolved_topic.resolve_name("hello"));
// Do not show a warning if stream name does not exist
compose_validate.warn_if_topic_resolved();
diff --git a/frontend_tests/node_tests/filter.js b/frontend_tests/node_tests/filter.js
index 9e4e908e828..fe819e50294 100644
--- a/frontend_tests/node_tests/filter.js
+++ b/frontend_tests/node_tests/filter.js
@@ -685,7 +685,7 @@ test("predicate_basics", () => {
assert.ok(predicate({}));
predicate = get_predicate([["is", "resolved"]]);
- const resolved_topic_name = resolved_topic.RESOLVED_TOPIC_PREFIX + "foo";
+ const resolved_topic_name = resolved_topic.resolve_name("foo");
assert.ok(predicate({type: "stream", topic: resolved_topic_name}));
assert.ok(!predicate({topic: resolved_topic_name}));
assert.ok(!predicate({type: "stream", topic: "foo"}));
diff --git a/frontend_tests/node_tests/narrow_local.js b/frontend_tests/node_tests/narrow_local.js
index d907d392e15..2a4ba532029 100644
--- a/frontend_tests/node_tests/narrow_local.js
+++ b/frontend_tests/node_tests/narrow_local.js
@@ -302,7 +302,7 @@ run_test("is:alerted with no unreads and one match", () => {
});
run_test("is:resolved with one unread", () => {
- const resolved_topic_name = resolved_topic.RESOLVED_TOPIC_PREFIX + "foo";
+ const resolved_topic_name = resolved_topic.resolve_name("foo");
const fixture = {
filter_terms: [{operator: "is", operand: "resolved"}],
unread_info: {
@@ -327,7 +327,7 @@ run_test("is:resolved with one unread", () => {
});
run_test("is:resolved with no unreads", () => {
- const resolved_topic_name = resolved_topic.RESOLVED_TOPIC_PREFIX + "foo";
+ const resolved_topic_name = resolved_topic.resolve_name("foo");
const fixture = {
filter_terms: [{operator: "is", operand: "resolved"}],
unread_info: {
diff --git a/frontend_tests/node_tests/resolved_topic.js b/frontend_tests/node_tests/resolved_topic.js
index 3136fb84cfd..9627940b12b 100644
--- a/frontend_tests/node_tests/resolved_topic.js
+++ b/frontend_tests/node_tests/resolved_topic.js
@@ -11,6 +11,7 @@ const topic_name = "asdf";
const resolved_name = "✔ " + topic_name;
const overresolved_name = "✔ ✔✔ " + topic_name;
const pseudoresolved_name = "✔" + topic_name; // check mark, but no space
+const names = [topic_name, resolved_name, overresolved_name, pseudoresolved_name];
run_test("is_resolved", () => {
assert.ok(!resolved_topic.is_resolved(topic_name));
@@ -18,3 +19,38 @@ run_test("is_resolved", () => {
assert.ok(resolved_topic.is_resolved(overresolved_name));
assert.ok(!resolved_topic.is_resolved(pseudoresolved_name));
});
+
+run_test("resolve_name", () => {
+ assert.equal(resolved_topic.resolve_name(topic_name), resolved_name);
+
+ for (const name of names) {
+ assert.notEqual(resolved_topic.resolve_name(name), name);
+ }
+});
+
+run_test("unresolve_name", () => {
+ assert.equal(resolved_topic.unresolve_name(topic_name), topic_name);
+ assert.equal(resolved_topic.unresolve_name(resolved_name), topic_name);
+ assert.equal(resolved_topic.unresolve_name(overresolved_name), topic_name);
+ assert.equal(resolved_topic.unresolve_name(pseudoresolved_name), pseudoresolved_name);
+});
+
+run_test("display_parts", () => {
+ const results = [];
+ for (const name of names) {
+ const [prefix, display_name] = resolved_topic.display_parts(name);
+
+ // The parts always partition the input name.
+ assert.equal(prefix + display_name, name);
+
+ // The prefix is always the canonical prefix, or empty…
+ assert.ok(prefix === "" || prefix === resolved_topic.RESOLVED_TOPIC_PREFIX);
+ // … and which one is determined by is_resolved.
+ assert.equal(Boolean(prefix), resolved_topic.is_resolved(name));
+
+ // The parts, together, differ from those of any other input.
+ // (Yes, this is quadratic. Keep the list of test data nice and short.)
+ assert.ok(!results.some(([p, d]) => p === prefix && d === display_name));
+ results.push([prefix, display_name]);
+ }
+});
diff --git a/static/js/message_edit.js b/static/js/message_edit.js
index ba4adba3ca4..1842967a022 100644
--- a/static/js/message_edit.js
+++ b/static/js/message_edit.js
@@ -1,6 +1,5 @@
import ClipboardJS from "clipboard";
import $ from "jquery";
-import _ from "lodash";
import * as resolved_topic from "../shared/js/resolved_topic";
import render_delete_message_modal from "../templates/confirm_dialog/confirm_delete_message.hbs";
@@ -641,10 +640,10 @@ export function start(row, edit_box_open_callback) {
export function toggle_resolve_topic(message_id, old_topic_name) {
let new_topic_name;
- if (old_topic_name.startsWith(resolved_topic.RESOLVED_TOPIC_PREFIX)) {
- new_topic_name = _.trimStart(old_topic_name, resolved_topic.RESOLVED_TOPIC_PREFIX);
+ if (resolved_topic.is_resolved(old_topic_name)) {
+ new_topic_name = resolved_topic.unresolve_name(old_topic_name);
} else {
- new_topic_name = resolved_topic.RESOLVED_TOPIC_PREFIX + old_topic_name;
+ new_topic_name = resolved_topic.resolve_name(old_topic_name);
}
const request = {
diff --git a/static/js/topic_list_data.js b/static/js/topic_list_data.js
index 3ea67fe1d10..26c99d6c0c2 100644
--- a/static/js/topic_list_data.js
+++ b/static/js/topic_list_data.js
@@ -33,16 +33,8 @@ export function get_list_info(stream_id, zoomed) {
const num_unread = unread.num_unread_for_topic(stream_id, topic_name);
const is_active_topic = active_topic === topic_name.toLowerCase();
const is_topic_muted = muted_topics.is_topic_muted(stream_id, topic_name);
-
- let topic_resolved_prefix = "";
- let topic_display_name = topic_name;
- const resolved = topic_name.startsWith(resolved_topic.RESOLVED_TOPIC_PREFIX);
- if (resolved) {
- topic_resolved_prefix = resolved_topic.RESOLVED_TOPIC_PREFIX;
- topic_display_name = topic_display_name.slice(
- resolved_topic.RESOLVED_TOPIC_PREFIX.length,
- );
- }
+ const [topic_resolved_prefix, topic_display_name] =
+ resolved_topic.display_parts(topic_name);
if (!zoomed) {
function should_show_topic(topics_selected) {
diff --git a/static/shared/js/resolved_topic.js b/static/shared/js/resolved_topic.js
index 404beca9735..a71286c08dd 100644
--- a/static/shared/js/resolved_topic.js
+++ b/static/shared/js/resolved_topic.js
@@ -1,6 +1,45 @@
/** The canonical form of the resolved-topic prefix. */
export const RESOLVED_TOPIC_PREFIX = "✔ ";
+/**
+ * Pattern for an arbitrary resolved-topic prefix.
+ *
+ * These always begin with the canonical prefix, but can go on longer.
+ */
+// The class has the same characters as RESOLVED_TOPIC_PREFIX.
+// It's designed to remove a weird "✔ ✔✔ " prefix, if present.
+// Compare maybe_send_resolve_topic_notifications in zerver/lib/actions.py.
+const RESOLVED_TOPIC_PREFIX_RE = /^✔ [ ✔]*/;
+
export function is_resolved(topic_name) {
return topic_name.startsWith(RESOLVED_TOPIC_PREFIX);
}
+
+export function resolve_name(topic_name) {
+ return RESOLVED_TOPIC_PREFIX + topic_name;
+}
+
+/**
+ * The un-resolved form of this topic name.
+ *
+ * If the topic is already not a resolved topic, this is the identity.
+ */
+export function unresolve_name(topic_name) {
+ return topic_name.replace(RESOLVED_TOPIC_PREFIX_RE, "");
+}
+
+/**
+ * Split the topic name for display, into a "resolved" prefix and remainder.
+ *
+ * The prefix is always the canonical resolved-topic prefix, or empty.
+ *
+ * This function is injective: different topics never produce the same
+ * result, even when `unresolve_name` would give the same result. That's a
+ * property we want when listing topics in the UI, so that we don't end up
+ * showing what look like several identical topics.
+ */
+export function display_parts(topic_name) {
+ return is_resolved(topic_name)
+ ? [RESOLVED_TOPIC_PREFIX, topic_name.slice(RESOLVED_TOPIC_PREFIX.length)]
+ : ["", topic_name];
+}
diff --git a/static/shared/js/resolved_topic.js.flow b/static/shared/js/resolved_topic.js.flow
index a3f754d82ef..4c6a83c409d 100644
--- a/static/shared/js/resolved_topic.js.flow
+++ b/static/shared/js/resolved_topic.js.flow
@@ -3,3 +3,9 @@
declare export var RESOLVED_TOPIC_PREFIX: string;
declare export function is_resolved(topic_name: string): boolean;
+
+declare export function resolve_name(topic_name: string): string;
+
+declare export function unresolve_name(topic_name: string): string;
+
+declare export function display_parts(topic_name: string): [string, string];