Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TW-1785: Improve logout for multiple homeserver #1788

Merged
merged 5 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/data/datasource/tom_configurations_datasource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ abstract class ToMConfigurationsDatasource {
String userId,
ToMConfigurations toMConfigurations,
);

Future<void> deleteTomConfigurations(String userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ class HiveToMConfigurationDatasource implements ToMConfigurationsDatasource {
.toJson(),
);
}

@override
Future<void> deleteTomConfigurations(String userId) {
final hiveCollectionToMDatabase = getIt.get<HiveCollectionToMDatabase>();
return hiveCollectionToMDatabase.tomConfigurationsBox.delete(userId);
}
}
5 changes: 5 additions & 0 deletions lib/data/repository/tom_configurations_repository_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ class ToMConfigurationsRepositoryImpl implements ToMConfigurationsRepository {
toMConfigurations,
);
}

@override
Future<void> deleteTomConfigurations(String userId) {
return tomConfigurationsDatasource.deleteTomConfigurations(userId);
}
}
2 changes: 2 additions & 0 deletions lib/domain/repository/tom_configurations_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ abstract class ToMConfigurationsRepository {
String userId,
ToMConfigurations toMConfigurations,
);

Future<void> deleteTomConfigurations(String userId);
}
69 changes: 60 additions & 9 deletions lib/pages/settings_dashboard/settings/settings.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:async';

import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/data/hive/hive_collection_tom_database.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/domain/repository/tom_configurations_repository.dart';
import 'package:fluffychat/event/twake_inapp_event_types.dart';
import 'package:fluffychat/pages/bootstrap/bootstrap_dialog.dart';
import 'package:fluffychat/presentation/mixins/connect_page_mixin.dart';
Expand Down Expand Up @@ -39,6 +39,8 @@ class SettingsController extends State<Settings> with ConnectPageMixin {
final ValueNotifier<Uri?> avatarUriNotifier = ValueNotifier(Uri());
final ValueNotifier<String?> displayNameNotifier = ValueNotifier('');

final tomConfigurationRepository = getIt.get<ToMConfigurationsRepository>();

StreamSubscription? onAccountDataSubscription;

final List<SettingEnum> getListSettingItem = [
Expand Down Expand Up @@ -86,26 +88,56 @@ class SettingsController extends State<Settings> with ConnectPageMixin {
OkCancelResult.cancel) {
return;
}
final matrix = Matrix.of(context);
if (PlatformInfos.isMobile) {
await tryLogoutSso(context);
await _logoutActionsOnMobile();
} else {
await _logoutActions();
}
if (matrix.twakeSupported == true) {
final hiveCollectionToMDatabase = getIt.get<HiveCollectionToMDatabase>();
await hiveCollectionToMDatabase.clear();
}

Future<void> _logoutActionsOnMobile() async {
try {
await tryLogoutSso(context);
} catch (e) {
Logs().e('SettingsController()::_logoutActionsOnMobile - error: $e');
return;
hoangdat marked this conversation as resolved.
Show resolved Hide resolved
}
await TwakeDialog.showFutureLoadingDialogFullScreen(
future: () async {
try {
if (matrix.backgroundPush != null) {
await matrix.backgroundPush!.removeCurrentPusher();
}
await matrix.client.logout();
await Future.wait([
matrix.client.logout(),
_deleteTomConfigurations(matrix.client),
]);
} catch (e) {
Logs().e('SettingsController()::_logoutActionsOnMobile - error: $e');
}
},
);
}

Future<void> _logoutActions() async {
await TwakeDialog.showFutureLoadingDialogFullScreen(
future: () async {
try {
if (matrix.backgroundPush != null) {
await matrix.backgroundPush!.removeCurrentPusher();
}
await Future.wait([
matrix.client.logout(),
_deleteTomConfigurations(matrix.client),
]);
} catch (e) {
Logs().e('SettingsController()::logoutAction - error: $e');
Logs().e('SettingsController()::_logoutActions - error: $e');
} finally {
if (PlatformInfos.isWeb) {
try {
await tryLogoutSso(context);
} catch (e) {
Logs().e('SettingsController()::_logoutActions - error: $e');
return;
}
}
},
Expand Down Expand Up @@ -244,6 +276,25 @@ class SettingsController extends State<Settings> with ConnectPageMixin {
});
}

Future<void> _deleteTomConfigurations(Client currentClient) async {
try {
Logs().d(
'SettingsController::_deleteTomConfigurations - Client ID: ${currentClient.userID}',
);
if (matrix.twakeSupported) {
await tomConfigurationRepository
.deleteTomConfigurations(currentClient.userID!);
}
Logs().d(
'SettingsController::_deleteTomConfigurations - Success',
);
} catch (e) {
Logs().e(
'SettingsController::_deleteTomConfigurations - error: $e',
);
}
}

@override
void initState() {
_getCurrentProfile(client);
Expand Down
1 change: 1 addition & 0 deletions lib/presentation/mixins/connect_page_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ mixin ConnectPageMixin {
Logs().d('tryLogoutSso::result: $result');
} catch (e) {
Logs().d('tryLogoutSso::error: $e');
rethrow;
}
}

Expand Down
90 changes: 61 additions & 29 deletions lib/widgets/matrix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class MatrixState extends State<Matrix>
String? activeBundle;
Store store = Store();
HomeserverSummary? loginHomeserverSummary;
String? authUrl;
String? _authUrl;
XFile? loginAvatar;
String? loginUsername;
LoginType? loginType;
Expand All @@ -96,6 +96,8 @@ class MatrixState extends State<Matrix>

BackgroundPush? backgroundPush;

String? get authUrl => _authUrl;

Client get client {
if (widget.clients.isEmpty) {
widget.clients.add(getLoginClient());
Expand Down Expand Up @@ -125,7 +127,7 @@ class MatrixState extends State<Matrix>
_activeClient = index;
// TODO: Multi-client VoiP support
createVoipPlugin();
_setUpToMServicesWhenChangingActiveClient(newClient);
await _setUpToMServicesWhenChangingActiveClient(newClient);
await _storePersistActiveAccount(newClient!);
return SetActiveClientState.success;
} else {
Expand Down Expand Up @@ -375,13 +377,8 @@ class MatrixState extends State<Matrix>
Logs().v('[MATRIX]:_listenLoginStateChanged:: First Log in successful');
_handleFirstLoggedIn(client);
} else {
Logs().v('[MATRIX]:_listenLoginStateChanged:: Log out successful');
if (PlatformInfos.isMobile) {
_deletePersistActiveAccount(state);
TwakeApp.router.go('/home/twakeWelcome');
} else {
TwakeApp.router.go('/home', extra: true);
}
Logs().v('[MATRIX]:_listenLoginStateChanged:: Last Log out successful');
await _handleLastLogout();
}
}
}
Expand All @@ -392,7 +389,7 @@ class MatrixState extends State<Matrix>
) async {
await _cancelSubs(currentClient.clientName);
widget.clients.remove(currentClient);
ClientManager.removeClientNameFromStore(currentClient.clientName);
await ClientManager.removeClientNameFromStore(currentClient.clientName);
TwakeSnackBar.show(
TwakeApp.routerKey.currentContext!,
L10n.of(context)!.oneClientLoggedOut,
Expand All @@ -411,9 +408,9 @@ class MatrixState extends State<Matrix>
}
}

void _handleFirstLoggedIn(Client newActiveClient) {
setUpToMServicesInLogin(newActiveClient);
_storePersistActiveAccount(newActiveClient);
Future<void> _handleFirstLoggedIn(Client newActiveClient) async {
await setUpToMServicesInLogin(newActiveClient);
await _storePersistActiveAccount(newActiveClient);
TwakeApp.router.go(
'/rooms',
extra: LoggedInBodyArgs(
Expand All @@ -439,7 +436,7 @@ class MatrixState extends State<Matrix>
_loginClientCandidate!.clientName,
);
if (activeClient == null) return;
setUpToMServicesInLogin(activeClient);
await setUpToMServicesInLogin(activeClient);
final result = await setActiveClient(activeClient);
if (result.isSuccess) {
TwakeApp.router.go(
Expand All @@ -452,7 +449,7 @@ class MatrixState extends State<Matrix>
}
}

void _deletePersistActiveAccount(LoginState state) async {
Future<void> _deletePersistActiveAccount() async {
try {
final multipleAccountRepository = getIt.get<MultipleAccountRepository>();
await multipleAccountRepository.deletePersistActiveAccount();
Expand Down Expand Up @@ -566,7 +563,7 @@ class MatrixState extends State<Matrix>
toMConfigurations.tomServerInformation,
toMConfigurations.identityServerInformation,
);
authUrl = toMConfigurations.authUrl;
_setupAuthUrl(url: toMConfigurations.authUrl);
loginType = toMConfigurations.loginType;
} catch (e) {
Logs().e('MatrixState::_retrieveToMConfiguration: $e');
Expand All @@ -588,7 +585,7 @@ class MatrixState extends State<Matrix>
}
}

void setUpToMServicesInLogin(Client client) {
Future<void> setUpToMServicesInLogin(Client client) async {
final tomServer = loginHomeserverSummary?.tomServer;
Logs().d('MatrixState::setUpToMServicesInLogin: $tomServer');
if (tomServer != null) {
Expand All @@ -598,17 +595,15 @@ class MatrixState extends State<Matrix>
loginHomeserverSummary?.discoveryInformation?.mIdentityServer;
final homeServer =
loginHomeserverSummary?.discoveryInformation?.mHomeserver;
final newAuthUrl = loginHomeserverSummary?.discoveryInformation
?.additionalProperties["m.authentication"]?["issuer"];
authUrl = newAuthUrl is String ? newAuthUrl : null;
_setupAuthUrl();
if (identityServer != null) {
_setUpIdentityServer(identityServer);
}
if (homeServer != null) {
_setUpHomeServer(homeServer.baseUrl);
}
if (tomServer != null) {
_storeToMConfiguration(
await _storeToMConfiguration(
client,
ToMConfigurations(
tomServerInformation: tomServer,
Expand Down Expand Up @@ -657,10 +652,10 @@ class MatrixState extends State<Matrix>
.changeBaseUrl(identityServer.baseUrl.toString());
}

void _storeToMConfiguration(
Future<void> _storeToMConfiguration(
Client client,
ToMConfigurations config,
) {
) async {
try {
Logs().e(
'Matrix::_storeToMConfiguration: clientName - ${client.clientName}',
Expand All @@ -671,7 +666,7 @@ class MatrixState extends State<Matrix>
if (client.userID == null) return;
final ToMConfigurationsRepository configurationRepository =
getIt.get<ToMConfigurationsRepository>();
configurationRepository.saveTomConfigurations(
await configurationRepository.saveTomConfigurations(
client.userID!,
config,
);
Expand All @@ -683,7 +678,7 @@ class MatrixState extends State<Matrix>
}
}

void _setUpToMServicesWhenChangingActiveClient(Client? client) async {
Future<void> _setUpToMServicesWhenChangingActiveClient(Client? client) async {
Logs().d(
'Matrix::_checkHomeserverExists: Old twakeSupported - $twakeSupported',
);
Expand All @@ -697,6 +692,7 @@ class MatrixState extends State<Matrix>
_setUpToMServer(null);
} else {
_setUpToMServer(toMConfigurations.tomServerInformation);
_setupAuthUrl(url: toMConfigurations.authUrl);
}
} catch (e) {
_setUpToMServer(null);
Expand All @@ -713,12 +709,12 @@ class MatrixState extends State<Matrix>
final persistActiveAccount =
await multipleAccountRepository.getPersistActiveAccount();
if (persistActiveAccount == null) {
_storePersistActiveAccount(client);
await _storePersistActiveAccount(client);
return;
} else {
final newActiveClient = getClientByUserId(persistActiveAccount);
if (newActiveClient != null) {
setActiveClient(newActiveClient);
await setActiveClient(newActiveClient);
}
}
} catch (e) {
Expand All @@ -731,10 +727,10 @@ class MatrixState extends State<Matrix>
Future<void> _storePersistActiveAccount(Client newClient) async {
if (newClient.userID == null) return;
try {
Logs().e(
Logs().d(
'Matrix::_storePersistActiveAccount: clientName - ${newClient.clientName}',
);
Logs().e(
Logs().d(
'Matrix::_storePersistActiveAccount: userId - ${newClient.userID}',
);
final MultipleAccountRepository multipleAccountRepository =
Expand All @@ -757,6 +753,42 @@ class MatrixState extends State<Matrix>
didChangeAppLifecycleState(AppLifecycleState.paused);
}

Future<void> _setupAuthUrl({
String? url,
}) async {
if (url != null) {
Logs().e(
'Matrix::_setupAuthUrl: newAuthUrl - $url',
);
_authUrl = url;
} else {
final newAuthUrl = loginHomeserverSummary?.discoveryInformation
?.additionalProperties["m.authentication"]?["issuer"];
Logs().e(
'Matrix::_setupAuthUrl: newAuthUrl - $newAuthUrl',
);
_authUrl = newAuthUrl is String ? newAuthUrl : url;
}
}

Future<void> _deleteAllTomConfigurations() async {
final hiveCollectionToMDatabase = getIt.get<HiveCollectionToMDatabase>();
await hiveCollectionToMDatabase.clear();
Logs().d(
'MatrixState::_deleteAllTomConfigurations: Delete ToM database success',
);
}

Future<void> _handleLastLogout() async {
if (PlatformInfos.isMobile) {
await _deletePersistActiveAccount();
TwakeApp.router.go('/home/twakeWelcome');
} else {
TwakeApp.router.go('/home', extra: true);
}
await _deleteAllTomConfigurations();
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
Logs().i('didChangeAppLifecycleState: AppLifecycleState = $state');
Expand Down
Loading