Skip to content

Commit fb6b53b

Browse files
PIG208gnprice
authored andcommitted
api [nfc]: Extract Conversation.isSameAs
This will make it easier to support comparing the conversations between subclasses of MessageBase. The message list tests on displayRecipient are now mostly exercising the logic on Conversation.isSameAs, which makes it reasonable to move the tests. Keep them here for now since this logic is more relevant to message lists than it is to the rest of the app. Dropped the comment on _equalIdSequences since it is now private in the short body of DmConversation.
1 parent 2d7d83d commit fb6b53b

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

lib/api/model/model.dart

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,10 @@ extension type const TopicName(String _value) {
614614
/// Different from [MessageDestination], this information comes from
615615
/// [getMessages] or [getEvents], identifying the conversation that contains a
616616
/// message.
617-
sealed class Conversation {}
617+
sealed class Conversation {
618+
/// Whether [this] and [other] refer to the same Zulip conversation.
619+
bool isSameAs(Conversation other);
620+
}
618621

619622
/// The conversation a stream message is in.
620623
@JsonSerializable(fieldRename: FieldRename.snake, createToJson: false)
@@ -640,6 +643,13 @@ class StreamConversation extends Conversation {
640643

641644
factory StreamConversation.fromJson(Map<String, dynamic> json) =>
642645
_$StreamConversationFromJson(json);
646+
647+
@override
648+
bool isSameAs(Conversation other) {
649+
return other is StreamConversation
650+
&& streamId == other.streamId
651+
&& topic.isSameAs(other.topic);
652+
}
643653
}
644654

645655
/// The conversation a DM message is in.
@@ -653,6 +663,21 @@ class DmConversation extends Conversation {
653663

654664
DmConversation({required this.allRecipientIds})
655665
: assert(isSortedWithoutDuplicates(allRecipientIds.toList()));
666+
667+
bool _equalIdSequences(Iterable<int> xs, Iterable<int> ys) {
668+
if (xs.length != ys.length) return false;
669+
final xs_ = xs.iterator; final ys_ = ys.iterator;
670+
while (xs_.moveNext() && ys_.moveNext()) {
671+
if (xs_.current != ys_.current) return false;
672+
}
673+
return true;
674+
}
675+
676+
@override
677+
bool isSameAs(Conversation other) {
678+
if (other is! DmConversation) return false;
679+
return _equalIdSequences(allRecipientIds, other.allRecipientIds);
680+
}
656681
}
657682

658683
/// A message or message-like object, for showing in a message list.

lib/model/message_list.dart

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -351,26 +351,7 @@ mixin _MessageSequence {
351351

352352
@visibleForTesting
353353
bool haveSameRecipient(Message prevMessage, Message message) {
354-
if (prevMessage is StreamMessage && message is StreamMessage) {
355-
if (prevMessage.streamId != message.streamId) return false;
356-
if (prevMessage.topic.canonicalize() != message.topic.canonicalize()) return false;
357-
} else if (prevMessage is DmMessage && message is DmMessage) {
358-
if (!_equalIdSequences(prevMessage.allRecipientIds, message.allRecipientIds)) {
359-
return false;
360-
}
361-
} else {
362-
return false;
363-
}
364-
return true;
365-
366-
// switch ((prevMessage, message)) {
367-
// case (StreamMessage(), StreamMessage()):
368-
// // TODO(dart-3): this doesn't type-narrow prevMessage and message
369-
// case (DmMessage(), DmMessage()):
370-
// // …
371-
// default:
372-
// return false;
373-
// }
354+
return prevMessage.conversation.isSameAs(message.conversation);
374355
}
375356

376357
@visibleForTesting
@@ -382,16 +363,6 @@ bool messagesSameDay(Message prevMessage, Message message) {
382363
return true;
383364
}
384365

385-
// Intended for [Message.allRecipientIds]. Assumes efficient `length`.
386-
bool _equalIdSequences(Iterable<int> xs, Iterable<int> ys) {
387-
if (xs.length != ys.length) return false;
388-
final xs_ = xs.iterator; final ys_ = ys.iterator;
389-
while (xs_.moveNext() && ys_.moveNext()) {
390-
if (xs_.current != ys_.current) return false;
391-
}
392-
return true;
393-
}
394-
395366
bool _sameDay(DateTime date1, DateTime date2) {
396367
if (date1.year != date2.year) return false;
397368
if (date1.month != date2.month) return false;

0 commit comments

Comments
 (0)