From 8c5bdf9cd89590d69e51313184fc3007803fc260 Mon Sep 17 00:00:00 2001 From: Inex Code Date: Fri, 26 Jul 2024 17:09:33 +0300 Subject: [PATCH] feat: Check if the server supports logs --- assets/translations/en.json | 4 ++- .../bloc/server_logs/server_logs_bloc.dart | 33 +++++++++++++++---- .../server_details/logs/logs_screen.dart | 22 ++++++++++--- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/assets/translations/en.json b/assets/translations/en.json index a73b1716..7c518067 100644 --- a/assets/translations/en.json +++ b/assets/translations/en.json @@ -37,7 +37,9 @@ "alert": "Alert", "copied_to_clipboard": "Copied to clipboard!", "please_connect": "Please connect your server, domain and DNS provider to dive in!", - "network_error": "Network error" + "network_error": "Network error", + "feature_unsupported_on_api_version": "This feature is only supported on server version {versionConstraint}. Your server is on version {currentVersion}.", + "error": "Error" }, "more_page": { "configuration_wizard": "Setup wizard", diff --git a/lib/logic/bloc/server_logs/server_logs_bloc.dart b/lib/logic/bloc/server_logs/server_logs_bloc.dart index f115ec5d..e528600e 100644 --- a/lib/logic/bloc/server_logs/server_logs_bloc.dart +++ b/lib/logic/bloc/server_logs/server_logs_bloc.dart @@ -1,7 +1,9 @@ import 'dart:async'; +import 'package:easy_localization/easy_localization.dart'; import 'package:equatable/equatable.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pub_semver/pub_semver.dart'; import 'package:selfprivacy/config/get_it_config.dart'; import 'package:selfprivacy/logic/models/server_logs.dart'; @@ -74,6 +76,8 @@ class ServerLogsBloc extends Bloc { }); } + static const String logsSupportedVersion = '>=3.3.0'; + Future<(List, ServerLogsPageMeta)> _getLogs({ // No more than 50 required final int limit, @@ -82,12 +86,29 @@ class ServerLogsBloc extends Bloc { // All entries returned will be greater than this cursor. Sets lower bound on results. final String? downCursor, // Only one cursor can be set at a time. - }) => - getIt().api.getServerLogs( - limit: limit, - upCursor: upCursor, - downCursor: downCursor, - ); + }) { + final String? apiVersion = + getIt().apiData.apiVersion.data; + if (apiVersion == null) { + throw Exception('basis.network_error'.tr()); + } + if (!VersionConstraint.parse(logsSupportedVersion) + .allows(Version.parse(apiVersion))) { + throw Exception( + 'basis.feature_unsupported_on_api_version'.tr( + namedArgs: { + 'versionConstraint': logsSupportedVersion, + 'currentVersion': apiVersion, + }, + ), + ); + } + return getIt().api.getServerLogs( + limit: limit, + upCursor: upCursor, + downCursor: downCursor, + ); + } @override Future close() { diff --git a/lib/ui/pages/server_details/logs/logs_screen.dart b/lib/ui/pages/server_details/logs/logs_screen.dart index f8f6a1ac..3c6e11ec 100644 --- a/lib/ui/pages/server_details/logs/logs_screen.dart +++ b/lib/ui/pages/server_details/logs/logs_screen.dart @@ -5,6 +5,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:selfprivacy/logic/bloc/server_logs/server_logs_bloc.dart'; import 'package:selfprivacy/logic/models/server_logs.dart'; +import 'package:selfprivacy/ui/helpers/empty_page_placeholder.dart'; import 'package:selfprivacy/utils/platform_adapter.dart'; @RoutePage() @@ -86,7 +87,8 @@ class _ServerLogsScreenState extends State { if (state is ServerLogsLoaded) { return _buildDrawer(state.systemdUnits); } - return const SizedBox.shrink(); + // Return an empty drawer if the state is not loaded + return const Drawer(child: SizedBox()); }, ), body: BlocBuilder( @@ -130,7 +132,11 @@ class _ServerLogsScreenState extends State { ); } } else if (state is ServerLogsError) { - return Center(child: Text('Error: ${state.error}')); + return EmptyPagePlaceholder( + title: 'basis.error'.tr(), + iconData: Icons.error_outline, + description: state.error.toString(), + ); } return Center(child: Text('server.no_logs'.tr())); }, @@ -240,13 +246,19 @@ class ServerLogEntryDialog extends StatelessWidget { _KeyValueRow('server.log_dialog.cursor'.tr(), log.cursor), if (log.priority != null) _KeyValueRow( - 'server.log_dialog.priority'.tr(), log.priority?.toString()), + 'server.log_dialog.priority'.tr(), + log.priority?.toString(), + ), if (log.systemdSlice != null) _KeyValueRow( - 'server.log_dialog.systemd_slice'.tr(), log.systemdSlice), + 'server.log_dialog.systemd_slice'.tr(), + log.systemdSlice, + ), if (log.systemdUnit != null) _KeyValueRow( - 'server.log_dialog.systemd_unit'.tr(), log.systemdUnit), + 'server.log_dialog.systemd_unit'.tr(), + log.systemdUnit, + ), const Divider(), _SectionRow('server.log_dialog.message'.tr()), _DataRow(log.message),