Skip to content

Commit f3e6429

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 415b1e9 commit f3e6429

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

lib/model/store.dart

Lines changed: 28 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

@@ -1107,6 +1129,12 @@ class UpdateMachine {
11071129
await globalStore.updateZulipVersionData(accountId, zulipVersionData);
11081130
connection.zulipFeatureLevel = zulipVersionData.zulipFeatureLevel;
11091131
}
1132+
if (globalStore.getAccount(accountId) != null) {
1133+
// TODO(#668) update realmName and realmIcon on realm update events
1134+
await globalStore.updateRealmData(accountId,
1135+
realmName: initialSnapshot.realmName,
1136+
realmIcon: initialSnapshot.realmIconUrl);
1137+
}
11101138

11111139
final store = PerAccountStore.fromInitialSnapshot(
11121140
globalStore: globalStore,

lib/widgets/login.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,7 @@ class _LoginPageState extends State<LoginPage> {
406406
try {
407407
accountId = await globalStore.insertAccount(AccountsCompanion.insert(
408408
realmUrl: realmUrl,
409-
// TODO store the updated value from /register
410409
realmName: Value(widget.serverSettings.realmName),
411-
// TODO store the updated value from /register
412410
realmIcon: Value(widget.serverSettings.realmIcon),
413411
email: email,
414412
apiKey: apiKey,

test/model/store_test.dart

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

351351
// TODO test database gets updated correctly (an integration test with sqlite?)
352352
});
353-
353+
354354
test('GlobalStore.updateZulipVersionData', () async {
355355
final [currentZulipVersion, newZulipVersion ]
356356
= ['10.0-beta2-302-gf5b08b11f4', '10.0-beta2-351-g75ac8fe961'];
@@ -376,6 +376,20 @@ void main() {
376376
zulipFeatureLevel: newZulipFeatureLevel));
377377
});
378378

379+
test('GlobalStore.updateRealmData', () async {
380+
final selfAccount = eg.selfAccount.copyWith(
381+
realmName: Value('Organization A'),
382+
realmIcon: Value(Uri.parse('/image-a.png')));
383+
final globalStore = eg.globalStore(accounts: [selfAccount]);
384+
final updated = await globalStore.updateRealmData(selfAccount.id,
385+
realmName: 'Organization B',
386+
realmIcon: Uri.parse('/image-b.png'));
387+
check(globalStore.getAccount(selfAccount.id)).identicalTo(updated);
388+
check(updated).equals(selfAccount.copyWith(
389+
realmName: Value('Organization B'),
390+
realmIcon: Value(Uri.parse('/image-b.png'))));
391+
});
392+
379393
group('GlobalStore.removeAccount', () {
380394
void checkGlobalStore(GlobalStore store, int accountId, {
381395
required bool expectAccount,
@@ -509,18 +523,24 @@ void main() {
509523

510524
test('updates account from snapshot', () => awaitFakeAsync((async) async {
511525
final account = eg.account(user: eg.selfUser,
526+
realmName: 'Organization A',
527+
realmIcon: Uri.parse('/image-a.png'),
512528
zulipVersion: '6.0+gabcd',
513529
zulipMergeBase: '6.0',
514530
zulipFeatureLevel: 123,
515531
);
516532
await prepareStore(account: account);
517533
check(globalStore.getAccount(account.id)).isNotNull()
534+
..realmName.equals('Organization A')
535+
..realmIcon.equals(Uri.parse('/image-a.png'))
518536
..zulipVersion.equals('6.0+gabcd')
519537
..zulipMergeBase.equals('6.0')
520538
..zulipFeatureLevel.equals(123);
521539

522540
globalStore.useCachedApiConnections = true;
523541
connection.prepare(json: eg.initialSnapshot(
542+
realmName: 'Organization B',
543+
realmIconUrl: Uri.parse('/image-b.png'),
524544
zulipVersion: '8.0+g9876',
525545
zulipMergeBase: '8.0',
526546
zulipFeatureLevel: 234,
@@ -529,6 +549,8 @@ void main() {
529549
updateMachine.debugPauseLoop();
530550
check(globalStore.getAccount(account.id)).isNotNull()
531551
..identicalTo(updateMachine.store.account)
552+
..realmName.equals('Organization B')
553+
..realmIcon.equals(Uri.parse('/image-b.png'))
532554
..zulipVersion.equals('8.0+g9876')
533555
..zulipMergeBase.equals('8.0')
534556
..zulipFeatureLevel.equals(234);

0 commit comments

Comments
 (0)