Skip to content

Commit 0fdceb3

Browse files
committed
api [nfc]: Comment on why to keep an existing implementation
1 parent 2a6a504 commit 0fdceb3

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lib/api/model/model.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,14 @@ enum MessageEditState {
872872
/// but for purposes of [Message.editState], we want to ignore such renames.
873873
/// This method identifies topic moves that should be ignored in that context.
874874
static bool topicMoveWasResolveOrUnresolve(TopicName topic, TopicName prevTopic) {
875+
// Implemented to match web; see analyze_edit_history in zulip/zulip's
876+
// web/src/message_list_view.ts.
877+
//
878+
// Also, this is a hot codepath (decoding messages, a high-volume type of
879+
// data we get from the server), so we avoid calling [canonicalize] and
880+
// using [TopicName.resolvedTopicPrefixRegexp], to be performance-sensitive.
881+
// Discussion:
882+
// https://github.com/zulip/zulip-flutter/pull/1242#discussion_r1917592157
875883
if (topic.apiName.startsWith(TopicName.resolvedTopicPrefix)
876884
&& topic.apiName.substring(TopicName.resolvedTopicPrefix.length) == prevTopic.apiName) {
877885
return true;

test/api/model/model_test.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,35 @@ void main() {
325325
]);
326326
});
327327

328+
// Technically the topic *was* unresolved, so MessageEditState.none
329+
// would be valid and preferable -- if it didn't need more intense
330+
// computation than we're comfortable with in a hot codepath, i.e.,
331+
// a regex test instead of a simple `startsWith` / `substring` check.
332+
// See comment on the implementation, and discussion:
333+
// https://github.com/zulip/zulip-flutter/pull/1242#discussion_r1917592157
328334
test('Unresolving topic with a weird prefix -> moved', () {
329335
checkEditState(MessageEditState.moved,
330336
[{'prev_topic': '✔ ✔old_topic', 'topic': 'old_topic'}]);
331337
});
338+
339+
// Similar reasoning as in the previous test.
340+
// Also, Zulip doesn't produce topics with a weird resolved-topic prefix,
341+
// so this case can only be produced by unusual input in an
342+
// edit/move-topic UI. A "moved" marker seems like a fine response
343+
// in that circumstance.
344+
test('Resolving topic with a weird prefix -> moved', () {
345+
checkEditState(MessageEditState.moved,
346+
[{'prev_topic': 'old_topic', 'topic': '✔ ✔old_topic'}]);
347+
});
348+
349+
// Similar reasoning as the previous test, including that this case had to
350+
// involve unusual input in an edit/move-topic UI.
351+
// Here the computation burden would have come from calling
352+
// [TopicName.canonicalize].
353+
test('Topic was resolved but with changed case -> moved', () {
354+
checkEditState(MessageEditState.moved,
355+
[{'prev_topic': 'old ToPiC', 'topic': '✔ OLD tOpIc'}]);
356+
});
332357
});
333358
});
334359
}

0 commit comments

Comments
 (0)