selfprivacy.org.app/lib/logic/cubit/client_jobs/client_jobs_cubit.dart

144 lines
4.6 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:easy_localization/easy_localization.dart';
import 'package:equatable/equatable.dart';
2021-05-25 21:53:54 +00:00
import 'package:flutter_bloc/flutter_bloc.dart';
2021-07-29 05:24:42 +00:00
import 'package:selfprivacy/config/get_it_config.dart';
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server.dart';
2021-08-29 13:54:28 +00:00
import 'package:selfprivacy/logic/cubit/services/services_cubit.dart';
2021-07-29 05:24:42 +00:00
import 'package:selfprivacy/logic/cubit/users/users_cubit.dart';
2021-08-29 15:02:51 +00:00
import 'package:selfprivacy/logic/models/job.dart';
2021-05-25 21:53:54 +00:00
export 'package:provider/provider.dart';
2022-08-30 03:09:09 +00:00
part 'client_jobs_state.dart';
2021-05-25 21:53:54 +00:00
class JobsCubit extends Cubit<JobsState> {
2021-08-29 13:54:28 +00:00
JobsCubit({
required this.usersCubit,
required this.servicesCubit,
2022-08-30 03:09:09 +00:00
}) : super(JobsStateEmpty());
2021-05-25 21:53:54 +00:00
2022-06-05 19:36:32 +00:00
final ServerApi api = ServerApi();
2021-07-29 05:24:42 +00:00
final UsersCubit usersCubit;
2021-08-29 13:54:28 +00:00
final ServicesCubit servicesCubit;
2021-05-25 21:53:54 +00:00
void addJob(final ClientJob job) {
final List<ClientJob> newJobsList = [];
2021-07-29 05:24:42 +00:00
if (state is JobsStateWithJobs) {
final JobsStateWithJobs jobsState = state as JobsStateWithJobs;
newJobsList.addAll(jobsState.clientJobList);
2021-07-29 05:24:42 +00:00
}
newJobsList.add(job);
2021-09-15 14:37:22 +00:00
getIt<NavigationService>().showSnackBar('jobs.jobAdded'.tr());
2022-08-30 03:09:09 +00:00
emit(JobsStateWithJobs(newJobsList));
2021-05-25 21:53:54 +00:00
}
2022-06-05 19:36:32 +00:00
void removeJob(final String id) {
final JobsState newState = (state as JobsStateWithJobs).removeById(id);
2021-05-25 21:53:54 +00:00
emit(newState);
}
2022-06-05 19:36:32 +00:00
void createOrRemoveServiceToggleJob(final ToggleJob job) {
final List<ClientJob> newJobsList = <ClientJob>[];
2021-08-29 15:02:51 +00:00
if (state is JobsStateWithJobs) {
newJobsList.addAll((state as JobsStateWithJobs).clientJobList);
2021-08-29 15:02:51 +00:00
}
final bool needToRemoveJob = newJobsList
.any((final el) => el is ServiceToggleJob && el.type == job.type);
2021-08-29 15:02:51 +00:00
if (needToRemoveJob) {
final ClientJob removingJob = newJobsList.firstWhere(
(final el) => el is ServiceToggleJob && el.type == job.type,
);
2021-08-29 15:02:51 +00:00
removeJob(removingJob.id);
} else {
newJobsList.add(job);
2021-09-15 14:37:22 +00:00
getIt<NavigationService>().showSnackBar('jobs.jobAdded'.tr());
2022-08-30 03:09:09 +00:00
emit(JobsStateWithJobs(newJobsList));
2021-09-02 19:32:07 +00:00
}
}
2022-06-05 19:36:32 +00:00
void createShhJobIfNotExist(final CreateSSHKeyJob job) {
final List<ClientJob> newJobsList = <ClientJob>[];
2021-09-02 19:32:07 +00:00
if (state is JobsStateWithJobs) {
newJobsList.addAll((state as JobsStateWithJobs).clientJobList);
2021-09-02 19:32:07 +00:00
}
final bool isExistInJobList =
newJobsList.any((final el) => el is CreateSSHKeyJob);
2021-09-02 19:32:07 +00:00
if (!isExistInJobList) {
newJobsList.add(job);
2021-09-15 14:37:22 +00:00
getIt<NavigationService>().showSnackBar('jobs.jobAdded'.tr());
2022-08-30 03:09:09 +00:00
emit(JobsStateWithJobs(newJobsList));
2021-08-29 15:02:51 +00:00
}
}
2021-12-06 18:31:19 +00:00
Future<void> rebootServer() async {
2022-08-30 03:09:09 +00:00
emit(JobsStateLoading());
2022-06-05 19:36:32 +00:00
final bool isSuccessful = await api.reboot();
2021-12-06 18:31:19 +00:00
if (isSuccessful) {
getIt<NavigationService>().showSnackBar('jobs.rebootSuccess'.tr());
} else {
getIt<NavigationService>().showSnackBar('jobs.rebootFailed'.tr());
}
2022-08-30 03:09:09 +00:00
emit(JobsStateEmpty());
2021-12-06 18:31:19 +00:00
}
Future<void> upgradeServer() async {
2022-08-30 03:09:09 +00:00
emit(JobsStateLoading());
2022-06-05 19:36:32 +00:00
final bool isPullSuccessful = await api.pullConfigurationUpdate();
final bool isSuccessful = await api.upgrade();
2021-12-06 18:31:19 +00:00
if (isSuccessful) {
if (!isPullSuccessful) {
getIt<NavigationService>().showSnackBar('jobs.configPullFailed'.tr());
} else {
getIt<NavigationService>().showSnackBar('jobs.upgradeSuccess'.tr());
}
} else {
getIt<NavigationService>().showSnackBar('jobs.upgradeFailed'.tr());
}
2022-08-30 03:09:09 +00:00
emit(JobsStateEmpty());
2021-12-06 18:31:19 +00:00
}
2021-06-08 18:52:44 +00:00
Future<void> applyAll() async {
2021-07-29 05:24:42 +00:00
if (state is JobsStateWithJobs) {
final List<ClientJob> jobs = (state as JobsStateWithJobs).clientJobList;
2022-08-30 03:09:09 +00:00
emit(JobsStateLoading());
2022-06-05 19:36:32 +00:00
bool hasServiceJobs = false;
for (final ClientJob job in jobs) {
// TODO: Rewrite to polymorphism
2021-07-29 05:24:42 +00:00
if (job is CreateUserJob) {
await usersCubit.createUser(job.user);
2021-12-20 15:25:31 +00:00
}
if (job is DeleteUserJob) {
await usersCubit.deleteUser(job.user);
2021-12-20 15:25:31 +00:00
}
if (job is ServiceToggleJob) {
hasServiceJobs = true;
await api.switchService(job.type.name, job.needToTurnOn);
2021-07-29 05:24:42 +00:00
}
2021-09-02 19:32:07 +00:00
if (job is CreateSSHKeyJob) {
2022-03-23 14:07:52 +00:00
await usersCubit.addSshKey(job.user, job.publicKey);
}
if (job is DeleteSSHKeyJob) {
await usersCubit.deleteSshKey(job.user, job.publicKey);
2021-09-02 19:32:07 +00:00
}
2022-09-18 20:25:26 +00:00
if (job is ResetUserPasswordJob) {
await usersCubit.changeUserPassword(job.user, job.user.password!);
}
if (job is RebuildServerJob) {
await upgradeServer();
}
2021-06-08 18:52:44 +00:00
}
2021-07-29 05:24:42 +00:00
2021-12-06 18:31:19 +00:00
await api.pullConfigurationUpdate();
2021-07-29 05:24:42 +00:00
await api.apply();
if (hasServiceJobs) {
await servicesCubit.load();
}
2021-07-29 05:24:42 +00:00
2022-08-30 03:09:09 +00:00
emit(JobsStateEmpty());
2021-06-08 18:52:44 +00:00
}
2021-05-25 21:53:54 +00:00
}
}