selfprivacy.org.app/lib/logic/cubit/jobs/jobs_cubit.dart

131 lines
4 KiB
Dart
Raw Normal View History

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';
2021-06-08 18:52:44 +00:00
import 'package:selfprivacy/logic/api_maps/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';
part 'jobs_state.dart';
class JobsCubit extends Cubit<JobsState> {
2021-08-29 13:54:28 +00:00
JobsCubit({
required this.usersCubit,
required this.servicesCubit,
}) : super(JobsStateEmpty());
2021-05-25 21:53:54 +00:00
2021-06-08 18:52:44 +00:00
final 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(Job job) {
2021-07-29 05:24:42 +00:00
var newJobsList = <Job>[];
if (state is JobsStateWithJobs) {
newJobsList.addAll((state as JobsStateWithJobs).jobList);
}
newJobsList.add(job);
2021-09-15 14:37:22 +00:00
getIt<NavigationService>().showSnackBar('jobs.jobAdded'.tr());
2021-07-29 05:24:42 +00:00
emit(JobsStateWithJobs(newJobsList));
2021-05-25 21:53:54 +00:00
}
void removeJob(String id) {
2021-07-29 05:24:42 +00:00
final newState = (state as JobsStateWithJobs).removeById(id);
2021-05-25 21:53:54 +00:00
emit(newState);
}
2022-01-25 17:00:47 +00:00
void createOrRemoveServiceToggleJob(ToggleJob job) {
2021-08-29 15:02:51 +00:00
var newJobsList = <Job>[];
if (state is JobsStateWithJobs) {
newJobsList.addAll((state as JobsStateWithJobs).jobList);
}
var needToRemoveJob =
newJobsList.any((el) => el is ServiceToggleJob && el.type == job.type);
if (needToRemoveJob) {
var removingJob = newJobsList
.firstWhere(((el) => el is ServiceToggleJob && el.type == job.type));
removeJob(removingJob.id);
} else {
newJobsList.add(job);
2021-09-15 14:37:22 +00:00
getIt<NavigationService>().showSnackBar('jobs.jobAdded'.tr());
2021-09-02 19:32:07 +00:00
emit(JobsStateWithJobs(newJobsList));
}
}
void createShhJobIfNotExist(CreateSSHKeyJob job) {
var newJobsList = <Job>[];
if (state is JobsStateWithJobs) {
newJobsList.addAll((state as JobsStateWithJobs).jobList);
}
var isExistInJobList = newJobsList.any((el) => el is CreateSSHKeyJob);
if (!isExistInJobList) {
newJobsList.add(job);
2021-09-15 14:37:22 +00:00
getIt<NavigationService>().showSnackBar('jobs.jobAdded'.tr());
2021-08-29 15:02:51 +00:00
emit(JobsStateWithJobs(newJobsList));
}
}
2021-12-06 18:31:19 +00:00
Future<void> rebootServer() async {
emit(JobsStateLoading());
2021-12-06 18:31:19 +00:00
final isSuccessful = await api.reboot();
if (isSuccessful) {
getIt<NavigationService>().showSnackBar('jobs.rebootSuccess'.tr());
} else {
getIt<NavigationService>().showSnackBar('jobs.rebootFailed'.tr());
}
emit(JobsStateEmpty());
2021-12-06 18:31:19 +00:00
}
Future<void> upgradeServer() async {
emit(JobsStateLoading());
2021-12-06 18:31:19 +00:00
final isPullSuccessful = await api.pullConfigurationUpdate();
final isSuccessful = await api.upgrade();
if (isSuccessful) {
if (!isPullSuccessful) {
getIt<NavigationService>().showSnackBar('jobs.configPullFailed'.tr());
} else {
getIt<NavigationService>().showSnackBar('jobs.upgradeSuccess'.tr());
}
} else {
getIt<NavigationService>().showSnackBar('jobs.upgradeFailed'.tr());
}
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) {
var jobs = (state as JobsStateWithJobs).jobList;
emit(JobsStateLoading());
var hasServiceJobs = false;
2021-07-29 05:24:42 +00:00
for (var job in jobs) {
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;
2021-08-29 15:02:51 +00:00
await api.switchService(job.type, 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
}
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
emit(JobsStateEmpty());
2021-06-08 18:52:44 +00:00
}
2021-05-25 21:53:54 +00:00
}
}