mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-08 17:11:14 +00:00
Merge pull request 'refactor(job): Make jobs execution polymorphic instead of relied on plain switch' (#133) from jobs-polymorphism into master
Reviewed-on: https://git.selfprivacy.org/kherel/selfprivacy.org.app/pulls/133 Reviewed-by: Inex Code <inex.code@selfprivacy.org>
This commit is contained in:
commit
e619d6351f
|
@ -103,39 +103,14 @@ class JobsCubit extends Cubit<JobsState> {
|
|||
if (state is JobsStateWithJobs) {
|
||||
final List<ClientJob> jobs = (state as JobsStateWithJobs).clientJobList;
|
||||
emit(JobsStateLoading());
|
||||
bool hasServiceJobs = false;
|
||||
|
||||
for (final ClientJob job in jobs) {
|
||||
// TODO: Rewrite to polymorphism
|
||||
if (job is CreateUserJob) {
|
||||
await usersCubit.createUser(job.user);
|
||||
}
|
||||
if (job is DeleteUserJob) {
|
||||
await usersCubit.deleteUser(job.user);
|
||||
}
|
||||
if (job is ServiceToggleJob) {
|
||||
hasServiceJobs = true;
|
||||
await api.switchService(job.service.id, job.needToTurnOn);
|
||||
}
|
||||
if (job is CreateSSHKeyJob) {
|
||||
await usersCubit.addSshKey(job.user, job.publicKey);
|
||||
}
|
||||
if (job is DeleteSSHKeyJob) {
|
||||
await usersCubit.deleteSshKey(job.user, job.publicKey);
|
||||
}
|
||||
if (job is ResetUserPasswordJob) {
|
||||
await usersCubit.changeUserPassword(job.user, job.user.password!);
|
||||
}
|
||||
if (job is RebuildServerJob) {
|
||||
await upgradeServer();
|
||||
}
|
||||
job.execute(this);
|
||||
}
|
||||
|
||||
await api.pullConfigurationUpdate();
|
||||
await api.apply();
|
||||
|
||||
if (hasServiceJobs) {
|
||||
await servicesCubit.load();
|
||||
}
|
||||
await servicesCubit.load();
|
||||
|
||||
emit(JobsStateEmpty());
|
||||
}
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
||||
import 'package:selfprivacy/logic/cubit/client_jobs/client_jobs_cubit.dart';
|
||||
import 'package:selfprivacy/logic/models/service.dart';
|
||||
import 'package:selfprivacy/utils/password_generator.dart';
|
||||
|
||||
import 'package:selfprivacy/logic/models/hive/user.dart';
|
||||
|
||||
@immutable
|
||||
class ClientJob extends Equatable {
|
||||
abstract class ClientJob extends Equatable {
|
||||
ClientJob({
|
||||
required this.title,
|
||||
final String? id,
|
||||
|
@ -17,6 +17,8 @@ class ClientJob extends Equatable {
|
|||
final String title;
|
||||
final String id;
|
||||
|
||||
void execute(final JobsCubit cubit);
|
||||
|
||||
@override
|
||||
List<Object> get props => [id, title];
|
||||
}
|
||||
|
@ -26,6 +28,11 @@ class RebuildServerJob extends ClientJob {
|
|||
required final super.title,
|
||||
final super.id,
|
||||
});
|
||||
|
||||
@override
|
||||
void execute(final JobsCubit cubit) async {
|
||||
await cubit.upgradeServer();
|
||||
}
|
||||
}
|
||||
|
||||
class CreateUserJob extends ClientJob {
|
||||
|
@ -35,6 +42,11 @@ class CreateUserJob extends ClientJob {
|
|||
|
||||
final User user;
|
||||
|
||||
@override
|
||||
void execute(final JobsCubit cubit) async {
|
||||
await cubit.usersCubit.createUser(user);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object> get props => [id, title, user];
|
||||
}
|
||||
|
@ -46,6 +58,11 @@ class ResetUserPasswordJob extends ClientJob {
|
|||
|
||||
final User user;
|
||||
|
||||
@override
|
||||
void execute(final JobsCubit cubit) async {
|
||||
await cubit.usersCubit.changeUserPassword(user, user.password!);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object> get props => [id, title, user];
|
||||
}
|
||||
|
@ -57,11 +74,16 @@ class DeleteUserJob extends ClientJob {
|
|||
|
||||
final User user;
|
||||
|
||||
@override
|
||||
void execute(final JobsCubit cubit) async {
|
||||
await cubit.usersCubit.deleteUser(user);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object> get props => [id, title, user];
|
||||
}
|
||||
|
||||
class ToggleJob extends ClientJob {
|
||||
abstract class ToggleJob extends ClientJob {
|
||||
ToggleJob({
|
||||
required final this.service,
|
||||
required final super.title,
|
||||
|
@ -82,6 +104,11 @@ class ServiceToggleJob extends ToggleJob {
|
|||
'${needToTurnOn ? "jobs.service_turn_on".tr() : "jobs.service_turn_off".tr()} ${service.displayName}',
|
||||
);
|
||||
|
||||
@override
|
||||
void execute(final JobsCubit cubit) async {
|
||||
await cubit.api.switchService(service.id, needToTurnOn);
|
||||
}
|
||||
|
||||
final bool needToTurnOn;
|
||||
}
|
||||
|
||||
|
@ -94,6 +121,11 @@ class CreateSSHKeyJob extends ClientJob {
|
|||
final User user;
|
||||
final String publicKey;
|
||||
|
||||
@override
|
||||
void execute(final JobsCubit cubit) async {
|
||||
await cubit.usersCubit.addSshKey(user, publicKey);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object> get props => [id, title, user, publicKey];
|
||||
}
|
||||
|
@ -107,6 +139,11 @@ class DeleteSSHKeyJob extends ClientJob {
|
|||
final User user;
|
||||
final String publicKey;
|
||||
|
||||
@override
|
||||
void execute(final JobsCubit cubit) async {
|
||||
await cubit.usersCubit.deleteSshKey(user, publicKey);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object> get props => [id, title, user, publicKey];
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue