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

136 lines
4.3 KiB
Dart
Raw Normal View History

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-09-15 14:37:22 +00:00
import 'package:selfprivacy/logic/get_it/ssh.dart';
2021-08-29 15:02:51 +00:00
import 'package:selfprivacy/logic/models/job.dart';
2021-05-25 21:53:54 +00:00
import 'package:equatable/equatable.dart';
2021-07-29 05:24:42 +00:00
import 'package:selfprivacy/logic/models/user.dart';
2021-05-25 21:53:54 +00:00
export 'package:provider/provider.dart';
2021-08-29 09:50:24 +00:00
import 'package:easy_localization/easy_localization.dart';
2021-05-25 21:53:54 +00:00
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);
}
2021-09-02 19:32:07 +00:00
void createOrRemoveServiceToggleJob(ServiceToggleJob 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 newUsers = <User>[];
var hasServiceJobs = false;
2021-07-29 05:24:42 +00:00
for (var job in jobs) {
if (job is CreateUserJob) {
newUsers.add(job.user);
await api.createUser(job.user);
2021-12-20 15:25:31 +00:00
}
if (job is DeleteUserJob) {
final deleted = await api.deleteUser(job.user);
if (deleted) usersCubit.remove(job.user);
}
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) {
await getIt<SSHModel>().generateKeys();
api.sendSsh(getIt<SSHModel>().savedPubKey!);
}
2021-06-08 18:52:44 +00:00
}
2021-07-29 05:24:42 +00:00
usersCubit.addUsers(newUsers);
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());
getIt<NavigationService>().navigator!.pop();
2021-06-08 18:52:44 +00:00
}
2021-05-25 21:53:54 +00:00
}
}