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

Separate steps in back up process #647

Merged
merged 9 commits into from
Sep 21, 2023
8 changes: 8 additions & 0 deletions assets/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,12 @@
"type": "text",
"placeholders": {}
},
"cannotEnableKeyBackup": "Cannot enable Chat Backup. Please Go to Settings to try it again.",
"@cannotEnableKeyBackup": {
"type": "text",
"placeholders": {}
},
"cannotUploadKey": "Cannot store Key Backup.",
"oopsPushError": "Oops! Unfortunately, an error occurred when setting up the push notifications.",
"@oopsPushError": {
"type": "text",
Expand Down Expand Up @@ -2443,6 +2449,8 @@
"@enableAutoBackups": {},
"unlockOldMessages": "Unlock old messages",
"@unlockOldMessages": {},
"cannotUnlockBackupKey": "Cannot unlock Key backup.",
"@cannotUnlockBackupKey": {},
"storeInSecureStorageDescription": "Store the recovery key in the secure storage of this device.",
"@storeInSecureStorageDescription": {},
"saveKeyManuallyDescription": "Save this key manually by triggering the system share dialog or clipboard.",
Expand Down
2 changes: 2 additions & 0 deletions lib/data/datasource/recovery_words_data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ abstract class RecoveryWordsDataSource {
Future<RecoveryWords> getRecoveryWords();

Future<bool> saveRecoveryWords(String recoveryWords);

Future<bool> deleteRecoveryWords();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ class RecoveryWordsDataSourceImpl implements RecoveryWordsDataSource {
Future<bool> saveRecoveryWords(String recoveryWords) async {
return _recoveryWordsAPI.saveRecoveryWords(recoveryWords);
}

@override
Future<bool> deleteRecoveryWords() {
return _recoveryWordsAPI.deleteRecoveryWords();
}
}
2 changes: 1 addition & 1 deletion lib/data/network/contact/tom_contact_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class TomContactAPI {
);

final response = await _client
.post(
.postToGetBody(
IdentityEndpoint.matchUserIdServicePath
.generateTwakeIdentityEndpoint(),
data: requestBody.toJson(),
Expand Down
25 changes: 23 additions & 2 deletions lib/data/network/dio_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DioClient {
.catchError((error) => throw error);
}

Future<dynamic> post(
Future<dynamic> postToGetBody(
String path, {
data,
Map<String, dynamic>? queryParameters,
Expand All @@ -49,6 +49,28 @@ class DioClient {
.catchError((error) => throw error);
}

Future<dynamic> post(
String path, {
data,
Map<String, dynamic>? queryParameters,
Options? options,
CancelToken? cancelToken,
ProgressCallback? onSendProgress,
ProgressCallback? onReceiveProgress,
}) async {
return await _dio
.post(
path,
data: data,
queryParameters: queryParameters,
options: options,
cancelToken: cancelToken,
onSendProgress: onSendProgress,
onReceiveProgress: onReceiveProgress,
)
.catchError((error) => throw error);
}

Future<dynamic> delete(
String path, {
data,
Expand All @@ -64,7 +86,6 @@ class DioClient {
options: options,
cancelToken: cancelToken,
)
.then((value) => value.data)
.catchError((error) => throw (error));
}

Expand Down
2 changes: 1 addition & 1 deletion lib/data/network/media/media_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MediaAPI {
await File(fileInfo.filePath).length();
dioHeaders[HttpHeaders.contentTypeHeader] = fileInfo.mimeType;
final response = await _client
.post(
.postToGetBody(
HomeserverEndpoint.uploadMediaServicePath
.generateHomeserverMediaEndpoint(),
data: fileInfo.readStream ?? File(fileInfo.filePath).openRead(),
Expand Down
18 changes: 18 additions & 0 deletions lib/data/network/recovery_words/recovery_words_api.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:dio/dio.dart';
import 'package:fluffychat/data/model/recovery_words_json.dart';
import 'package:fluffychat/data/network/dio_client.dart';
import 'package:fluffychat/data/network/tom_endpoint.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/di/global/network_di.dart';
import 'package:matrix/matrix.dart';

class RecoveryWordsAPI {
final DioClient _client =
Expand All @@ -26,4 +28,20 @@ class RecoveryWordsAPI {
.onError((error, stackTrace) => throw Exception(error));
return response.statusCode >= 200 && response.statusCode <= 299;
}

Future<bool> deleteRecoveryWords() async {
final options = Options(validateStatus: _deleteRecoverySuccess);
final response = await _client
.delete(TomEndpoint.recoveryWordsServicePath.path, options: options)
.onError((error, stackTrace) {
Logs().e('RecoveryWordsAPI::deleteRecoveryWords() [Exception]', error);
throw Exception(error);
});
return _deleteRecoverySuccess(response?.statusCode);
}

bool _deleteRecoverySuccess(int? statusCode) {
if (statusCode == null) return false;
return statusCode >= 200 && statusCode <= 299 || statusCode == 404;
}
}
5 changes: 5 additions & 0 deletions lib/data/repository/recovery_words_repository_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ class RecoveryWordsRepositoryImpl implements RecoveryWordsRepository {
Future<bool> saveRecoveryWords(String recoveryWords) async {
return await recoveryWordsDataSource.saveRecoveryWords(recoveryWords);
}

@override
Future<bool> deleteRecoveryWords() async {
return await recoveryWordsDataSource.deleteRecoveryWords();
}
}
9 changes: 7 additions & 2 deletions lib/di/global/get_it_initializer.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:collection';

import 'package:fluffychat/data/datasource/media/media_data_source.dart';
import 'package:fluffychat/data/datasource/recovery_words_data_source.dart';
import 'package:fluffychat/data/datasource/tom_configurations_datasource.dart';
Expand All @@ -25,6 +26,9 @@ import 'package:fluffychat/domain/usecase/download_file_for_preview_interactor.d
import 'package:fluffychat/domain/usecase/forward/forward_message_interactor.dart';
import 'package:fluffychat/domain/usecase/get_contacts_interactor.dart';
import 'package:fluffychat/domain/usecase/preview_url/get_preview_url_interactor.dart';
import 'package:fluffychat/domain/usecase/recovery/delete_recovery_words_interactor.dart';
import 'package:fluffychat/domain/usecase/recovery/get_recovery_words_interactor.dart';
import 'package:fluffychat/domain/usecase/recovery/save_recovery_words_interactor.dart';
import 'package:fluffychat/domain/usecase/room/chat_room_search_interactor.dart';
import 'package:fluffychat/domain/usecase/room/create_new_group_chat_interactor.dart';
import 'package:fluffychat/domain/usecase/room/upload_content_interactor.dart';
Expand All @@ -34,8 +38,6 @@ import 'package:fluffychat/domain/usecase/send_file_interactor.dart';
import 'package:fluffychat/domain/usecase/send_file_on_web_interactor.dart';
import 'package:fluffychat/domain/usecase/send_image_interactor.dart';
import 'package:fluffychat/domain/usecase/send_images_interactor.dart';
import 'package:fluffychat/domain/usecases/get_recovery_words_interactor.dart';
import 'package:fluffychat/domain/usecases/save_recovery_words_interactor.dart';
import 'package:fluffychat/utils/responsive/responsive_utils.dart';
import 'package:get_it/get_it.dart';

Expand Down Expand Up @@ -122,6 +124,9 @@ class GetItInitializer {
getIt.registerLazySingleton<SaveRecoveryWordsInteractor>(
() => SaveRecoveryWordsInteractor(),
);
getIt.registerLazySingleton<DeleteRecoveryWordsInteractor>(
() => DeleteRecoveryWordsInteractor(),
);
getIt.registerFactory<GetContactsInteractor>(
() => GetContactsInteractor(),
);
Expand Down
18 changes: 18 additions & 0 deletions lib/domain/app_state/recovery_words/delete_recovery_states.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/success.dart';

class DeleteRecoveryWordsFailed extends Failure {
final dynamic exception;

const DeleteRecoveryWordsFailed({this.exception});

@override
List<Object?> get props => [exception];
}

class DeleteRecoveryWordsSuccess extends Success {
const DeleteRecoveryWordsSuccess();

@override
List<Object?> get props => [];
}
2 changes: 2 additions & 0 deletions lib/domain/repository/recovery_words_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ abstract class RecoveryWordsRepository {
Future<RecoveryWords> getRecoveryWords();

Future<bool> saveRecoveryWords(String recoveryWords);

Future<bool> deleteRecoveryWords();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:dartz/dartz.dart';
import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/success.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/domain/app_state/recovery_words/delete_recovery_states.dart';
import 'package:fluffychat/domain/repository/recovery_words_repository.dart';
import 'package:matrix/matrix.dart';

class DeleteRecoveryWordsInteractor {
final RecoveryWordsRepository recoveryWordsRepository =
getIt.get<RecoveryWordsRepository>();

Future<Either<Failure, Success>> execute() async {
try {
final bool response = await recoveryWordsRepository.deleteRecoveryWords();
return response
? const Right(DeleteRecoveryWordsSuccess())
: const Left(DeleteRecoveryWordsFailed());
} catch (e) {
Logs().e('DeleteRecoveryWordsInteractor::execute() [Exception]', e);
return Left(DeleteRecoveryWordsFailed(exception: e));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/domain/app_state/recovery_words/save_recovery_words_failed.dart';
import 'package:fluffychat/domain/app_state/recovery_words/save_recovery_words_success.dart';
import 'package:fluffychat/domain/repository/recovery_words_repository.dart';
import 'package:matrix/matrix.dart';

class SaveRecoveryWordsInteractor {
final RecoveryWordsRepository recoveryWordsRepository =
Expand All @@ -18,6 +19,7 @@ class SaveRecoveryWordsInteractor {
? const Right(SaveRecoveryWordsSuccess())
: const Left(SaveRecoveryWordsFailed());
} catch (e) {
Logs().e('SaveRecoveryWordsInteractor::execute() [Exception]', e);
return Left(SaveRecoveryWordsFailed(exception: e));
}
}
Expand Down
12 changes: 11 additions & 1 deletion lib/pages/bootstrap/bootstrap_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:fluffychat/config/themes.dart';
import 'package:fluffychat/pages/bootstrap/tom_bootstrap_dialog.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/widgets/adaptive_flat_button.dart';
import 'package:flutter/cupertino.dart';
Expand Down Expand Up @@ -374,7 +375,16 @@ class BootstrapDialogState extends State<BootstrapDialog> {
cancelLabel: L10n.of(context)!.cancel,
isDestructiveAction: true,
)) {
setState(() => _createBootstrap(true));
await TomBootstrapDialog(
wipe: true,
wipeRecovery: true,
client: widget.client,
).show(context).then(
(value) => Navigator.of(
context,
rootNavigator: false,
).pop<bool>(false),
);
}
},
)
Expand Down
Loading