Files
umbrix/lib/data/repository/profiles_repository_impl.dart

184 lines
4.9 KiB
Dart
Raw Normal View History

2023-07-06 17:18:41 +03:30
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:fpdart/fpdart.dart';
import 'package:hiddify/data/local/dao/dao.dart';
import 'package:hiddify/data/repository/exception_handlers.dart';
2023-07-26 16:42:31 +03:30
import 'package:hiddify/domain/enums.dart';
2023-07-06 17:18:41 +03:30
import 'package:hiddify/domain/profiles/profiles.dart';
2023-08-19 22:27:23 +03:30
import 'package:hiddify/domain/singbox/singbox.dart';
2023-07-06 17:18:41 +03:30
import 'package:hiddify/services/files_editor_service.dart';
import 'package:hiddify/utils/utils.dart';
import 'package:meta/meta.dart';
2023-07-26 14:17:11 +03:30
import 'package:uuid/uuid.dart';
2023-07-06 17:18:41 +03:30
class ProfilesRepositoryImpl
with ExceptionHandler, InfraLogger
implements ProfilesRepository {
ProfilesRepositoryImpl({
required this.profilesDao,
required this.filesEditor,
2023-08-19 22:27:23 +03:30
required this.singbox,
2023-07-06 17:18:41 +03:30
required this.dio,
});
final ProfilesDao profilesDao;
final FilesEditorService filesEditor;
2023-08-19 22:27:23 +03:30
final SingboxFacade singbox;
2023-07-06 17:18:41 +03:30
final Dio dio;
@override
TaskEither<ProfileFailure, Profile?> get(String id) {
return TaskEither.tryCatch(
() => profilesDao.getById(id),
ProfileUnexpectedFailure.new,
);
}
@override
Stream<Either<ProfileFailure, Profile?>> watchActiveProfile() {
return profilesDao
.watchActiveProfile()
.handleExceptions(ProfileUnexpectedFailure.new);
}
@override
Stream<Either<ProfileFailure, bool>> watchHasAnyProfile() {
return profilesDao
.watchProfileCount()
.map((event) => event != 0)
.handleExceptions(ProfileUnexpectedFailure.new);
}
@override
2023-07-26 16:42:31 +03:30
Stream<Either<ProfileFailure, List<Profile>>> watchAll({
ProfilesSort sort = ProfilesSort.lastUpdate,
SortMode mode = SortMode.ascending,
}) {
2023-07-06 17:18:41 +03:30
return profilesDao
2023-07-26 16:42:31 +03:30
.watchAll(sort: sort, mode: mode)
2023-07-06 17:18:41 +03:30
.handleExceptions(ProfileUnexpectedFailure.new);
}
2023-07-26 14:17:11 +03:30
@override
TaskEither<ProfileFailure, Unit> addByUrl(
String url, {
bool markAsActive = false,
}) {
return exceptionHandler(
() async {
final profileId = const Uuid().v4();
return fetch(url, profileId)
.flatMap(
(profile) => TaskEither(
() async {
await profilesDao.create(
profile.copyWith(
id: profileId,
active: markAsActive,
),
);
return right(unit);
},
),
)
.run();
},
ProfileUnexpectedFailure.new,
);
}
2023-07-06 17:18:41 +03:30
@override
TaskEither<ProfileFailure, Unit> add(Profile baseProfile) {
return exceptionHandler(
() async {
return fetch(baseProfile.url, baseProfile.id)
.flatMap(
2023-07-26 14:17:11 +03:30
(remoteProfile) => TaskEither(() async {
2023-07-06 17:18:41 +03:30
await profilesDao.create(
baseProfile.copyWith(
2023-07-26 14:17:11 +03:30
subInfo: remoteProfile.subInfo,
extra: remoteProfile.extra,
2023-07-06 17:18:41 +03:30
lastUpdate: DateTime.now(),
),
);
return right(unit);
}),
)
.run();
},
ProfileUnexpectedFailure.new,
);
}
@override
TaskEither<ProfileFailure, Unit> update(Profile baseProfile) {
return exceptionHandler(
() async {
return fetch(baseProfile.url, baseProfile.id)
.flatMap(
2023-07-26 14:17:11 +03:30
(remoteProfile) => TaskEither(() async {
2023-07-06 17:18:41 +03:30
await profilesDao.edit(
baseProfile.copyWith(
2023-07-26 14:17:11 +03:30
subInfo: remoteProfile.subInfo,
extra: remoteProfile.extra,
2023-07-06 17:18:41 +03:30
lastUpdate: DateTime.now(),
),
);
return right(unit);
}),
)
.run();
},
ProfileUnexpectedFailure.new,
);
}
@override
TaskEither<ProfileFailure, Unit> setAsActive(String id) {
return TaskEither.tryCatch(
() async {
await profilesDao.setAsActive(id);
return unit;
},
ProfileUnexpectedFailure.new,
);
}
@override
TaskEither<ProfileFailure, Unit> delete(String id) {
return TaskEither.tryCatch(
() async {
await profilesDao.removeById(id);
await filesEditor.deleteConfig(id);
return unit;
},
ProfileUnexpectedFailure.new,
);
}
@visibleForTesting
2023-07-26 14:17:11 +03:30
TaskEither<ProfileFailure, Profile> fetch(
2023-07-06 17:18:41 +03:30
String url,
String fileName,
) {
return TaskEither(
() async {
final path = filesEditor.configPath(fileName);
final response = await dio.download(url, path);
2023-08-19 22:27:23 +03:30
final parseResult = await singbox.parseConfig(path).run();
return parseResult.fold(
(l) async {
await File(path).delete();
return left(ProfileFailure.invalidConfig(l.msg));
},
(_) {
final profile = Profile.fromResponse(url, response.headers.map);
return right(profile);
},
);
2023-07-06 17:18:41 +03:30
},
);
}
}