From 75b8a7b9ac1005df38f7886ddf1f837ebcab5ce4 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Mon, 7 Aug 2023 09:23:48 -0300 Subject: [PATCH] feat(backups): Implement restore backup strategy --- .../graphql_maps/schema/backups.graphql | 4 +- .../graphql_maps/schema/backups.graphql.dart | 61 ++++++++++++++++--- .../graphql_maps/schema/schema.graphql | 8 ++- .../graphql_maps/schema/schema.graphql.dart | 24 ++++++++ .../graphql_maps/server_api/backups_api.dart | 7 ++- lib/logic/cubit/backups/backups_cubit.dart | 7 ++- lib/logic/models/backup.dart | 24 ++++++++ lib/ui/pages/backups/backup_details.dart | 7 ++- lib/ui/pages/backups/backups_list.dart | 5 +- 9 files changed, 129 insertions(+), 18 deletions(-) diff --git a/lib/logic/api_maps/graphql_maps/schema/backups.graphql b/lib/logic/api_maps/graphql_maps/schema/backups.graphql index fae80432..432678d0 100644 --- a/lib/logic/api_maps/graphql_maps/schema/backups.graphql +++ b/lib/logic/api_maps/graphql_maps/schema/backups.graphql @@ -81,9 +81,9 @@ mutation InitializeRepository($repository: InitializeRepositoryInput!) { } } -mutation RestoreBackup($snapshotId: String!) { +mutation RestoreBackup($snapshotId: String!, $strategy: RestoreStrategy! = DOWNLOAD_VERIFY_OVERWRITE) { backup { - restoreBackup(snapshotId: $snapshotId) { + restoreBackup(snapshotId: $snapshotId, strategy: $strategy) { ...basicMutationReturnFields job { ...basicApiJobsFields diff --git a/lib/logic/api_maps/graphql_maps/schema/backups.graphql.dart b/lib/logic/api_maps/graphql_maps/schema/backups.graphql.dart index 428da3e4..72399c2c 100644 --- a/lib/logic/api_maps/graphql_maps/schema/backups.graphql.dart +++ b/lib/logic/api_maps/graphql_maps/schema/backups.graphql.dart @@ -4982,9 +4982,13 @@ class _CopyWithStubImpl$Mutation$InitializeRepository$backup } class Variables$Mutation$RestoreBackup { - factory Variables$Mutation$RestoreBackup({required String snapshotId}) => + factory Variables$Mutation$RestoreBackup({ + required String snapshotId, + required Enum$RestoreStrategy strategy, + }) => Variables$Mutation$RestoreBackup._({ r'snapshotId': snapshotId, + r'strategy': strategy, }); Variables$Mutation$RestoreBackup._(this._$data); @@ -4993,16 +4997,23 @@ class Variables$Mutation$RestoreBackup { final result$data = {}; final l$snapshotId = data['snapshotId']; result$data['snapshotId'] = (l$snapshotId as String); + final l$strategy = data['strategy']; + result$data['strategy'] = + fromJson$Enum$RestoreStrategy((l$strategy as String)); return Variables$Mutation$RestoreBackup._(result$data); } Map _$data; String get snapshotId => (_$data['snapshotId'] as String); + Enum$RestoreStrategy get strategy => + (_$data['strategy'] as Enum$RestoreStrategy); Map toJson() { final result$data = {}; final l$snapshotId = snapshotId; result$data['snapshotId'] = l$snapshotId; + final l$strategy = strategy; + result$data['strategy'] = toJson$Enum$RestoreStrategy(l$strategy); return result$data; } @@ -5025,13 +5036,22 @@ class Variables$Mutation$RestoreBackup { if (l$snapshotId != lOther$snapshotId) { return false; } + final l$strategy = strategy; + final lOther$strategy = other.strategy; + if (l$strategy != lOther$strategy) { + return false; + } return true; } @override int get hashCode { final l$snapshotId = snapshotId; - return Object.hashAll([l$snapshotId]); + final l$strategy = strategy; + return Object.hashAll([ + l$snapshotId, + l$strategy, + ]); } } @@ -5044,7 +5064,10 @@ abstract class CopyWith$Variables$Mutation$RestoreBackup { factory CopyWith$Variables$Mutation$RestoreBackup.stub(TRes res) = _CopyWithStubImpl$Variables$Mutation$RestoreBackup; - TRes call({String? snapshotId}); + TRes call({ + String? snapshotId, + Enum$RestoreStrategy? strategy, + }); } class _CopyWithImpl$Variables$Mutation$RestoreBackup @@ -5060,11 +5083,16 @@ class _CopyWithImpl$Variables$Mutation$RestoreBackup static const _undefined = {}; - TRes call({Object? snapshotId = _undefined}) => + TRes call({ + Object? snapshotId = _undefined, + Object? strategy = _undefined, + }) => _then(Variables$Mutation$RestoreBackup._({ ..._instance._$data, if (snapshotId != _undefined && snapshotId != null) 'snapshotId': (snapshotId as String), + if (strategy != _undefined && strategy != null) + 'strategy': (strategy as Enum$RestoreStrategy), })); } @@ -5074,7 +5102,11 @@ class _CopyWithStubImpl$Variables$Mutation$RestoreBackup TRes _res; - call({String? snapshotId}) => _res; + call({ + String? snapshotId, + Enum$RestoreStrategy? strategy, + }) => + _res; } class Mutation$RestoreBackup { @@ -5223,7 +5255,18 @@ const documentNodeMutationRestoreBackup = DocumentNode(definitions: [ ), defaultValue: DefaultValueNode(value: null), directives: [], - ) + ), + VariableDefinitionNode( + variable: VariableNode(name: NameNode(value: 'strategy')), + type: NamedTypeNode( + name: NameNode(value: 'RestoreStrategy'), + isNonNull: true, + ), + defaultValue: DefaultValueNode( + value: EnumValueNode( + name: NameNode(value: 'DOWNLOAD_VERIFY_OVERWRITE'))), + directives: [], + ), ], directives: [], selectionSet: SelectionSetNode(selections: [ @@ -5240,7 +5283,11 @@ const documentNodeMutationRestoreBackup = DocumentNode(definitions: [ ArgumentNode( name: NameNode(value: 'snapshotId'), value: VariableNode(name: NameNode(value: 'snapshotId')), - ) + ), + ArgumentNode( + name: NameNode(value: 'strategy'), + value: VariableNode(name: NameNode(value: 'strategy')), + ), ], directives: [], selectionSet: SelectionSetNode(selections: [ diff --git a/lib/logic/api_maps/graphql_maps/schema/schema.graphql b/lib/logic/api_maps/graphql_maps/schema/schema.graphql index 1f57a51a..368c2b90 100644 --- a/lib/logic/api_maps/graphql_maps/schema/schema.graphql +++ b/lib/logic/api_maps/graphql_maps/schema/schema.graphql @@ -94,7 +94,8 @@ type BackupMutations { removeRepository: GenericBackupConfigReturn! setAutobackupPeriod(period: Int = null): GenericBackupConfigReturn! startBackup(serviceId: String!): GenericJobMutationReturn! - restoreBackup(snapshotId: String!): GenericJobMutationReturn! + restoreBackup(snapshotId: String!, strategy: RestoreStrategy! = DOWNLOAD_VERIFY_OVERWRITE): GenericJobMutationReturn! + forgetSnapshot(snapshotId: String!): GenericMutationReturn! forceSnapshotsReload: GenericMutationReturn! } @@ -241,6 +242,11 @@ input RecoveryKeyLimitsInput { uses: Int = null } +enum RestoreStrategy { + INPLACE + DOWNLOAD_VERIFY_OVERWRITE +} + enum ServerProvider { HETZNER DIGITALOCEAN diff --git a/lib/logic/api_maps/graphql_maps/schema/schema.graphql.dart b/lib/logic/api_maps/graphql_maps/schema/schema.graphql.dart index e92ecc5e..8a78a6f8 100644 --- a/lib/logic/api_maps/graphql_maps/schema/schema.graphql.dart +++ b/lib/logic/api_maps/graphql_maps/schema/schema.graphql.dart @@ -1338,6 +1338,30 @@ Enum$DnsProvider fromJson$Enum$DnsProvider(String value) { } } +enum Enum$RestoreStrategy { INPLACE, DOWNLOAD_VERIFY_OVERWRITE, $unknown } + +String toJson$Enum$RestoreStrategy(Enum$RestoreStrategy e) { + switch (e) { + case Enum$RestoreStrategy.INPLACE: + return r'INPLACE'; + case Enum$RestoreStrategy.DOWNLOAD_VERIFY_OVERWRITE: + return r'DOWNLOAD_VERIFY_OVERWRITE'; + case Enum$RestoreStrategy.$unknown: + return r'$unknown'; + } +} + +Enum$RestoreStrategy fromJson$Enum$RestoreStrategy(String value) { + switch (value) { + case r'INPLACE': + return Enum$RestoreStrategy.INPLACE; + case r'DOWNLOAD_VERIFY_OVERWRITE': + return Enum$RestoreStrategy.DOWNLOAD_VERIFY_OVERWRITE; + default: + return Enum$RestoreStrategy.$unknown; + } +} + enum Enum$ServerProvider { HETZNER, DIGITALOCEAN, $unknown } String toJson$Enum$ServerProvider(Enum$ServerProvider e) { diff --git a/lib/logic/api_maps/graphql_maps/server_api/backups_api.dart b/lib/logic/api_maps/graphql_maps/server_api/backups_api.dart index b652db03..3d629cb7 100644 --- a/lib/logic/api_maps/graphql_maps/server_api/backups_api.dart +++ b/lib/logic/api_maps/graphql_maps/server_api/backups_api.dart @@ -209,14 +209,17 @@ mixin BackupsApi on GraphQLApiMap { Future> restoreBackup( final String snapshotId, + final BackupRestoreStrategy strategy, ) async { QueryResult response; GenericResult? result; try { final GraphQLClient client = await getClient(); - final variables = - Variables$Mutation$RestoreBackup(snapshotId: snapshotId); + final variables = Variables$Mutation$RestoreBackup( + snapshotId: snapshotId, + strategy: strategy.toGraphQL, + ); final options = Options$Mutation$RestoreBackup(variables: variables); response = await client.mutate$RestoreBackup(options); if (response.hasException) { diff --git a/lib/logic/cubit/backups/backups_cubit.dart b/lib/logic/cubit/backups/backups_cubit.dart index ef3cec4d..300a898b 100644 --- a/lib/logic/cubit/backups/backups_cubit.dart +++ b/lib/logic/cubit/backups/backups_cubit.dart @@ -185,9 +185,12 @@ class BackupsCubit extends ServerInstallationDependendCubit { emit(state.copyWith(preventActions: false)); } - Future restoreBackup(final String backupId) async { + Future restoreBackup( + final String backupId, + final BackupRestoreStrategy strategy, + ) async { emit(state.copyWith(preventActions: true)); - await api.restoreBackup(backupId); + await api.restoreBackup(backupId, strategy); emit(state.copyWith(preventActions: false)); } diff --git a/lib/logic/models/backup.dart b/lib/logic/models/backup.dart index fda68375..2199e223 100644 --- a/lib/logic/models/backup.dart +++ b/lib/logic/models/backup.dart @@ -1,5 +1,6 @@ import 'package:json_annotation/json_annotation.dart'; import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/backups.graphql.dart'; +import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/schema.graphql.dart'; import 'package:selfprivacy/logic/models/hive/backups_credential.dart'; class Backup { @@ -58,3 +59,26 @@ class BackupConfiguration { final String? locationName; final BackupsProviderType provider; } + +enum BackupRestoreStrategy { + inplace, + downloadVerifyOverwrite, + unknown; + + factory BackupRestoreStrategy.fromGraphQL( + final Enum$RestoreStrategy strategy, + ) => + switch (strategy) { + Enum$RestoreStrategy.INPLACE => inplace, + Enum$RestoreStrategy.DOWNLOAD_VERIFY_OVERWRITE => + downloadVerifyOverwrite, + Enum$RestoreStrategy.$unknown => unknown, + }; + + Enum$RestoreStrategy get toGraphQL => switch (this) { + inplace => Enum$RestoreStrategy.INPLACE, + downloadVerifyOverwrite => + Enum$RestoreStrategy.DOWNLOAD_VERIFY_OVERWRITE, + unknown => Enum$RestoreStrategy.$unknown, + }; +} diff --git a/lib/ui/pages/backups/backup_details.dart b/lib/ui/pages/backups/backup_details.dart index 44995577..00ca8df2 100644 --- a/lib/ui/pages/backups/backup_details.dart +++ b/lib/ui/pages/backups/backup_details.dart @@ -240,9 +240,10 @@ class BackupDetailsPage extends StatelessWidget { ), actionButtonTitle: 'modals.yes'.tr(), actionButtonOnPressed: () => { - context - .read() - .restoreBackup(backup.id) + context.read().restoreBackup( + backup.id, // TODO: inex + BackupRestoreStrategy.unknown, + ) }, ); }, diff --git a/lib/ui/pages/backups/backups_list.dart b/lib/ui/pages/backups/backups_list.dart index 5241693a..af652a9b 100644 --- a/lib/ui/pages/backups/backups_list.dart +++ b/lib/ui/pages/backups/backups_list.dart @@ -54,7 +54,10 @@ class BackupsListPage extends StatelessWidget { ), actionButtonTitle: 'modals.yes'.tr(), actionButtonOnPressed: () => { - context.read().restoreBackup(backup.id) + context.read().restoreBackup( + backup.id, // TODO: inex + BackupRestoreStrategy.unknown, + ) }, ); },