selfprivacy.org.app/lib/logic/models/job.dart

164 lines
4 KiB
Dart
Raw Normal View History

import 'package:easy_localization/easy_localization.dart';
2021-05-25 21:53:54 +00:00
import 'package:equatable/equatable.dart';
2021-06-08 18:52:44 +00:00
import 'package:flutter/material.dart';
import 'package:selfprivacy/logic/cubit/client_jobs/client_jobs_cubit.dart';
2024-01-31 05:14:23 +00:00
import 'package:selfprivacy/logic/models/hive/user.dart';
import 'package:selfprivacy/logic/models/service.dart';
2021-08-18 09:36:40 +00:00
import 'package:selfprivacy/utils/password_generator.dart';
2021-05-25 21:53:54 +00:00
2021-06-08 18:52:44 +00:00
@immutable
abstract class ClientJob extends Equatable {
ClientJob({
2021-05-25 21:53:54 +00:00
required this.title,
2022-06-05 19:36:32 +00:00
final String? id,
2021-08-18 09:36:40 +00:00
}) : id = id ?? StringGenerators.simpleId();
2021-05-25 21:53:54 +00:00
final String title;
final String id;
bool canAddTo(final List<ClientJob> jobs) => true;
void execute(final JobsCubit cubit);
2021-05-25 21:53:54 +00:00
@override
List<Object> get props => [id, title];
}
2021-06-08 18:52:44 +00:00
class RebuildServerJob extends ClientJob {
RebuildServerJob({
required super.title,
super.id,
});
@override
bool canAddTo(final List<ClientJob> jobs) =>
!jobs.any((final job) => job is RebuildServerJob);
@override
void execute(final JobsCubit cubit) async {
await cubit.upgradeServer();
}
}
class CreateUserJob extends ClientJob {
2021-06-08 18:52:44 +00:00
CreateUserJob({
required this.user,
}) : super(title: '${"jobs.create_user".tr()} ${user.login}');
2021-06-08 18:52:44 +00:00
final User user;
@override
void execute(final JobsCubit cubit) async {
await cubit.usersCubit.createUser(user);
}
2021-06-08 18:52:44 +00:00
@override
2021-08-29 15:02:51 +00:00
List<Object> get props => [id, title, user];
2021-06-08 18:52:44 +00:00
}
2021-08-18 10:44:46 +00:00
2022-09-18 20:25:26 +00:00
class ResetUserPasswordJob extends ClientJob {
ResetUserPasswordJob({
required this.user,
}) : super(title: '${"jobs.reset_user_password".tr()} ${user.login}');
2022-09-18 20:25:26 +00:00
final User user;
@override
void execute(final JobsCubit cubit) async {
await cubit.usersCubit.changeUserPassword(user, user.password!);
}
2022-09-18 20:25:26 +00:00
@override
List<Object> get props => [id, title, user];
}
class DeleteUserJob extends ClientJob {
2021-12-20 15:25:31 +00:00
DeleteUserJob({
required this.user,
}) : super(title: '${"jobs.delete_user".tr()} ${user.login}');
2021-12-20 15:25:31 +00:00
final User user;
@override
bool canAddTo(final List<ClientJob> jobs) => !jobs.any(
(final job) => job is DeleteUserJob && job.user.login == user.login,
);
@override
void execute(final JobsCubit cubit) async {
await cubit.usersCubit.deleteUser(user);
}
2021-12-20 15:25:31 +00:00
@override
List<Object> get props => [id, title, user];
}
class ServiceToggleJob extends ClientJob {
2022-01-25 17:00:47 +00:00
ServiceToggleJob({
required this.service,
2021-08-18 10:44:46 +00:00
required this.needToTurnOn,
2021-08-29 15:02:51 +00:00
}) : super(
2022-01-25 17:00:47 +00:00
title:
'${needToTurnOn ? "jobs.service_turn_on".tr() : "jobs.service_turn_off".tr()} ${service.displayName}',
2022-01-25 17:00:47 +00:00
);
2021-08-18 10:44:46 +00:00
final bool needToTurnOn;
final Service service;
@override
bool canAddTo(final List<ClientJob> jobs) => !jobs.any(
(final job) => job is ServiceToggleJob && job.service.id == service.id,
);
@override
void execute(final JobsCubit cubit) async {
await cubit.api.switchService(service.id, needToTurnOn);
}
@override
List<Object> get props => [...super.props, service];
2021-08-18 10:44:46 +00:00
}
2021-09-02 19:32:07 +00:00
class CreateSSHKeyJob extends ClientJob {
CreateSSHKeyJob({
required this.user,
required this.publicKey,
2022-05-24 18:55:39 +00:00
}) : super(title: 'jobs.create_ssh_key'.tr(args: [user.login]));
final User user;
final String publicKey;
2021-09-02 19:32:07 +00:00
@override
void execute(final JobsCubit cubit) async {
await cubit.usersCubit.addSshKey(user, publicKey);
}
2021-09-02 19:32:07 +00:00
@override
List<Object> get props => [id, title, user, publicKey];
}
class DeleteSSHKeyJob extends ClientJob {
DeleteSSHKeyJob({
required this.user,
required this.publicKey,
2022-05-24 18:55:39 +00:00
}) : super(title: 'jobs.delete_ssh_key'.tr(args: [user.login]));
final User user;
final String publicKey;
@override
bool canAddTo(final List<ClientJob> jobs) => !jobs.any(
(final job) =>
job is DeleteSSHKeyJob &&
job.publicKey == publicKey &&
job.user.login == user.login,
);
@override
void execute(final JobsCubit cubit) async {
await cubit.usersCubit.deleteSshKey(user, publicKey);
}
@override
List<Object> get props => [id, title, user, publicKey];
2021-09-02 19:32:07 +00:00
}