Skip to content

Commit 4b8615f

Browse files
committed
store [nfc]: Move selfUserId to PerAccountStoreBase
1 parent 9c09b6d commit 4b8615f

8 files changed

+50
-43
lines changed

lib/model/recent_dm_conversations.dart

+6-7
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@ import '../api/model/initial_snapshot.dart';
77
import '../api/model/model.dart';
88
import '../api/model/events.dart';
99
import 'narrow.dart';
10+
import 'store.dart';
1011

1112
/// A view-model for the recent-DM-conversations UI.
1213
///
1314
/// This maintains the list of recent DM conversations,
1415
/// plus additional data in order to efficiently maintain the list.
15-
class RecentDmConversationsView extends ChangeNotifier {
16+
class RecentDmConversationsView extends PerAccountStoreBase with ChangeNotifier {
1617
factory RecentDmConversationsView({
18+
required CorePerAccountStore core,
1719
required List<RecentDmConversation> initial,
18-
required int selfUserId,
1920
}) {
2021
final entries = initial.map((conversation) => MapEntry(
21-
DmNarrow.ofRecentDmConversation(conversation, selfUserId: selfUserId),
22+
DmNarrow.ofRecentDmConversation(conversation, selfUserId: core.selfUserId),
2223
conversation.maxMessageId,
2324
)).toList()..sort((a, b) => -a.value.compareTo(b.value));
2425

@@ -33,18 +34,18 @@ class RecentDmConversationsView extends ChangeNotifier {
3334
}
3435

3536
return RecentDmConversationsView._(
37+
core: core,
3638
map: Map.fromEntries(entries),
3739
sorted: QueueList.from(entries.map((e) => e.key)),
3840
latestMessagesByRecipient: latestMessagesByRecipient,
39-
selfUserId: selfUserId,
4041
);
4142
}
4243

4344
RecentDmConversationsView._({
45+
required super.core,
4446
required this.map,
4547
required this.sorted,
4648
required this.latestMessagesByRecipient,
47-
required this.selfUserId,
4849
});
4950

5051
/// The latest message ID in each conversation.
@@ -62,8 +63,6 @@ class RecentDmConversationsView extends ChangeNotifier {
6263
/// it might have been sent by anyone in its conversation.)
6364
final Map<int, int> latestMessagesByRecipient;
6465

65-
final int selfUserId;
66-
6766
/// Insert the key at the proper place in [sorted].
6867
///
6968
/// Optimized, taking O(1) time, for the case where that place is the start,

lib/model/store.dart

+14-6
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,15 @@ class AccountNotFoundException implements Exception {}
329329

330330
/// A bundle of items that are useful to [PerAccountStore] and its substores.
331331
class CorePerAccountStore {
332-
CorePerAccountStore({required this.connection, required this.queueId});
332+
CorePerAccountStore({
333+
required this.connection,
334+
required this.queueId,
335+
required this.selfUserId,
336+
});
333337

334338
final ApiConnection connection; // TODO(#135): update zulipFeatureLevel with events
335339
final String queueId;
340+
final int selfUserId;
336341
}
337342

338343
/// A base class for [PerAccountStore] and its substores,
@@ -345,6 +350,7 @@ abstract class PerAccountStoreBase {
345350

346351
ApiConnection get connection => _core.connection;
347352
String get queueId => _core.queueId;
353+
int get selfUserId => _core.selfUserId;
348354
}
349355

350356
/// Store for the user's data for a given Zulip account.
@@ -388,7 +394,8 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
388394
}
389395

390396
final realmUrl = account.realmUrl;
391-
final core = CorePerAccountStore(connection: connection, queueId: queueId);
397+
final core = CorePerAccountStore(
398+
connection: connection, queueId: queueId, selfUserId: account.userId);
392399
final channels = ChannelStoreImpl(initialSnapshot: initialSnapshot);
393400
return PerAccountStore._(
394401
globalStore: globalStore,
@@ -414,21 +421,22 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
414421
milliseconds: initialSnapshot.serverTypingStartedWaitPeriodMilliseconds),
415422
),
416423
users: UserStoreImpl(
417-
selfUserId: account.userId,
424+
core: core,
418425
initialSnapshot: initialSnapshot),
419426
typingStatus: TypingStatus(
420-
selfUserId: account.userId,
427+
core: core,
421428
typingStartedExpiryPeriod: Duration(milliseconds: initialSnapshot.serverTypingStartedExpiryPeriodMilliseconds),
422429
),
423430
channels: channels,
424431
messages: MessageStoreImpl(core: core),
425432
unreads: Unreads(
433+
core: core,
426434
initial: initialSnapshot.unreadMsgs,
427-
selfUserId: account.userId,
428435
channelStore: channels,
429436
),
430437
recentDmConversationsView: RecentDmConversationsView(
431-
initial: initialSnapshot.recentPrivateConversations, selfUserId: account.userId),
438+
core: core,
439+
initial: initialSnapshot.recentPrivateConversations),
432440
recentSenders: RecentSenders(),
433441
);
434442
}

lib/model/typing_status.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ import '../api/model/events.dart';
77
import '../api/route/typing.dart';
88
import 'binding.dart';
99
import 'narrow.dart';
10+
import 'store.dart';
1011

1112
/// The model for tracking the typing status organized by narrows.
1213
///
1314
/// Listeners are notified when a typist is added or removed from any narrow.
14-
class TypingStatus extends ChangeNotifier {
15+
class TypingStatus extends PerAccountStoreBase with ChangeNotifier {
1516
TypingStatus({
16-
required this.selfUserId,
17+
required super.core,
1718
required this.typingStartedExpiryPeriod,
1819
});
1920

20-
final int selfUserId;
2121
final Duration typingStartedExpiryPeriod;
2222

2323
Iterable<SendableNarrow> get debugActiveNarrows => _timerMapsByNarrow.keys;

lib/model/unreads.dart

+7-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import '../log.dart';
1010
import 'algorithms.dart';
1111
import 'narrow.dart';
1212
import 'channel.dart';
13+
import 'store.dart';
1314

1415
/// The view-model for unread messages.
1516
///
@@ -34,10 +35,10 @@ import 'channel.dart';
3435
// sync to those unreads, because the user has shown an interest in them.
3536
// TODO When loading a message list with stream messages, check all the stream
3637
// messages and refresh [mentions] (see [mentions] dartdoc).
37-
class Unreads extends ChangeNotifier {
38+
class Unreads extends PerAccountStoreBase with ChangeNotifier {
3839
factory Unreads({
40+
required CorePerAccountStore core,
3941
required UnreadMessagesSnapshot initial,
40-
required int selfUserId,
4142
required ChannelStore channelStore,
4243
}) {
4344
final streams = <int, Map<TopicName, QueueList<int>>>{};
@@ -52,32 +53,32 @@ class Unreads extends ChangeNotifier {
5253

5354
for (final unreadDmSnapshot in initial.dms) {
5455
final otherUserId = unreadDmSnapshot.otherUserId;
55-
final narrow = DmNarrow.withUser(otherUserId, selfUserId: selfUserId);
56+
final narrow = DmNarrow.withUser(otherUserId, selfUserId: core.selfUserId);
5657
dms[narrow] = QueueList.from(unreadDmSnapshot.unreadMessageIds);
5758
}
5859

5960
for (final unreadHuddleSnapshot in initial.huddles) {
60-
final narrow = DmNarrow.ofUnreadHuddleSnapshot(unreadHuddleSnapshot, selfUserId: selfUserId);
61+
final narrow = DmNarrow.ofUnreadHuddleSnapshot(unreadHuddleSnapshot, selfUserId: core.selfUserId);
6162
dms[narrow] = QueueList.from(unreadHuddleSnapshot.unreadMessageIds);
6263
}
6364

6465
return Unreads._(
66+
core: core,
6567
channelStore: channelStore,
6668
streams: streams,
6769
dms: dms,
6870
mentions: mentions,
6971
oldUnreadsMissing: initial.oldUnreadsMissing,
70-
selfUserId: selfUserId,
7172
);
7273
}
7374

7475
Unreads._({
76+
required super.core,
7577
required this.channelStore,
7678
required this.streams,
7779
required this.dms,
7880
required this.mentions,
7981
required this.oldUnreadsMissing,
80-
required this.selfUserId,
8182
});
8283

8384
final ChannelStore channelStore;
@@ -125,8 +126,6 @@ class Unreads extends ChangeNotifier {
125126
/// Is set to false when the user clears out all unreads.
126127
bool oldUnreadsMissing;
127128

128-
final int selfUserId;
129-
130129
// TODO(#370): maintain this count incrementally, rather than recomputing from scratch
131130
int countInCombinedFeedNarrow() {
132131
int c = 0;

lib/model/user.dart

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import '../api/model/events.dart';
22
import '../api/model/initial_snapshot.dart';
33
import '../api/model/model.dart';
44
import 'localizations.dart';
5+
import 'store.dart';
56

67
/// The portion of [PerAccountStore] describing the users in the realm.
78
mixin UserStore {
@@ -80,19 +81,16 @@ mixin UserStore {
8081
/// Generally the only code that should need this class is [PerAccountStore]
8182
/// itself. Other code accesses this functionality through [PerAccountStore],
8283
/// or through the mixin [UserStore] which describes its interface.
83-
class UserStoreImpl with UserStore {
84+
class UserStoreImpl extends PerAccountStoreBase with UserStore {
8485
UserStoreImpl({
85-
required this.selfUserId,
86+
required super.core,
8687
required InitialSnapshot initialSnapshot,
8788
}) : _users = Map.fromEntries(
8889
initialSnapshot.realmUsers
8990
.followedBy(initialSnapshot.realmNonActiveUsers)
9091
.followedBy(initialSnapshot.crossRealmBots)
9192
.map((user) => MapEntry(user.userId, user)));
9293

93-
@override
94-
final int selfUserId;
95-
9694
final Map<int, User> _users;
9795

9896
@override

test/model/recent_dm_conversations_test.dart

+10-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:zulip/model/recent_dm_conversations.dart';
66

77
import '../example_data.dart' as eg;
88
import 'recent_dm_conversations_checks.dart';
9+
import 'store_checks.dart';
910

1011
void main() {
1112
group('RecentDmConversationsView', () {
@@ -18,18 +19,19 @@ void main() {
1819
}
1920

2021
test('construct from initial data', () {
21-
check(RecentDmConversationsView(selfUserId: eg.selfUser.userId,
22-
initial: []))
22+
check(eg.store(initialSnapshot: eg.initialSnapshot(
23+
recentPrivateConversations: [],
24+
))).recentDmConversationsView
2325
..map.isEmpty()
2426
..sorted.isEmpty()
2527
..latestMessagesByRecipient.isEmpty();
2628

27-
check(RecentDmConversationsView(selfUserId: eg.selfUser.userId,
28-
initial: [
29+
check(eg.store(initialSnapshot: eg.initialSnapshot(
30+
recentPrivateConversations: [
2931
RecentDmConversation(userIds: [], maxMessageId: 200),
3032
RecentDmConversation(userIds: [1], maxMessageId: 100),
3133
RecentDmConversation(userIds: [2, 1], maxMessageId: 300), // userIds out of order
32-
]))
34+
]))).recentDmConversationsView
3335
..map.deepEquals({
3436
key([1, 2]): 300,
3537
key([]): 200,
@@ -41,11 +43,11 @@ void main() {
4143

4244
group('message event (new message)', () {
4345
RecentDmConversationsView setupView() {
44-
return RecentDmConversationsView(selfUserId: eg.selfUser.userId,
45-
initial: [
46+
return eg.store(initialSnapshot: eg.initialSnapshot(
47+
recentPrivateConversations: [
4648
RecentDmConversation(userIds: [1], maxMessageId: 200),
4749
RecentDmConversation(userIds: [1, 2], maxMessageId: 100),
48-
]);
50+
])).recentDmConversationsView;
4951
}
5052

5153
test('(check base state)', () {

test/model/typing_status_test.dart

+4-3
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ void main() {
7777
int? selfUserId,
7878
Map<SendableNarrow, List<User>> typistsByNarrow = const {},
7979
}) {
80-
model = TypingStatus(
81-
selfUserId: selfUserId ?? eg.selfUser.userId,
82-
typingStartedExpiryPeriod: const Duration(milliseconds: 15000));
80+
final store = eg.store(account: eg.selfAccount.copyWith(id: selfUserId),
81+
initialSnapshot: eg.initialSnapshot(
82+
serverTypingStartedExpiryPeriodMilliseconds: 15000));
83+
model = store.typingStatus;
8384
check(model.debugActiveNarrows).isEmpty();
8485
notifiedCount = 0;
8586
model.addListener(() => notifiedCount += 1);

test/model/unreads_test.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ void main() {
3737
oldUnreadsMissing: false,
3838
),
3939
}) {
40-
channelStore = eg.store();
40+
channelStore = eg.store(
41+
initialSnapshot: eg.initialSnapshot(unreadMsgs: initial));
4142
notifiedCount = 0;
42-
model = Unreads(initial: initial,
43-
selfUserId: eg.selfUser.userId, channelStore: channelStore)
43+
model = channelStore.unreads
4444
..addListener(() {
4545
notifiedCount++;
4646
});

0 commit comments

Comments
 (0)