Skip to content

Commit a127481

Browse files
gnpricechrisbobbe
authored andcommitted
store: Update account record from initial snapshot
Fixes: #458
1 parent d5181b7 commit a127481

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

lib/model/store.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:async';
22
import 'dart:convert';
33
import 'dart:io';
44

5+
import 'package:drift/drift.dart';
56
import 'package:drift/native.dart';
67
import 'package:flutter/foundation.dart';
78
import 'package:path/path.dart' as p;
@@ -630,14 +631,25 @@ class UpdateMachine {
630631
///
631632
/// In the future this might load an old snapshot from local storage first.
632633
static Future<UpdateMachine> load(GlobalStore globalStore, int accountId) async {
633-
final account = globalStore.getAccount(accountId)!;
634+
Account account = globalStore.getAccount(accountId)!;
634635
final connection = globalStore.apiConnectionFromAccount(account);
635636

636637
final stopwatch = Stopwatch()..start();
637638
final initialSnapshot = await _registerQueueWithRetry(connection);
638639
final t = (stopwatch..stop()).elapsed;
639640
assert(debugLog("initial fetch time: ${t.inMilliseconds}ms"));
640641

642+
if (initialSnapshot.zulipVersion != account.zulipVersion
643+
|| initialSnapshot.zulipMergeBase != account.zulipMergeBase
644+
|| initialSnapshot.zulipFeatureLevel != account.zulipFeatureLevel) {
645+
account = await globalStore.updateAccount(accountId, AccountsCompanion(
646+
zulipVersion: Value(initialSnapshot.zulipVersion),
647+
zulipMergeBase: Value(initialSnapshot.zulipMergeBase),
648+
zulipFeatureLevel: Value(initialSnapshot.zulipFeatureLevel),
649+
));
650+
connection.zulipFeatureLevel = initialSnapshot.zulipFeatureLevel;
651+
}
652+
641653
final store = PerAccountStore.fromInitialSnapshot(
642654
globalStore: globalStore,
643655
accountId: accountId,

test/model/store_test.dart

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import '../example_data.dart' as eg;
1717
import '../fake_async.dart';
1818
import '../stdlib_checks.dart';
1919
import 'binding.dart';
20+
import 'store_checks.dart';
2021
import 'test_store.dart';
2122

2223
void main() {
@@ -187,10 +188,11 @@ void main() {
187188
late TestGlobalStore globalStore;
188189
late FakeApiConnection connection;
189190

190-
Future<void> prepareStore() async {
191+
Future<void> prepareStore({Account? account}) async {
191192
globalStore = TestGlobalStore(accounts: []);
192-
await globalStore.insertAccount(eg.selfAccount.toCompanion(false));
193-
connection = (globalStore.apiConnectionFromAccount(eg.selfAccount)
193+
account ??= eg.selfAccount;
194+
await globalStore.insertAccount(account.toCompanion(false));
195+
connection = (globalStore.apiConnectionFromAccount(account)
194196
as FakeApiConnection);
195197
UpdateMachine.debugEnableRegisterNotificationToken = false;
196198
addTearDown(() => UpdateMachine.debugEnableRegisterNotificationToken = true);
@@ -220,6 +222,32 @@ void main() {
220222
users.map((expected) => (it) => it.fullName.equals(expected.fullName)));
221223
}));
222224

225+
test('updates account from snapshot', () => awaitFakeAsync((async) async {
226+
final account = eg.account(user: eg.selfUser,
227+
zulipVersion: '6.0+gabcd',
228+
zulipMergeBase: '6.0',
229+
zulipFeatureLevel: 123,
230+
);
231+
await prepareStore(account: account);
232+
check(globalStore.getAccount(account.id)).isNotNull()
233+
..zulipVersion.equals('6.0+gabcd')
234+
..zulipMergeBase.equals('6.0')
235+
..zulipFeatureLevel.equals(123);
236+
237+
connection.prepare(json: eg.initialSnapshot(
238+
zulipVersion: '8.0+g9876',
239+
zulipMergeBase: '8.0',
240+
zulipFeatureLevel: 234,
241+
).toJson());
242+
final updateMachine = await UpdateMachine.load(globalStore, account.id);
243+
updateMachine.debugPauseLoop();
244+
check(globalStore.getAccount(account.id)).isNotNull()
245+
..identicalTo(updateMachine.store.account)
246+
..zulipVersion.equals('8.0+g9876')
247+
..zulipMergeBase.equals('8.0')
248+
..zulipFeatureLevel.equals(234);
249+
}));
250+
223251
test('retries registerQueue on NetworkError', () => awaitFakeAsync((async) async {
224252
await prepareStore();
225253

0 commit comments

Comments
 (0)