Skip to content

Commit d316dda

Browse files
committed
store [nfc]: Helper code for UpdateMachine.load's update-feature-level
This will be helpful for disallowing ancient servers, coming up.
1 parent 4b4962c commit d316dda

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

lib/model/store.dart

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -970,24 +970,27 @@ class UpdateMachine {
970970
}
971971
}
972972

973+
Future<void> updateZulipVersionData(ZulipVersionData data) async {
974+
account = globalStore.getAccount(accountId)!;
975+
if (data.zulipVersion != account.zulipVersion
976+
|| data.zulipMergeBase != account.zulipMergeBase
977+
|| data.zulipFeatureLevel != account.zulipFeatureLevel) {
978+
account = await globalStore.updateAccount(accountId, AccountsCompanion(
979+
zulipVersion: Value(data.zulipVersion),
980+
zulipMergeBase: Value(data.zulipMergeBase),
981+
zulipFeatureLevel: Value(data.zulipFeatureLevel)));
982+
connection.zulipFeatureLevel = data.zulipFeatureLevel;
983+
}
984+
}
985+
973986
final stopwatch = Stopwatch()..start();
974987
final initialSnapshot = await _registerQueueWithRetry(connection,
975988
stopAndThrowIfNoAccount: stopAndThrowIfNoAccount);
976989
final t = (stopwatch..stop()).elapsed;
977990
assert(debugLog("initial fetch time: ${t.inMilliseconds}ms"));
978991

979-
// `!` is OK because _registerQueueWithRetry would have thrown if no account
980-
account = globalStore.getAccount(accountId)!;
981-
if (initialSnapshot.zulipVersion != account.zulipVersion
982-
|| initialSnapshot.zulipMergeBase != account.zulipMergeBase
983-
|| initialSnapshot.zulipFeatureLevel != account.zulipFeatureLevel) {
984-
account = await globalStore.updateAccount(accountId, AccountsCompanion(
985-
zulipVersion: Value(initialSnapshot.zulipVersion),
986-
zulipMergeBase: Value(initialSnapshot.zulipMergeBase),
987-
zulipFeatureLevel: Value(initialSnapshot.zulipFeatureLevel),
988-
));
989-
connection.zulipFeatureLevel = initialSnapshot.zulipFeatureLevel;
990-
}
992+
final zulipVersionData = ZulipVersionData.fromInitialSnapshot(initialSnapshot);
993+
await updateZulipVersionData(zulipVersionData);
991994

992995
final store = PerAccountStore.fromInitialSnapshot(
993996
globalStore: globalStore,
@@ -1456,6 +1459,26 @@ class UpdateMachine {
14561459
String toString() => '${objectRuntimeType(this, 'UpdateMachine')}#${shortHash(this)}';
14571460
}
14581461

1462+
/// The fields 'zulip_version', 'zulip_merge_base', and 'zulip_feature_level'
1463+
/// from a /register response.
1464+
class ZulipVersionData {
1465+
ZulipVersionData({
1466+
required this.zulipVersion,
1467+
required this.zulipMergeBase,
1468+
required this.zulipFeatureLevel,
1469+
});
1470+
1471+
factory ZulipVersionData.fromInitialSnapshot(InitialSnapshot initialSnapshot) =>
1472+
ZulipVersionData(
1473+
zulipVersion: initialSnapshot.zulipVersion,
1474+
zulipMergeBase: initialSnapshot.zulipMergeBase,
1475+
zulipFeatureLevel: initialSnapshot.zulipFeatureLevel);
1476+
1477+
final String zulipVersion;
1478+
final String? zulipMergeBase;
1479+
final int zulipFeatureLevel;
1480+
}
1481+
14591482
class _EventHandlingException implements Exception {
14601483
final Object cause;
14611484
final Event event;

0 commit comments

Comments
 (0)