diff --git a/assets/translations/en.json b/assets/translations/en.json index 52409913..840ffed0 100644 --- a/assets/translations/en.json +++ b/assets/translations/en.json @@ -172,6 +172,7 @@ "disk_total": "{} GB total ยท {}", "gb": "{} GB", "mb": "{} MB", + "kb": "{} KB", "extend_volume_button": "Extend volume", "extending_volume_title": "Extending volume", "extending_volume_description": "Resizing volume will allow you to store more data on your server without extending the server itself. Volume can only be extended: shrinking is not possible.", diff --git a/lib/logic/api_maps/graphql_maps/schema/server.dart b/lib/logic/api_maps/graphql_maps/schema/server.dart deleted file mode 100644 index 794cff51..00000000 --- a/lib/logic/api_maps/graphql_maps/schema/server.dart +++ /dev/null @@ -1,235 +0,0 @@ -import 'package:graphql/client.dart'; -import 'package:selfprivacy/config/get_it_config.dart'; -import 'package:selfprivacy/logic/api_maps/graphql_maps/api_map.dart'; -import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/server_api.graphql.dart'; -import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/disk_volumes.graphql.dart'; -import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/services.graphql.dart'; -import 'package:selfprivacy/logic/models/hive/server_domain.dart'; -import 'package:selfprivacy/logic/models/json/api_token.dart'; -import 'package:selfprivacy/logic/models/json/server_disk_volume.dart'; -import 'package:selfprivacy/logic/models/json/server_job.dart'; - -class ServerApi extends ApiMap { - ServerApi({ - this.hasLogger = false, - this.isWithToken = true, - this.customToken = '', - }) { - final ServerDomain? serverDomain = getIt().serverDomain; - rootAddress = serverDomain?.domainName ?? ''; - } - @override - bool hasLogger; - @override - bool isWithToken; - @override - String customToken; - @override - String? rootAddress; - - Future _commonBoolRequest(final Function graphQLMethod) async { - QueryResult response; - bool result = false; - - try { - response = await graphQLMethod(); - if (response.hasException) { - print(response.exception.toString()); - result = false; - } else { - result = true; - } - } catch (e) { - print(e); - } - - return result; - } - - Future getApiVersion() async { - QueryResult response; - String? apiVersion; - - try { - final GraphQLClient client = await getClient(); - response = await client.query$GetApiVersion(); - if (response.hasException) { - print(response.exception.toString()); - } - apiVersion = response.data!['api']['version']; - } catch (e) { - print(e); - } - return apiVersion; - } - - Future> getApiTokens() async { - QueryResult response; - List tokens = []; - - try { - final GraphQLClient client = await getClient(); - response = await client.query$GetApiTokens(); - if (response.hasException) { - print(response.exception.toString()); - } - tokens = response.data!['api']['devices'] - .map((final e) => ApiToken.fromJson(e)) - .toList(); - } catch (e) { - print(e); - } - - return tokens; - } - - Future> getServerDiskVolumes() async { - QueryResult response; - List volumes = []; - - try { - final GraphQLClient client = await getClient(); - response = await client.query$GetServerDiskVolumes(); - if (response.hasException) { - print(response.exception.toString()); - } - volumes = response.data!['storage']['volumes'] - .map((final e) => ServerDiskVolume.fromJson(e)) - .toList(); - } catch (e) { - print(e); - } - - return volumes; - } - - Future mountVolume(final String volumeName) async { - try { - final GraphQLClient client = await getClient(); - final variables = Variables$Mutation$MountVolume(name: volumeName); - final mountVolumeMutation = - Options$Mutation$MountVolume(variables: variables); - await client.mutate$MountVolume(mountVolumeMutation); - } catch (e) { - print(e); - } - } - - Future unmountVolume(final String volumeName) async { - try { - final GraphQLClient client = await getClient(); - final variables = Variables$Mutation$UnmountVolume(name: volumeName); - final unmountVolumeMutation = - Options$Mutation$UnmountVolume(variables: variables); - await client.mutate$UnmountVolume(unmountVolumeMutation); - } catch (e) { - print(e); - } - } - - Future resizeVolume(final String volumeName) async { - try { - final GraphQLClient client = await getClient(); - final variables = Variables$Mutation$ResizeVolume(name: volumeName); - final resizeVolumeMutation = - Options$Mutation$ResizeVolume(variables: variables); - await client.mutate$ResizeVolume(resizeVolumeMutation); - } catch (e) { - print(e); - } - } - - Future> getServerJobs() async { - QueryResult response; - List jobs = []; - - try { - final GraphQLClient client = await getClient(); - response = await client.query$GetApiJobs(); - if (response.hasException) { - print(response.exception.toString()); - } - jobs = response.data!['jobs'] - .map((final e) => ServerJob.fromJson(e)) - .toList(); - } catch (e) { - print(e); - } - - return jobs; - } - - Future removeApiJob(final String uid) async { - try { - final GraphQLClient client = await getClient(); - //await client.query$GetApiJobsQuery(); - } catch (e) { - print(e); - } - } - - Future reboot() async { - try { - final GraphQLClient client = await getClient(); - return await _commonBoolRequest( - () async { - await client.mutate$RebootSystem(); - }, - ); - } catch (e) { - return false; - } - } - - Future pullConfigurationUpdate() async { - try { - final GraphQLClient client = await getClient(); - return await _commonBoolRequest( - () async { - await client.mutate$PullRepositoryChanges(); - }, - ); - } catch (e) { - return false; - } - } - - Future upgrade() async { - try { - final GraphQLClient client = await getClient(); - return await _commonBoolRequest( - () async { - await client.mutate$RunSystemUpgrade(); - }, - ); - } catch (e) { - return false; - } - } - - Future switchService(final String uid, final bool needTurnOn) async { - try { - final GraphQLClient client = await getClient(); - if (needTurnOn) { - final variables = Variables$Mutation$EnableService(serviceId: uid); - final mutation = Options$Mutation$EnableService(variables: variables); - await client.mutate$EnableService(mutation); - } else { - final variables = Variables$Mutation$DisableService(serviceId: uid); - final mutation = Options$Mutation$DisableService(variables: variables); - await client.mutate$DisableService(mutation); - } - } catch (e) { - print(e); - } - } - - Future apply() async { - try { - final GraphQLClient client = await getClient(); - await client.mutate$RunSystemRebuild(); - } catch (e) { - print(e); - } - } -} diff --git a/lib/logic/api_maps/graphql_maps/server_api/jobs_api.dart b/lib/logic/api_maps/graphql_maps/server_api/jobs_api.dart new file mode 100644 index 00000000..f6bf36f8 --- /dev/null +++ b/lib/logic/api_maps/graphql_maps/server_api/jobs_api.dart @@ -0,0 +1,34 @@ +part of 'server.dart'; + +mixin JobsApi on ApiMap { + Future> getServerJobs() async { + QueryResult response; + List jobs = []; + + try { + final GraphQLClient client = await getClient(); + response = await client.query$GetApiJobs(); + if (response.hasException) { + print(response.exception.toString()); + } + jobs = response.data!['jobs'] + .map((final e) => ServerJob.fromJson(e)) + .toList(); + } catch (e) { + print(e); + } + + return jobs; + } + + Future removeApiJob(final String uid) async { + try { + final GraphQLClient client = await getClient(); + final variables = Variables$Mutation$RemoveJob(jobId: uid); + final mutation = Options$Mutation$RemoveJob(variables: variables); + await client.mutate$RemoveJob(mutation); + } catch (e) { + print(e); + } + } +} diff --git a/lib/logic/api_maps/graphql_maps/server_api/server.dart b/lib/logic/api_maps/graphql_maps/server_api/server.dart new file mode 100644 index 00000000..c7ee23e9 --- /dev/null +++ b/lib/logic/api_maps/graphql_maps/server_api/server.dart @@ -0,0 +1,88 @@ +import 'package:graphql/client.dart'; +import 'package:selfprivacy/config/get_it_config.dart'; +import 'package:selfprivacy/logic/api_maps/graphql_maps/api_map.dart'; +import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/server_api.graphql.dart'; +import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/disk_volumes.graphql.dart'; +import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/services.graphql.dart'; +import 'package:selfprivacy/logic/models/hive/server_domain.dart'; +import 'package:selfprivacy/logic/models/json/api_token.dart'; +import 'package:selfprivacy/logic/models/json/server_disk_volume.dart'; +import 'package:selfprivacy/logic/models/json/server_job.dart'; + +part 'volume_api.dart'; +part 'jobs_api.dart'; +part 'server_actions_api.dart'; +part 'services_api.dart'; + +class ServerApi extends ApiMap with VolumeApi, JobsApi, ServerActionsApi { + ServerApi({ + this.hasLogger = false, + this.isWithToken = true, + this.customToken = '', + }) { + final ServerDomain? serverDomain = getIt().serverDomain; + rootAddress = serverDomain?.domainName ?? ''; + } + @override + bool hasLogger; + @override + bool isWithToken; + @override + String customToken; + @override + String? rootAddress; + + Future getApiVersion() async { + QueryResult response; + String? apiVersion; + + try { + final GraphQLClient client = await getClient(); + response = await client.query$GetApiVersion(); + if (response.hasException) { + print(response.exception.toString()); + } + apiVersion = response.data!['api']['version']; + } catch (e) { + print(e); + } + return apiVersion; + } + + Future> getApiTokens() async { + QueryResult response; + List tokens = []; + + try { + final GraphQLClient client = await getClient(); + response = await client.query$GetApiTokens(); + if (response.hasException) { + print(response.exception.toString()); + } + tokens = response.data!['api']['devices'] + .map((final e) => ApiToken.fromJson(e)) + .toList(); + } catch (e) { + print(e); + } + + return tokens; + } + + Future switchService(final String uid, final bool needTurnOn) async { + try { + final GraphQLClient client = await getClient(); + if (needTurnOn) { + final variables = Variables$Mutation$EnableService(serviceId: uid); + final mutation = Options$Mutation$EnableService(variables: variables); + await client.mutate$EnableService(mutation); + } else { + final variables = Variables$Mutation$DisableService(serviceId: uid); + final mutation = Options$Mutation$DisableService(variables: variables); + await client.mutate$DisableService(mutation); + } + } catch (e) { + print(e); + } + } +} diff --git a/lib/logic/api_maps/graphql_maps/server_api/server_actions_api.dart b/lib/logic/api_maps/graphql_maps/server_api/server_actions_api.dart new file mode 100644 index 00000000..07ca101d --- /dev/null +++ b/lib/logic/api_maps/graphql_maps/server_api/server_actions_api.dart @@ -0,0 +1,70 @@ +part of 'server.dart'; + +mixin ServerActionsApi on ApiMap { + Future _commonBoolRequest(final Function graphQLMethod) async { + QueryResult response; + bool result = false; + + try { + response = await graphQLMethod(); + if (response.hasException) { + print(response.exception.toString()); + result = false; + } else { + result = true; + } + } catch (e) { + print(e); + } + + return result; + } + + Future reboot() async { + try { + final GraphQLClient client = await getClient(); + return await _commonBoolRequest( + () async { + await client.mutate$RebootSystem(); + }, + ); + } catch (e) { + return false; + } + } + + Future pullConfigurationUpdate() async { + try { + final GraphQLClient client = await getClient(); + return await _commonBoolRequest( + () async { + await client.mutate$PullRepositoryChanges(); + }, + ); + } catch (e) { + return false; + } + } + + Future upgrade() async { + try { + final GraphQLClient client = await getClient(); + return await _commonBoolRequest( + () async { + await client.mutate$RunSystemUpgrade(); + }, + ); + } catch (e) { + return false; + } + } + + Future apply() async { + try { + final GraphQLClient client = await getClient(); + await client.mutate$RunSystemRebuild(); + } catch (e) { + print(e); + } + } +} diff --git a/lib/logic/api_maps/graphql_maps/server_api/services_api.dart b/lib/logic/api_maps/graphql_maps/server_api/services_api.dart new file mode 100644 index 00000000..acbb8348 --- /dev/null +++ b/lib/logic/api_maps/graphql_maps/server_api/services_api.dart @@ -0,0 +1,3 @@ +part of 'server.dart'; + +mixin ServicesApi on ApiMap {} diff --git a/lib/logic/api_maps/graphql_maps/server_api/volume_api.dart b/lib/logic/api_maps/graphql_maps/server_api/volume_api.dart new file mode 100644 index 00000000..ada66d58 --- /dev/null +++ b/lib/logic/api_maps/graphql_maps/server_api/volume_api.dart @@ -0,0 +1,59 @@ +part of 'server.dart'; + +mixin VolumeApi on ApiMap { + Future> getServerDiskVolumes() async { + QueryResult response; + List volumes = []; + + try { + final GraphQLClient client = await getClient(); + response = await client.query$GetServerDiskVolumes(); + if (response.hasException) { + print(response.exception.toString()); + } + volumes = response.data!['storage']['volumes'] + .map((final e) => ServerDiskVolume.fromJson(e)) + .toList(); + } catch (e) { + print(e); + } + + return volumes; + } + + Future mountVolume(final String volumeName) async { + try { + final GraphQLClient client = await getClient(); + final variables = Variables$Mutation$MountVolume(name: volumeName); + final mountVolumeMutation = + Options$Mutation$MountVolume(variables: variables); + await client.mutate$MountVolume(mountVolumeMutation); + } catch (e) { + print(e); + } + } + + Future unmountVolume(final String volumeName) async { + try { + final GraphQLClient client = await getClient(); + final variables = Variables$Mutation$UnmountVolume(name: volumeName); + final unmountVolumeMutation = + Options$Mutation$UnmountVolume(variables: variables); + await client.mutate$UnmountVolume(unmountVolumeMutation); + } catch (e) { + print(e); + } + } + + Future resizeVolume(final String volumeName) async { + try { + final GraphQLClient client = await getClient(); + final variables = Variables$Mutation$ResizeVolume(name: volumeName); + final resizeVolumeMutation = + Options$Mutation$ResizeVolume(variables: variables); + await client.mutate$ResizeVolume(resizeVolumeMutation); + } catch (e) { + print(e); + } + } +} diff --git a/lib/logic/cubit/jobs/jobs_cubit.dart b/lib/logic/cubit/jobs/jobs_cubit.dart index 486770c0..baac08dd 100644 --- a/lib/logic/cubit/jobs/jobs_cubit.dart +++ b/lib/logic/cubit/jobs/jobs_cubit.dart @@ -4,7 +4,7 @@ import 'package:easy_localization/easy_localization.dart'; import 'package:equatable/equatable.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:selfprivacy/config/get_it_config.dart'; -import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/server.dart'; +import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server.dart'; import 'package:selfprivacy/logic/cubit/services/services_cubit.dart'; import 'package:selfprivacy/logic/cubit/users/users_cubit.dart'; import 'package:selfprivacy/logic/models/job.dart'; diff --git a/lib/logic/cubit/provider_volumes/provider_volume_cubit.dart b/lib/logic/cubit/provider_volumes/provider_volume_cubit.dart index be0a0d82..c1cb391a 100644 --- a/lib/logic/cubit/provider_volumes/provider_volume_cubit.dart +++ b/lib/logic/cubit/provider_volumes/provider_volume_cubit.dart @@ -1,5 +1,5 @@ import 'package:selfprivacy/config/get_it_config.dart'; -import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/server.dart'; +import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server.dart'; import 'package:selfprivacy/logic/api_maps/rest_maps/api_factory_creator.dart'; import 'package:selfprivacy/logic/api_maps/rest_maps/server_providers/server_provider_factory.dart'; import 'package:selfprivacy/logic/common_enum/common_enum.dart'; diff --git a/lib/logic/cubit/server_installation/server_installation_cubit.dart b/lib/logic/cubit/server_installation/server_installation_cubit.dart index 571eb8ac..aee5207f 100644 --- a/lib/logic/cubit/server_installation/server_installation_cubit.dart +++ b/lib/logic/cubit/server_installation/server_installation_cubit.dart @@ -4,7 +4,7 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:equatable/equatable.dart'; import 'package:selfprivacy/config/get_it_config.dart'; -import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/server.dart'; +import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server.dart'; import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider_factory.dart'; import 'package:selfprivacy/logic/api_maps/rest_maps/provider_api_settings.dart'; import 'package:selfprivacy/logic/models/hive/backblaze_credential.dart'; diff --git a/lib/logic/cubit/server_volumes/server_volume_cubit.dart b/lib/logic/cubit/server_volumes/server_volume_cubit.dart index fbd49a99..13015d0b 100644 --- a/lib/logic/cubit/server_volumes/server_volume_cubit.dart +++ b/lib/logic/cubit/server_volumes/server_volume_cubit.dart @@ -1,4 +1,4 @@ -import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/server.dart'; +import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server.dart'; import 'package:selfprivacy/logic/common_enum/common_enum.dart'; import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart'; import 'package:selfprivacy/logic/models/json/server_disk_volume.dart'; diff --git a/lib/logic/models/disk_size.dart b/lib/logic/models/disk_size.dart index d71b7479..ecccb2b8 100644 --- a/lib/logic/models/disk_size.dart +++ b/lib/logic/models/disk_size.dart @@ -1,9 +1,24 @@ +import 'package:easy_localization/easy_localization.dart'; + class DiskSize { DiskSize({final this.byte = 0}); + int byte; + double asKb() => byte / 1024.0; double asMb() => byte / 1024.0 / 1024.0; double asGb() => byte / 1024.0 / 1024.0 / 1024.0; - int byte; + @override + String toString() { + if (byte < 1024) { + return '${byte.toStringAsFixed(0)} ${tr('bytes')}'; + } else if (byte < 1024 * 1024) { + return 'providers.storage.kb'.tr(args: [asKb().toStringAsFixed(1)]); + } else if (byte < 1024 * 1024 * 1024) { + return 'providers.storage.mb'.tr(args: [asMb().toStringAsFixed(1)]); + } else { + return 'providers.storage.gb'.tr(args: [asGb().toStringAsFixed(1)]); + } + } } diff --git a/lib/logic/models/json/server_job.dart b/lib/logic/models/json/server_job.dart index 0a6454c1..a54b7651 100644 --- a/lib/logic/models/json/server_job.dart +++ b/lib/logic/models/json/server_job.dart @@ -24,16 +24,12 @@ class ServerJob { final String description; final String status; final String uid; - @JsonKey(name: 'updated_at') final String updatedAt; - @JsonKey(name: 'created_at') final DateTime createdAt; final String? error; final int? progress; final String? result; - @JsonKey(name: 'status_text') final String? statusText; - @JsonKey(name: 'finished_at') final String? finishedAt; } diff --git a/lib/ui/pages/more/info/info.dart b/lib/ui/pages/more/info/info.dart index 0860ca80..ac0419fc 100644 --- a/lib/ui/pages/more/info/info.dart +++ b/lib/ui/pages/more/info/info.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'package:selfprivacy/config/brand_theme.dart'; -import 'package:selfprivacy/logic/api_maps/graphql_maps/schema/server.dart'; +import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server.dart'; import 'package:selfprivacy/ui/components/brand_divider/brand_divider.dart'; import 'package:selfprivacy/ui/components/brand_header/brand_header.dart'; import 'package:selfprivacy/ui/components/brand_text/brand_text.dart';