Skip to content

Commit 377544a

Browse files
committed
api [nfc]: Add/use topicsMatchModuloResolvePrefix helper, with tests
1 parent e58963e commit 377544a

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

lib/api/model/model.dart

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -813,19 +813,8 @@ enum MessageEditState {
813813
/// The Zulip "resolved topics" feature is implemented by renaming the topic;
814814
/// but for purposes of [Message.editState], we want to ignore such renames.
815815
/// This method identifies topic moves that should be ignored in that context.
816-
static bool topicMoveWasResolveOrUnresolve(String topic, String prevTopic) {
817-
if (topic.startsWith(kResolvedTopicPrefix)
818-
&& topic.substring(kResolvedTopicPrefix.length) == prevTopic) {
819-
return true;
820-
}
821-
822-
if (prevTopic.startsWith(kResolvedTopicPrefix)
823-
&& prevTopic.substring(kResolvedTopicPrefix.length) == topic) {
824-
return true;
825-
}
826-
827-
return false;
828-
}
816+
static bool topicMoveWasResolveOrUnresolve(String topic, String prevTopic) =>
817+
topic != prevTopic && topicsMatchModuloResolvePrefix(topic, prevTopic);
829818

830819
static MessageEditState _readFromMessage(Map<dynamic, dynamic> json, String key) {
831820
// Adapted from `analyze_edit_history` in the web app:

lib/api/route/messages.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ const int kMaxTopicLength = 60;
163163
// https://github.com/zulip/zulip/blob/1fac99733/web/shared/src/resolved_topic.ts
164164
const String kResolvedTopicPrefix = '✔ ';
165165

166+
String stripResolvePrefixIfPresent(String topic) =>
167+
topic.startsWith(kResolvedTopicPrefix)
168+
? topic.substring(kResolvedTopicPrefix.length)
169+
: topic;
170+
171+
bool topicsMatchModuloResolvePrefix(String topicA, String topicB) =>
172+
stripResolvePrefixIfPresent(topicA) == stripResolvePrefixIfPresent(topicB);
173+
166174
// https://zulip.com/api/send-message#parameter-content
167175
const int kMaxMessageLengthCodePoints = 10000;
168176

test/api/route/messages_test.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,42 @@ void main() {
297297
});
298298
});
299299

300+
group('resolve/unresolve with kResolvedTopicPrefix', () {
301+
group('stripResolvePrefixIfPresent', () {
302+
void doTest(String description, String input, String expected) {
303+
test(description, () {
304+
check(stripResolvePrefixIfPresent(input)).equals(expected);
305+
});
306+
}
307+
doTest('not present', 'topic', 'topic');
308+
doTest('present once', '✔ topic', 'topic');
309+
doTest('present twice', '✔ ✔ topic', '✔ topic');
310+
doTest('present in middle', 'some ✔ topic', 'some ✔ topic');
311+
});
312+
313+
group('topicsMatchModuloResolvePrefix', () {
314+
void doTest(String description, String topicA, String topicB, bool expected) {
315+
test(description, () {
316+
check(topicsMatchModuloResolvePrefix(topicA, topicB)).equals(expected);
317+
});
318+
}
319+
doTest('same-named, both unresolved', 'topic', 'topic', true);
320+
doTest('same-named, topicA resolved', '✔ topic', 'topic', true);
321+
doTest('same-named, topicB resolved', 'topic', '✔ topic', true);
322+
doTest('same-named, both resolved', '✔ topic', '✔ topic', true);
323+
324+
doTest('different-named, both unresolved', 'this', 'other', false);
325+
doTest('different-named, topicA resolved', '✔ this', 'other', false);
326+
doTest('different-named, topicB resolved', 'this', '✔ other', false);
327+
doTest('different-named, both resolved', '✔ this', '✔ other', false);
328+
329+
doTest('different-named because one duplicates prefix',
330+
'✔ topic', '✔ ✔ topic', false);
331+
doTest("different-named but one's name is a prefix of the other",
332+
'topic', 'topic zulip', false);
333+
});
334+
});
335+
300336
group('sendMessage', () {
301337
const streamId = 123;
302338
const content = 'hello';

0 commit comments

Comments
 (0)