Skip to content

Commit 2983cdb

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 ebe5f70 commit 2983cdb

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
@@ -995,24 +995,27 @@ class UpdateMachine {
995995
}
996996
}
997997

998+
Future<void> updateZulipVersionData(ZulipVersionData data) async {
999+
account = globalStore.getAccount(accountId)!;
1000+
if (data.zulipVersion != account.zulipVersion
1001+
|| data.zulipMergeBase != account.zulipMergeBase
1002+
|| data.zulipFeatureLevel != account.zulipFeatureLevel) {
1003+
account = await globalStore.updateAccount(accountId, AccountsCompanion(
1004+
zulipVersion: Value(data.zulipVersion),
1005+
zulipMergeBase: Value(data.zulipMergeBase),
1006+
zulipFeatureLevel: Value(data.zulipFeatureLevel)));
1007+
connection.zulipFeatureLevel = data.zulipFeatureLevel;
1008+
}
1009+
}
1010+
9981011
final stopwatch = Stopwatch()..start();
9991012
final initialSnapshot = await _registerQueueWithRetry(connection,
10001013
stopAndThrowIfNoAccount: stopAndThrowIfNoAccount);
10011014
final t = (stopwatch..stop()).elapsed;
10021015
assert(debugLog("initial fetch time: ${t.inMilliseconds}ms"));
10031016

1004-
// `!` is OK because _registerQueueWithRetry would have thrown if no account
1005-
account = globalStore.getAccount(accountId)!;
1006-
if (initialSnapshot.zulipVersion != account.zulipVersion
1007-
|| initialSnapshot.zulipMergeBase != account.zulipMergeBase
1008-
|| initialSnapshot.zulipFeatureLevel != account.zulipFeatureLevel) {
1009-
account = await globalStore.updateAccount(accountId, AccountsCompanion(
1010-
zulipVersion: Value(initialSnapshot.zulipVersion),
1011-
zulipMergeBase: Value(initialSnapshot.zulipMergeBase),
1012-
zulipFeatureLevel: Value(initialSnapshot.zulipFeatureLevel),
1013-
));
1014-
connection.zulipFeatureLevel = initialSnapshot.zulipFeatureLevel;
1015-
}
1017+
final zulipVersionData = ZulipVersionData.fromInitialSnapshot(initialSnapshot);
1018+
await updateZulipVersionData(zulipVersionData);
10161019

10171020
final store = PerAccountStore.fromInitialSnapshot(
10181021
globalStore: globalStore,
@@ -1481,6 +1484,26 @@ class UpdateMachine {
14811484
String toString() => '${objectRuntimeType(this, 'UpdateMachine')}#${shortHash(this)}';
14821485
}
14831486

1487+
/// The fields 'zulip_version', 'zulip_merge_base', and 'zulip_feature_level'
1488+
/// from a /register response.
1489+
class ZulipVersionData {
1490+
const ZulipVersionData({
1491+
required this.zulipVersion,
1492+
required this.zulipMergeBase,
1493+
required this.zulipFeatureLevel,
1494+
});
1495+
1496+
factory ZulipVersionData.fromInitialSnapshot(InitialSnapshot initialSnapshot) =>
1497+
ZulipVersionData(
1498+
zulipVersion: initialSnapshot.zulipVersion,
1499+
zulipMergeBase: initialSnapshot.zulipMergeBase,
1500+
zulipFeatureLevel: initialSnapshot.zulipFeatureLevel);
1501+
1502+
final String zulipVersion;
1503+
final String? zulipMergeBase;
1504+
final int zulipFeatureLevel;
1505+
}
1506+
14841507
class _EventHandlingException implements Exception {
14851508
final Object cause;
14861509
final Event event;

0 commit comments

Comments
 (0)