Skip to content

Commit e1fb60c

Browse files
store: Update account with realmName and realmIcon from InitialSnapshot
This will populate these fields for the currently opened account in the database when the PerAccountStore loads.
1 parent b6ea8c7 commit e1fb60c

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

lib/model/store.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,28 @@ abstract class GlobalStore extends ChangeNotifier {
323323
zulipFeatureLevel: Value(data.zulipFeatureLevel)));
324324
}
325325

326+
/// Update an account with [realmName] and [realmIcon], returning the new version.
327+
///
328+
/// The account must already exist in the store.
329+
Future<Account> updateRealmData(int accountId, {
330+
required String realmName,
331+
required Uri realmIcon,
332+
}) async {
333+
final account = getAccount(accountId)!;
334+
if (account.realmName == realmName && account.realmIcon == realmIcon) {
335+
return account;
336+
}
337+
338+
return updateAccount(accountId, AccountsCompanion(
339+
realmName: account.realmName != realmName
340+
? Value(realmName)
341+
: const Value.absent(),
342+
realmIcon: account.realmIcon != realmIcon
343+
? Value(realmIcon)
344+
: const Value.absent(),
345+
));
346+
}
347+
326348
/// Update an account in the underlying data store.
327349
Future<void> doUpdateAccount(int accountId, AccountsCompanion data);
328350

@@ -1102,6 +1124,11 @@ class UpdateMachine {
11021124
await globalStore.updateZulipVersionData(accountId, zulipVersionData);
11031125
connection.zulipFeatureLevel = zulipVersionData.zulipFeatureLevel;
11041126
}
1127+
if (globalStore.getAccount(accountId) != null) {
1128+
await globalStore.updateRealmData(accountId,
1129+
realmName: initialSnapshot.realmName,
1130+
realmIcon: initialSnapshot.realmIconUrl);
1131+
}
11051132

11061133
final store = PerAccountStore.fromInitialSnapshot(
11071134
globalStore: globalStore,

lib/widgets/login.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,7 @@ class _LoginPageState extends State<LoginPage> {
409409
email: email,
410410
apiKey: apiKey,
411411
userId: userId,
412-
// TODO store the updated value from /register
413412
realmName: Value(widget.serverSettings.realmName),
414-
// TODO store the updated value from /register
415413
realmIcon: Value(widget.serverSettings.realmIcon),
416414
zulipFeatureLevel: widget.serverSettings.zulipFeatureLevel,
417415
zulipVersion: widget.serverSettings.zulipVersion,

test/model/store_test.dart

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ void main() {
354354

355355
// TODO test database gets updated correctly (an integration test with sqlite?)
356356
});
357-
357+
358358
test('GlobalStore.updateZulipVersionData', () async {
359359
final [currentZulipVersion, newZulipVersion ]
360360
= ['10.0-beta2-302-gf5b08b11f4', '10.0-beta2-351-g75ac8fe961'];
@@ -380,6 +380,20 @@ void main() {
380380
zulipFeatureLevel: newZulipFeatureLevel));
381381
});
382382

383+
test('GlobalStore.updateRealmData', () async {
384+
final selfAccount = eg.selfAccount.copyWith(
385+
realmName: Value('Organization A'),
386+
realmIcon: Value(Uri.parse('/image-a.png')));
387+
final globalStore = eg.globalStore(accounts: [selfAccount]);
388+
final updated = await globalStore.updateRealmData(selfAccount.id,
389+
realmName: 'Organization B',
390+
realmIcon: Uri.parse('/image-b.png'));
391+
check(globalStore.getAccount(selfAccount.id)).identicalTo(updated);
392+
check(updated).equals(selfAccount.copyWith(
393+
realmName: Value('Organization B'),
394+
realmIcon: Value(Uri.parse('/image-b.png'))));
395+
});
396+
383397
group('GlobalStore.removeAccount', () {
384398
void checkGlobalStore(GlobalStore store, int accountId, {
385399
required bool expectAccount,
@@ -515,18 +529,24 @@ void main() {
515529

516530
test('updates account from snapshot', () => awaitFakeAsync((async) async {
517531
final account = eg.account(user: eg.selfUser,
532+
realmName: 'Organization A',
533+
realmIcon: Uri.parse('/image-a.png'),
518534
zulipVersion: '6.0+gabcd',
519535
zulipMergeBase: '6.0',
520536
zulipFeatureLevel: 123,
521537
);
522538
await prepareStore(account: account);
523539
check(globalStore.getAccount(account.id)).isNotNull()
540+
..realmName.equals('Organization A')
541+
..realmIcon.equals(Uri.parse('/image-a.png'))
524542
..zulipVersion.equals('6.0+gabcd')
525543
..zulipMergeBase.equals('6.0')
526544
..zulipFeatureLevel.equals(123);
527545

528546
globalStore.useCachedApiConnections = true;
529547
connection.prepare(json: eg.initialSnapshot(
548+
realmName: 'Organization B',
549+
realmIconUrl: Uri.parse('/image-b.png'),
530550
zulipVersion: '8.0+g9876',
531551
zulipMergeBase: '8.0',
532552
zulipFeatureLevel: 234,
@@ -535,6 +555,8 @@ void main() {
535555
updateMachine.debugPauseLoop();
536556
check(globalStore.getAccount(account.id)).isNotNull()
537557
..identicalTo(updateMachine.store.account)
558+
..realmName.equals('Organization B')
559+
..realmIcon.equals(Uri.parse('/image-b.png'))
538560
..zulipVersion.equals('8.0+g9876')
539561
..zulipMergeBase.equals('8.0')
540562
..zulipFeatureLevel.equals(234);

0 commit comments

Comments
 (0)