resolved_topic: Add and use predicate is_resolved.

We leave in place a couple of sites where the `startsWith` is
entangled with other string manipulation.  We'll handle those next.
This commit is contained in:
Greg Price 2022-03-07 13:59:56 -08:00 committed by Tim Abbott
parent 624cdb0a14
commit 7bf0fd3fa3
8 changed files with 31 additions and 8 deletions

View File

@ -770,7 +770,7 @@ test_ui("test warn_if_topic_resolved", ({override, mock_template}) => {
mock_template("compose_resolved_topic.hbs", false, (context) => {
assert.ok(context.can_move_topic);
assert.ok(context.topic_name.startsWith(resolved_topic.RESOLVED_TOPIC_PREFIX));
assert.ok(resolved_topic.is_resolved(context.topic_name));
return "fake-compose_resolved_topic";
});

View File

@ -0,0 +1,20 @@
"use strict";
const {strict: assert} = require("assert");
const {zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const resolved_topic = zrequire("../shared/js/resolved_topic");
const topic_name = "asdf";
const resolved_name = "✔ " + topic_name;
const overresolved_name = "✔ ✔✔ " + topic_name;
const pseudoresolved_name = "✔" + topic_name; // check mark, but no space
run_test("is_resolved", () => {
assert.ok(!resolved_topic.is_resolved(topic_name));
assert.ok(resolved_topic.is_resolved(resolved_name));
assert.ok(resolved_topic.is_resolved(overresolved_name));
assert.ok(!resolved_topic.is_resolved(pseudoresolved_name));
});

View File

@ -178,7 +178,7 @@ export function warn_if_topic_resolved() {
const sub = stream_data.get_sub(stream_name);
if (sub && topic_name.startsWith(resolved_topic.RESOLVED_TOPIC_PREFIX)) {
if (sub && resolved_topic.is_resolved(topic_name)) {
const error_area = $("#compose_resolved_topic");
if (error_area.html()) {

View File

@ -96,10 +96,7 @@ function message_matches_search_term(message, operator, operand) {
case "unread":
return unread.message_unread(message);
case "resolved":
return (
message.type === "stream" &&
message.topic.startsWith(resolved_topic.RESOLVED_TOPIC_PREFIX)
);
return message.type === "stream" && resolved_topic.is_resolved(message.topic);
default:
return false; // is:whatever returns false
}

View File

@ -182,7 +182,7 @@ function populate_group_from_message_container(group, message_container) {
} else {
group.stream_id = sub.stream_id;
}
group.topic_is_resolved = group.topic.startsWith(resolved_topic.RESOLVED_TOPIC_PREFIX);
group.topic_is_resolved = resolved_topic.is_resolved(group.topic);
group.topic_muted = muted_topics.is_topic_muted(group.stream_id, group.topic);
} else if (group.is_private) {
group.pm_with_url = message_container.pm_with_url;

View File

@ -291,7 +291,7 @@ function build_topic_popover(opts) {
topic_muted,
can_move_topic,
is_realm_admin: page_params.is_admin,
topic_is_resolved: topic_name.startsWith(resolved_topic.RESOLVED_TOPIC_PREFIX),
topic_is_resolved: resolved_topic.is_resolved(topic_name),
color: sub.color,
has_starred_messages,
});

View File

@ -1,2 +1,6 @@
/** The canonical form of the resolved-topic prefix. */
export const RESOLVED_TOPIC_PREFIX = "✔ ";
export function is_resolved(topic_name) {
return topic_name.startsWith(RESOLVED_TOPIC_PREFIX);
}

View File

@ -1,3 +1,5 @@
// @flow strict
declare export var RESOLVED_TOPIC_PREFIX: string;
declare export function is_resolved(topic_name: string): boolean;