2024-01-26 14:46:09 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2024-02-09 15:01:05 +00:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2024-01-26 14:46:09 +00:00
|
|
|
import 'package:hive/hive.dart';
|
|
|
|
import 'package:pub_semver/pub_semver.dart';
|
|
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
|
|
|
import 'package:selfprivacy/config/hive_config.dart';
|
|
|
|
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server_api.dart';
|
2024-05-15 14:16:59 +00:00
|
|
|
import 'package:selfprivacy/logic/get_it/resources_model.dart';
|
2024-02-20 20:17:36 +00:00
|
|
|
import 'package:selfprivacy/logic/models/auto_upgrade_settings.dart';
|
2024-01-29 13:54:09 +00:00
|
|
|
import 'package:selfprivacy/logic/models/backup.dart';
|
2024-01-26 14:46:09 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
2024-02-09 15:01:05 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/user.dart';
|
2024-02-09 11:07:03 +00:00
|
|
|
import 'package:selfprivacy/logic/models/json/api_token.dart';
|
2024-02-08 15:08:29 +00:00
|
|
|
import 'package:selfprivacy/logic/models/json/recovery_token_status.dart';
|
2024-02-01 14:30:06 +00:00
|
|
|
import 'package:selfprivacy/logic/models/json/server_disk_volume.dart';
|
2024-01-26 14:46:09 +00:00
|
|
|
import 'package:selfprivacy/logic/models/json/server_job.dart';
|
2024-01-29 15:57:22 +00:00
|
|
|
import 'package:selfprivacy/logic/models/service.dart';
|
2024-03-09 16:30:43 +00:00
|
|
|
import 'package:selfprivacy/logic/models/ssh_settings.dart';
|
2024-02-20 17:09:14 +00:00
|
|
|
import 'package:selfprivacy/logic/models/system_settings.dart';
|
2024-01-26 14:46:09 +00:00
|
|
|
|
|
|
|
/// Repository for all API calls
|
|
|
|
/// Stores the current state of all data from API and exposes it to Blocs.
|
|
|
|
class ApiConnectionRepository {
|
|
|
|
Box box = Hive.box(BNames.serverInstallationBox);
|
|
|
|
final ServerApi api = ServerApi();
|
|
|
|
|
2024-01-29 13:54:09 +00:00
|
|
|
final ApiData _apiData = ApiData(ServerApi());
|
2024-01-26 14:46:09 +00:00
|
|
|
|
|
|
|
ApiData get apiData => _apiData;
|
|
|
|
|
|
|
|
ConnectionStatus connectionStatus = ConnectionStatus.nonexistent;
|
|
|
|
|
|
|
|
final _dataStream = StreamController<ApiData>.broadcast();
|
|
|
|
final _connectionStatusStream =
|
|
|
|
StreamController<ConnectionStatus>.broadcast();
|
|
|
|
|
|
|
|
Stream<ApiData> get dataStream => _dataStream.stream;
|
|
|
|
Stream<ConnectionStatus> get connectionStatusStream =>
|
|
|
|
_connectionStatusStream.stream;
|
|
|
|
|
|
|
|
ConnectionStatus get currentConnectionStatus => connectionStatus;
|
|
|
|
|
|
|
|
Timer? _timer;
|
|
|
|
|
2024-07-04 17:13:20 +00:00
|
|
|
StreamSubscription<List<ServerJob>>? _serverJobsStreamSubscription;
|
|
|
|
|
2024-01-26 14:46:09 +00:00
|
|
|
Future<void> removeServerJob(final String uid) async {
|
|
|
|
await api.removeApiJob(uid);
|
|
|
|
_apiData.serverJobs.data
|
|
|
|
?.removeWhere((final ServerJob element) => element.uid == uid);
|
|
|
|
_dataStream.add(_apiData);
|
|
|
|
}
|
|
|
|
|
2024-01-29 16:14:12 +00:00
|
|
|
Future<void> removeAllFinishedServerJobs() async {
|
|
|
|
final List<ServerJob> finishedJobs = _apiData.serverJobs.data
|
|
|
|
?.where(
|
|
|
|
(final ServerJob element) =>
|
|
|
|
element.status == JobStatusEnum.finished ||
|
|
|
|
element.status == JobStatusEnum.error,
|
|
|
|
)
|
|
|
|
.toList() ??
|
|
|
|
[];
|
|
|
|
// Optimistically remove the jobs from the list
|
|
|
|
_apiData.serverJobs.data?.removeWhere(
|
|
|
|
(final ServerJob element) =>
|
|
|
|
element.status == JobStatusEnum.finished ||
|
|
|
|
element.status == JobStatusEnum.error,
|
|
|
|
);
|
|
|
|
_dataStream.add(_apiData);
|
|
|
|
|
|
|
|
await Future.forEach<ServerJob>(
|
|
|
|
finishedJobs,
|
|
|
|
(final ServerJob job) async => removeServerJob(job.uid),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-02-20 16:33:24 +00:00
|
|
|
Future<(bool, String)> createUser(final User user) async {
|
2024-02-09 15:01:05 +00:00
|
|
|
final List<User>? loadedUsers = _apiData.users.data;
|
|
|
|
if (loadedUsers == null) {
|
2024-02-20 16:33:24 +00:00
|
|
|
return (false, 'basis.network_error'.tr());
|
2024-02-09 15:01:05 +00:00
|
|
|
}
|
|
|
|
// If user exists on server, do nothing
|
|
|
|
if (loadedUsers
|
|
|
|
.any((final User u) => u.login == user.login && u.isFoundOnServer)) {
|
2024-02-20 16:33:24 +00:00
|
|
|
return (false, 'users.user_already_exists'.tr());
|
2024-02-09 15:01:05 +00:00
|
|
|
}
|
|
|
|
final String? password = user.password;
|
|
|
|
if (password == null) {
|
2024-02-20 16:33:24 +00:00
|
|
|
return (false, 'users.could_not_create_user'.tr());
|
2024-02-09 15:01:05 +00:00
|
|
|
}
|
|
|
|
// If API returned error, do nothing
|
|
|
|
final GenericResult<User?> result =
|
|
|
|
await api.createUser(user.login, password);
|
|
|
|
if (result.data == null) {
|
2024-02-20 16:33:24 +00:00
|
|
|
return (false, result.message ?? 'users.could_not_create_user'.tr());
|
2024-02-09 15:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_apiData.users.data?.add(result.data!);
|
|
|
|
_apiData.users.invalidate();
|
2024-02-20 16:33:24 +00:00
|
|
|
|
|
|
|
return (true, result.message ?? 'basis.done'.tr());
|
2024-02-09 15:01:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 16:33:24 +00:00
|
|
|
Future<(bool, String)> deleteUser(final User user) async {
|
2024-02-09 15:01:05 +00:00
|
|
|
final List<User>? loadedUsers = _apiData.users.data;
|
|
|
|
if (loadedUsers == null) {
|
2024-02-20 16:33:24 +00:00
|
|
|
return (false, 'basis.network_error'.tr());
|
2024-02-09 15:01:05 +00:00
|
|
|
}
|
|
|
|
// If user is primary or root, don't delete
|
|
|
|
if (user.type != UserType.normal) {
|
2024-02-20 16:33:24 +00:00
|
|
|
return (false, 'users.could_not_delete_user'.tr());
|
2024-02-09 15:01:05 +00:00
|
|
|
}
|
|
|
|
final GenericResult result = await api.deleteUser(user.login);
|
|
|
|
if (result.success && result.data) {
|
|
|
|
_apiData.users.data?.removeWhere((final User u) => u.login == user.login);
|
|
|
|
_apiData.users.invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result.success || !result.data) {
|
2024-02-20 16:33:24 +00:00
|
|
|
return (false, result.message ?? 'jobs.generic_error'.tr());
|
2024-02-09 15:01:05 +00:00
|
|
|
}
|
2024-02-20 16:33:24 +00:00
|
|
|
|
|
|
|
return (true, result.message ?? 'basis.done'.tr());
|
2024-02-09 15:01:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 16:33:24 +00:00
|
|
|
Future<(bool, String)> changeUserPassword(
|
2024-02-09 15:01:05 +00:00
|
|
|
final User user,
|
|
|
|
final String newPassword,
|
|
|
|
) async {
|
|
|
|
if (user.type == UserType.root) {
|
2024-02-20 16:33:24 +00:00
|
|
|
return (false, 'users.could_not_change_password'.tr());
|
2024-02-09 15:01:05 +00:00
|
|
|
}
|
|
|
|
final GenericResult<User?> result = await api.updateUser(
|
|
|
|
user.login,
|
|
|
|
newPassword,
|
|
|
|
);
|
|
|
|
if (result.data == null) {
|
|
|
|
getIt<NavigationService>().showSnackBar(
|
|
|
|
result.message ?? 'users.could_not_change_password'.tr(),
|
|
|
|
);
|
2024-02-20 16:33:24 +00:00
|
|
|
return (
|
|
|
|
false,
|
|
|
|
result.message ?? 'users.could_not_change_password'.tr(),
|
|
|
|
);
|
2024-02-09 15:01:05 +00:00
|
|
|
}
|
2024-02-20 16:33:24 +00:00
|
|
|
return (true, result.message ?? 'basis.done'.tr());
|
2024-02-09 15:01:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 16:33:24 +00:00
|
|
|
Future<(bool, String)> addSshKey(
|
|
|
|
final User user,
|
|
|
|
final String publicKey,
|
|
|
|
) async {
|
2024-02-09 15:01:05 +00:00
|
|
|
final List<User>? loadedUsers = _apiData.users.data;
|
|
|
|
if (loadedUsers == null) {
|
2024-02-20 16:33:24 +00:00
|
|
|
return (false, 'basis.network_error'.tr());
|
2024-02-09 15:01:05 +00:00
|
|
|
}
|
|
|
|
final GenericResult<User?> result =
|
|
|
|
await api.addSshKey(user.login, publicKey);
|
|
|
|
if (result.data != null) {
|
|
|
|
final User updatedUser = result.data!;
|
|
|
|
final int index =
|
|
|
|
loadedUsers.indexWhere((final User u) => u.login == user.login);
|
|
|
|
loadedUsers[index] = updatedUser;
|
|
|
|
_apiData.users.invalidate();
|
|
|
|
} else {
|
2024-02-20 16:33:24 +00:00
|
|
|
return (false, result.message ?? 'users.could_not_add_ssh_key'.tr());
|
2024-02-09 15:01:05 +00:00
|
|
|
}
|
2024-02-20 16:33:24 +00:00
|
|
|
|
|
|
|
return (true, result.message ?? 'basis.done'.tr());
|
2024-02-09 15:01:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 16:33:24 +00:00
|
|
|
Future<(bool, String)> deleteSshKey(
|
|
|
|
final User user,
|
|
|
|
final String publicKey,
|
|
|
|
) async {
|
2024-02-09 15:01:05 +00:00
|
|
|
final List<User>? loadedUsers = _apiData.users.data;
|
|
|
|
if (loadedUsers == null) {
|
2024-02-20 16:33:24 +00:00
|
|
|
return (false, 'basis.network_error'.tr());
|
2024-02-09 15:01:05 +00:00
|
|
|
}
|
|
|
|
final GenericResult<User?> result =
|
|
|
|
await api.removeSshKey(user.login, publicKey);
|
|
|
|
if (result.data != null) {
|
|
|
|
final User updatedUser = result.data!;
|
|
|
|
final int index =
|
|
|
|
loadedUsers.indexWhere((final User u) => u.login == user.login);
|
|
|
|
loadedUsers[index] = updatedUser;
|
|
|
|
_apiData.users.invalidate();
|
|
|
|
} else {
|
2024-02-20 16:33:24 +00:00
|
|
|
return (false, result.message ?? 'jobs.generic_error'.tr());
|
2024-02-09 15:01:05 +00:00
|
|
|
}
|
2024-02-20 16:33:24 +00:00
|
|
|
return (true, result.message ?? 'basis.done'.tr());
|
2024-02-09 15:01:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 20:17:36 +00:00
|
|
|
Future<(bool, String)> setAutoUpgradeSettings(
|
|
|
|
final bool enable,
|
|
|
|
final bool allowReboot,
|
|
|
|
) async {
|
|
|
|
final GenericResult<AutoUpgradeSettings?> result =
|
|
|
|
await api.setAutoUpgradeSettings(
|
|
|
|
AutoUpgradeSettings(
|
|
|
|
enable: enable,
|
|
|
|
allowReboot: allowReboot,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
_apiData.settings.invalidate();
|
|
|
|
if (result.data != null) {
|
|
|
|
return (true, result.message ?? 'basis.done'.tr());
|
|
|
|
} else {
|
|
|
|
return (false, result.message ?? 'jobs.generic_error'.tr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<(bool, String)> setServerTimezone(
|
|
|
|
final String timezone,
|
|
|
|
) async {
|
|
|
|
final GenericResult result = await api.setTimezone(timezone);
|
|
|
|
_apiData.settings.invalidate();
|
|
|
|
if (result.success) {
|
|
|
|
return (true, result.message ?? 'basis.done'.tr());
|
|
|
|
} else {
|
|
|
|
return (false, result.message ?? 'jobs.generic_error'.tr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-09 16:30:43 +00:00
|
|
|
Future<(bool, String)> setSshSettings(
|
|
|
|
final bool enable,
|
|
|
|
final bool passwordAuthentication,
|
|
|
|
) async {
|
|
|
|
final GenericResult<SshSettings?> result = await api.setSshSettings(
|
|
|
|
SshSettings(
|
|
|
|
enable: enable,
|
|
|
|
passwordAuthentication: passwordAuthentication,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
_apiData.settings.invalidate();
|
|
|
|
if (result.data != null) {
|
|
|
|
return (true, result.message ?? 'basis.done'.tr());
|
|
|
|
} else {
|
|
|
|
return (false, result.message ?? 'jobs.generic_error'.tr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 14:46:09 +00:00
|
|
|
void dispose() {
|
|
|
|
_dataStream.close();
|
|
|
|
_connectionStatusStream.close();
|
|
|
|
_timer?.cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerHostingDetails? get serverDetails =>
|
2024-05-15 14:16:59 +00:00
|
|
|
getIt<ResourcesModel>().serverDetails;
|
|
|
|
ServerDomain? get serverDomain => getIt<ResourcesModel>().serverDomain;
|
2024-01-26 14:46:09 +00:00
|
|
|
|
|
|
|
void init() async {
|
2024-05-15 14:16:59 +00:00
|
|
|
final serverDetails = getIt<ResourcesModel>().serverDetails;
|
|
|
|
if (serverDetails == null) {
|
2024-01-26 14:46:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
connectionStatus = ConnectionStatus.reconnecting;
|
|
|
|
_connectionStatusStream.add(connectionStatus);
|
|
|
|
|
|
|
|
final String? apiVersion = await api.getApiVersion();
|
|
|
|
if (apiVersion == null) {
|
|
|
|
connectionStatus = ConnectionStatus.offline;
|
|
|
|
_connectionStatusStream.add(connectionStatus);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
_apiData.apiVersion.data = apiVersion;
|
|
|
|
_dataStream.add(_apiData);
|
|
|
|
}
|
|
|
|
|
2024-02-20 20:34:45 +00:00
|
|
|
await _refetchEverything(Version.parse(apiVersion));
|
2024-01-26 14:46:09 +00:00
|
|
|
|
2024-01-29 13:54:09 +00:00
|
|
|
connectionStatus = ConnectionStatus.connected;
|
|
|
|
_connectionStatusStream.add(connectionStatus);
|
|
|
|
|
2024-07-10 16:00:16 +00:00
|
|
|
if (VersionConstraint.parse(wsJobsUpdatesSupportedVersion)
|
|
|
|
.allows(Version.parse(apiVersion))) {
|
|
|
|
_serverJobsStreamSubscription =
|
|
|
|
api.getServerJobsStream().listen((final List<ServerJob> jobs) {
|
|
|
|
_apiData.serverJobs.data = jobs;
|
|
|
|
_dataStream.add(_apiData);
|
|
|
|
});
|
|
|
|
}
|
2024-07-04 17:13:20 +00:00
|
|
|
|
2024-01-26 14:46:09 +00:00
|
|
|
// Use timer to periodically check for new jobs
|
|
|
|
_timer = Timer.periodic(
|
|
|
|
const Duration(seconds: 10),
|
|
|
|
reload,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-15 14:16:59 +00:00
|
|
|
void clear() async {
|
|
|
|
connectionStatus = ConnectionStatus.nonexistent;
|
|
|
|
_connectionStatusStream.add(connectionStatus);
|
|
|
|
_timer?.cancel();
|
2024-07-04 17:13:20 +00:00
|
|
|
await _serverJobsStreamSubscription?.cancel();
|
2024-05-15 14:16:59 +00:00
|
|
|
}
|
|
|
|
|
2024-07-10 16:00:16 +00:00
|
|
|
static const String wsJobsUpdatesSupportedVersion = '>=3.3.0';
|
|
|
|
|
2024-02-20 20:34:45 +00:00
|
|
|
Future<void> _refetchEverything(final Version version) async {
|
2024-07-10 16:00:16 +00:00
|
|
|
if (_serverJobsStreamSubscription == null ||
|
|
|
|
_apiData.serverJobs.data == null) {
|
|
|
|
await _apiData.serverJobs
|
|
|
|
.refetchData(version, () => _dataStream.add(_apiData));
|
|
|
|
}
|
2024-01-29 13:54:09 +00:00
|
|
|
await _apiData.backups
|
|
|
|
.refetchData(version, () => _dataStream.add(_apiData));
|
|
|
|
await _apiData.backupConfig
|
|
|
|
.refetchData(version, () => _dataStream.add(_apiData));
|
2024-01-29 15:57:22 +00:00
|
|
|
await _apiData.services
|
|
|
|
.refetchData(version, () => _dataStream.add(_apiData));
|
2024-02-01 14:30:06 +00:00
|
|
|
await _apiData.volumes
|
|
|
|
.refetchData(version, () => _dataStream.add(_apiData));
|
2024-02-08 15:08:29 +00:00
|
|
|
await _apiData.recoveryKeyStatus
|
|
|
|
.refetchData(version, () => _dataStream.add(_apiData));
|
2024-02-09 11:07:03 +00:00
|
|
|
await _apiData.devices
|
|
|
|
.refetchData(version, () => _dataStream.add(_apiData));
|
2024-02-09 15:01:05 +00:00
|
|
|
await _apiData.users.refetchData(version, () => _dataStream.add(_apiData));
|
2024-02-20 17:09:14 +00:00
|
|
|
await _apiData.settings
|
|
|
|
.refetchData(version, () => _dataStream.add(_apiData));
|
2024-02-20 20:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> reload(final Timer? timer) async {
|
2024-05-15 14:16:59 +00:00
|
|
|
final serverDetails = getIt<ResourcesModel>().serverDetails;
|
2024-02-20 20:34:45 +00:00
|
|
|
if (serverDetails == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final String? apiVersion = await api.getApiVersion();
|
|
|
|
if (apiVersion == null) {
|
|
|
|
connectionStatus = ConnectionStatus.offline;
|
|
|
|
_connectionStatusStream.add(connectionStatus);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
connectionStatus = ConnectionStatus.connected;
|
|
|
|
_connectionStatusStream.add(connectionStatus);
|
|
|
|
_apiData.apiVersion.data = apiVersion;
|
|
|
|
}
|
|
|
|
final Version version = Version.parse(apiVersion);
|
|
|
|
await _refetchEverything(version);
|
2024-01-29 13:54:09 +00:00
|
|
|
}
|
2024-01-26 14:46:09 +00:00
|
|
|
|
2024-01-29 13:54:09 +00:00
|
|
|
void emitData() {
|
|
|
|
_dataStream.add(_apiData);
|
2024-01-26 14:46:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ApiData {
|
2024-01-29 13:54:09 +00:00
|
|
|
ApiData(final ServerApi api)
|
|
|
|
: apiVersion = ApiDataElement<String>(
|
|
|
|
fetchData: () async => api.getApiVersion(),
|
|
|
|
),
|
|
|
|
serverJobs = ApiDataElement<List<ServerJob>>(
|
|
|
|
fetchData: () async => api.getServerJobs(),
|
2024-07-10 16:00:16 +00:00
|
|
|
ttl: 10,
|
2024-01-29 13:54:09 +00:00
|
|
|
),
|
|
|
|
backupConfig = ApiDataElement<BackupConfiguration>(
|
|
|
|
fetchData: () async => api.getBackupsConfiguration(),
|
|
|
|
requiredApiVersion: '>=2.4.2',
|
2024-02-08 15:08:29 +00:00
|
|
|
ttl: 120,
|
2024-01-29 13:54:09 +00:00
|
|
|
),
|
|
|
|
backups = ApiDataElement<List<Backup>>(
|
|
|
|
fetchData: () async => api.getBackups(),
|
|
|
|
requiredApiVersion: '>=2.4.2',
|
2024-02-08 15:08:29 +00:00
|
|
|
ttl: 120,
|
2024-01-29 15:57:22 +00:00
|
|
|
),
|
|
|
|
services = ApiDataElement<List<Service>>(
|
|
|
|
fetchData: () async => api.getAllServices(),
|
|
|
|
requiredApiVersion: '>=2.4.3',
|
2024-02-01 14:30:06 +00:00
|
|
|
),
|
|
|
|
volumes = ApiDataElement<List<ServerDiskVolume>>(
|
|
|
|
fetchData: () async => api.getServerDiskVolumes(),
|
2024-02-08 15:08:29 +00:00
|
|
|
),
|
|
|
|
recoveryKeyStatus = ApiDataElement<RecoveryKeyStatus>(
|
|
|
|
fetchData: () async => (await api.getRecoveryTokenStatus()).data,
|
|
|
|
ttl: 300,
|
2024-02-09 11:07:03 +00:00
|
|
|
),
|
|
|
|
devices = ApiDataElement<List<ApiToken>>(
|
|
|
|
fetchData: () async => (await api.getApiTokens()).data,
|
2024-02-09 15:01:05 +00:00
|
|
|
),
|
|
|
|
users = ApiDataElement<List<User>>(
|
|
|
|
fetchData: () async => api.getAllUsers(),
|
2024-02-20 17:09:14 +00:00
|
|
|
),
|
|
|
|
settings = ApiDataElement<SystemSettings>(
|
|
|
|
fetchData: () async => api.getSystemSettings(),
|
|
|
|
ttl: 600,
|
2024-01-29 13:54:09 +00:00
|
|
|
);
|
2024-01-26 14:46:09 +00:00
|
|
|
|
|
|
|
ApiDataElement<List<ServerJob>> serverJobs;
|
|
|
|
ApiDataElement<String> apiVersion;
|
2024-01-29 13:54:09 +00:00
|
|
|
ApiDataElement<BackupConfiguration> backupConfig;
|
|
|
|
ApiDataElement<List<Backup>> backups;
|
2024-01-29 15:57:22 +00:00
|
|
|
ApiDataElement<List<Service>> services;
|
2024-02-01 14:30:06 +00:00
|
|
|
ApiDataElement<List<ServerDiskVolume>> volumes;
|
2024-02-08 15:08:29 +00:00
|
|
|
ApiDataElement<RecoveryKeyStatus> recoveryKeyStatus;
|
2024-02-09 11:07:03 +00:00
|
|
|
ApiDataElement<List<ApiToken>> devices;
|
2024-02-09 15:01:05 +00:00
|
|
|
ApiDataElement<List<User>> users;
|
2024-02-20 17:09:14 +00:00
|
|
|
ApiDataElement<SystemSettings> settings;
|
2024-01-26 14:46:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum ConnectionStatus {
|
|
|
|
nonexistent,
|
|
|
|
connected,
|
|
|
|
reconnecting,
|
|
|
|
offline,
|
|
|
|
unauthorized,
|
|
|
|
}
|
|
|
|
|
|
|
|
class ApiDataElement<T> {
|
2024-01-29 13:54:09 +00:00
|
|
|
ApiDataElement({
|
|
|
|
required this.fetchData,
|
|
|
|
final T? data,
|
2024-01-26 14:46:09 +00:00
|
|
|
this.requiredApiVersion = '>=2.3.0',
|
|
|
|
this.ttl = 60,
|
|
|
|
}) : _data = data,
|
|
|
|
_lastUpdated = DateTime.now();
|
|
|
|
|
|
|
|
T? _data;
|
|
|
|
final String requiredApiVersion;
|
|
|
|
|
2024-01-29 13:54:09 +00:00
|
|
|
final Future<T?> Function() fetchData;
|
|
|
|
|
|
|
|
Future<void> refetchData(
|
2024-01-29 15:57:22 +00:00
|
|
|
final Version version,
|
|
|
|
final Function callback,
|
|
|
|
) async {
|
2024-01-29 13:54:09 +00:00
|
|
|
if (VersionConstraint.parse(requiredApiVersion).allows(version)) {
|
2024-02-20 20:34:45 +00:00
|
|
|
if (isExpired || _data == null) {
|
2024-01-29 13:54:09 +00:00
|
|
|
final newData = await fetchData();
|
|
|
|
if (T is List) {
|
|
|
|
if (Object.hashAll(newData as Iterable<Object?>) !=
|
|
|
|
Object.hashAll(_data as Iterable<Object?>)) {
|
|
|
|
_data = [...newData] as T?;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (newData.hashCode != _data.hashCode) {
|
|
|
|
_data = newData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 14:46:09 +00:00
|
|
|
/// TTL of the data in seconds
|
|
|
|
final int ttl;
|
|
|
|
|
2024-01-29 13:54:09 +00:00
|
|
|
Type get type => T;
|
|
|
|
|
|
|
|
void invalidate() {
|
|
|
|
_lastUpdated = DateTime.fromMillisecondsSinceEpoch(0);
|
|
|
|
}
|
|
|
|
|
2024-01-26 14:46:09 +00:00
|
|
|
/// Timestamp of when the data was last updated
|
|
|
|
DateTime _lastUpdated;
|
|
|
|
|
|
|
|
bool get isExpired {
|
|
|
|
final now = DateTime.now();
|
|
|
|
final difference = now.difference(_lastUpdated);
|
|
|
|
return difference.inSeconds > ttl;
|
|
|
|
}
|
|
|
|
|
|
|
|
T? get data => _data;
|
|
|
|
|
|
|
|
/// Sets the data and updates the lastUpdated timestamp
|
|
|
|
set data(final T? data) {
|
|
|
|
_data = data;
|
|
|
|
_lastUpdated = DateTime.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the last time the data was updated
|
|
|
|
DateTime get lastUpdated => _lastUpdated;
|
|
|
|
}
|