Skip to content

Commit a8b236d

Browse files
rajveermalviyagnprice
authored andcommitted
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 c29934a commit a8b236d

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-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

@@ -1108,6 +1130,11 @@ class UpdateMachine {
11081130
connection.zulipFeatureLevel = zulipVersionData.zulipFeatureLevel;
11091131
}
11101132

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+
11111138
final store = PerAccountStore.fromInitialSnapshot(
11121139
globalStore: globalStore,
11131140
accountId: accountId,

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: 24 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,21 @@ 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))
388+
..identicalTo(updated)
389+
..equals(selfAccount.copyWith(
390+
realmName: Value('Organization B'),
391+
realmIcon: Value(Uri.parse('/image-b.png'))));
392+
});
393+
379394
group('GlobalStore.removeAccount', () {
380395
void checkGlobalStore(GlobalStore store, int accountId, {
381396
required bool expectAccount,
@@ -509,18 +524,24 @@ void main() {
509524

510525
test('updates account from snapshot', () => awaitFakeAsync((async) async {
511526
final account = eg.account(user: eg.selfUser,
527+
realmName: 'Organization A',
528+
realmIcon: Uri.parse('/image-a.png'),
512529
zulipVersion: '6.0+gabcd',
513530
zulipMergeBase: '6.0',
514531
zulipFeatureLevel: 123,
515532
);
516533
await prepareStore(account: account);
517534
check(globalStore.getAccount(account.id)).isNotNull()
535+
..realmName.equals('Organization A')
536+
..realmIcon.equals(Uri.parse('/image-a.png'))
518537
..zulipVersion.equals('6.0+gabcd')
519538
..zulipMergeBase.equals('6.0')
520539
..zulipFeatureLevel.equals(123);
521540

522541
globalStore.useCachedApiConnections = true;
523542
connection.prepare(json: eg.initialSnapshot(
543+
realmName: 'Organization B',
544+
realmIconUrl: Uri.parse('/image-b.png'),
524545
zulipVersion: '8.0+g9876',
525546
zulipMergeBase: '8.0',
526547
zulipFeatureLevel: 234,
@@ -529,6 +550,8 @@ void main() {
529550
updateMachine.debugPauseLoop();
530551
check(globalStore.getAccount(account.id)).isNotNull()
531552
..identicalTo(updateMachine.store.account)
553+
..realmName.equals('Organization B')
554+
..realmIcon.equals(Uri.parse('/image-b.png'))
532555
..zulipVersion.equals('8.0+g9876')
533556
..zulipMergeBase.equals('8.0')
534557
..zulipFeatureLevel.equals(234);

0 commit comments

Comments
 (0)