From ca26716be14d541f193f471dc2f1c17bd1780f62 Mon Sep 17 00:00:00 2001 From: Shu Chen Date: Fri, 15 Sep 2023 14:51:50 +0100 Subject: [PATCH] narrow: add DmNarrow.withUsers Refactored other DmNarrow factories to use this more generic interface --- lib/model/narrow.dart | 13 ++++++++++--- test/model/narrow_test.dart | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/model/narrow.dart b/lib/model/narrow.dart index c71cfe5407a..9532b27eb4e 100644 --- a/lib/model/narrow.dart +++ b/lib/model/narrow.dart @@ -155,15 +155,22 @@ class DmNarrow extends Narrow implements SendableNarrow { /// A [DmNarrow] from an item in [InitialSnapshot.recentPrivateConversations]. factory DmNarrow.ofRecentDmConversation(RecentDmConversation conversation, {required int selfUserId}) { - return DmNarrow( - allRecipientIds: [...conversation.userIds, selfUserId]..sort(), + return DmNarrow.withUsers( + conversation.userIds, selfUserId: selfUserId, ); } factory DmNarrow.withUser(int userId, {required int selfUserId}) { + return DmNarrow.withUsers( + [userId], + selfUserId: selfUserId, + ); + } + + factory DmNarrow.withUsers(List userIds, {required int selfUserId}) { return DmNarrow( - allRecipientIds: {userId, selfUserId}.toList()..sort(), + allRecipientIds: {...userIds, selfUserId}.toList()..sort(), selfUserId: selfUserId, ); } diff --git a/test/model/narrow_test.dart b/test/model/narrow_test.dart index c7825c67663..10fb8e8efa2 100644 --- a/test/model/narrow_test.dart +++ b/test/model/narrow_test.dart @@ -90,6 +90,20 @@ void main() { selfUserId: 5)); }); + test('withUsers: without selfUserId', () { + final actual = DmNarrow.withUsers([1, 2], selfUserId: 3); + check(actual).equals(DmNarrow( + allRecipientIds: [1, 2, 3], + selfUserId: 3)); + }); + + test('withUsers: with selfUserId', () { + final actual = DmNarrow.withUsers([1, 2, 3], selfUserId: 3); + check(actual).equals(DmNarrow( + allRecipientIds: [1, 2, 3], + selfUserId: 3)); + }); + test('otherRecipientIds', () { check(DmNarrow(allRecipientIds: [1, 2, 3], selfUserId: 2)) .otherRecipientIds.deepEquals([1, 3]);