more refactors
This commit is contained in:
@@ -4,7 +4,7 @@ import 'package:immich_mobile/domain/models/app_setting.model.dart';
|
||||
class AppSettingService {
|
||||
final IStoreRepository _store;
|
||||
|
||||
const AppSettingService(this._store);
|
||||
const AppSettingService({required IStoreRepository store}) : _store = store;
|
||||
|
||||
Future<T> get<T>(AppSetting<T> setting) async {
|
||||
final value = await _store.tryGet(setting.storeKey);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:immich_mobile/domain/interfaces/api/sync_api.interface.dart';
|
||||
import 'package:immich_mobile/domain/interfaces/asset.interface.dart';
|
||||
import 'package:immich_mobile/domain/interfaces/database.interface.dart';
|
||||
import 'package:immich_mobile/domain/models/asset.model.dart';
|
||||
@@ -8,10 +9,8 @@ import 'package:immich_mobile/domain/models/user.model.dart';
|
||||
import 'package:immich_mobile/service_locator.dart';
|
||||
import 'package:immich_mobile/utils/collection_util.dart';
|
||||
import 'package:immich_mobile/utils/constants/globals.dart';
|
||||
import 'package:immich_mobile/utils/immich_api_client.dart';
|
||||
import 'package:immich_mobile/utils/isolate_helper.dart';
|
||||
import 'package:immich_mobile/utils/mixins/log.mixin.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
class AssetSyncService with LogMixin {
|
||||
const AssetSyncService();
|
||||
@@ -36,9 +35,9 @@ class AssetSyncService with LogMixin {
|
||||
int? limit,
|
||||
}) async {
|
||||
try {
|
||||
final syncClient = di<ImApiClient>().getSyncApi();
|
||||
final db = di<IDatabaseRepository>();
|
||||
final assetRepo = di<IAssetRepository>();
|
||||
final syncApiRepo = di<ISyncApiRepository>();
|
||||
|
||||
final chunkSize = limit ?? kFullSyncChunkSize;
|
||||
final updatedTill = updatedUtil ?? DateTime.now().toUtc();
|
||||
@@ -50,18 +49,16 @@ class AssetSyncService with LogMixin {
|
||||
"Requesting more chunks from lastId - ${lastAssetId ?? "<initial_fetch>"}",
|
||||
);
|
||||
|
||||
final assets = await syncClient.getFullSyncForUser(AssetFullSyncDto(
|
||||
final assetsFromServer = await syncApiRepo.getFullSyncForUser(
|
||||
limit: chunkSize,
|
||||
updatedUntil: updatedTill,
|
||||
lastId: lastAssetId,
|
||||
userId: user.id,
|
||||
));
|
||||
if (assets == null) {
|
||||
);
|
||||
if (assetsFromServer == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
final assetsFromServer = assets.map(Asset.remote).toList();
|
||||
|
||||
await db.txn(() async {
|
||||
final assetsInDb =
|
||||
await assetRepo.getForHashes(assetsFromServer.map((a) => a.hash));
|
||||
@@ -73,8 +70,8 @@ class AssetSyncService with LogMixin {
|
||||
);
|
||||
});
|
||||
|
||||
lastAssetId = assets.lastOrNull?.id;
|
||||
if (assets.length != chunkSize) break;
|
||||
lastAssetId = assetsFromServer.lastOrNull?.remoteId;
|
||||
if (assetsFromServer.length != chunkSize) break;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -4,28 +4,31 @@ import 'dart:io';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_web_auth_2/flutter_web_auth_2.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:immich_mobile/domain/interfaces/api/authentication_api.interface.dart';
|
||||
import 'package:immich_mobile/domain/interfaces/api/server_api.interface.dart';
|
||||
import 'package:immich_mobile/domain/interfaces/api/user_api.interface.dart';
|
||||
import 'package:immich_mobile/domain/interfaces/store.interface.dart';
|
||||
import 'package:immich_mobile/domain/models/store.model.dart';
|
||||
import 'package:immich_mobile/domain/services/user.service.dart';
|
||||
import 'package:immich_mobile/service_locator.dart';
|
||||
import 'package:immich_mobile/utils/immich_api_client.dart';
|
||||
import 'package:immich_mobile/utils/mixins/log.mixin.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
// Cannot add dependency repos to constructor as this requires the newly registered API client from login
|
||||
// and not a cached repos from DI
|
||||
class LoginService with LogMixin {
|
||||
const LoginService();
|
||||
|
||||
Future<bool> isEndpointAvailable(Uri uri, {ImApiClient? client}) async {
|
||||
Future<bool> isEndpointAvailable(Uri uri) async {
|
||||
String baseUrl = uri.toString();
|
||||
|
||||
if (!baseUrl.endsWith('/api')) {
|
||||
baseUrl += '/api';
|
||||
}
|
||||
|
||||
final serverAPI =
|
||||
client?.getServerApi() ?? ImApiClient(endpoint: baseUrl).getServerApi();
|
||||
await ServiceLocator.registerApiClient(baseUrl);
|
||||
|
||||
try {
|
||||
await serverAPI.pingServer();
|
||||
await di<IServerApiRepository>().pingServer();
|
||||
} catch (e) {
|
||||
log.e("Exception occured while validating endpoint", e);
|
||||
return false;
|
||||
@@ -61,12 +64,7 @@ class LoginService with LogMixin {
|
||||
|
||||
Future<String?> passwordLogin(String email, String password) async {
|
||||
try {
|
||||
final loginResponse =
|
||||
await di<ImApiClient>().getAuthenticationApi().login(
|
||||
LoginCredentialDto(email: email, password: password),
|
||||
);
|
||||
|
||||
return loginResponse?.accessToken;
|
||||
return await di<IAuthenticationApiRepository>().login(email, password);
|
||||
} catch (e, s) {
|
||||
log.e("Exception occured while performing password login", e, s);
|
||||
}
|
||||
@@ -75,16 +73,14 @@ class LoginService with LogMixin {
|
||||
|
||||
Future<String?> oAuthLogin() async {
|
||||
const String oAuthCallbackSchema = 'app.immich';
|
||||
|
||||
final oAuthApi = di<ImApiClient>().getOAuthApi();
|
||||
final authApi = di<IAuthenticationApiRepository>();
|
||||
|
||||
try {
|
||||
final oAuthUrl = await oAuthApi.startOAuth(
|
||||
OAuthConfigDto(redirectUri: "$oAuthCallbackSchema:/"),
|
||||
final oAuthUrl = await authApi.startOAuth(
|
||||
redirectUri: "$oAuthCallbackSchema:/",
|
||||
);
|
||||
|
||||
final oAuthUrlRes = oAuthUrl?.url;
|
||||
if (oAuthUrlRes == null) {
|
||||
if (oAuthUrl == null) {
|
||||
log.e(
|
||||
"oAuth Server URL not available. Kindly ensure oAuth login is enabled in the server",
|
||||
);
|
||||
@@ -92,15 +88,11 @@ class LoginService with LogMixin {
|
||||
}
|
||||
|
||||
final oAuthCallbackUrl = await FlutterWebAuth2.authenticate(
|
||||
url: oAuthUrlRes,
|
||||
url: oAuthUrl,
|
||||
callbackUrlScheme: oAuthCallbackSchema,
|
||||
);
|
||||
|
||||
final loginResponse = await oAuthApi.finishOAuth(
|
||||
OAuthCallbackDto(url: oAuthCallbackUrl),
|
||||
);
|
||||
|
||||
return loginResponse?.accessToken;
|
||||
return await authApi.finishOAuth(oAuthCallbackUrl);
|
||||
} catch (e) {
|
||||
log.e("Exception occured while performing oauth login", e);
|
||||
}
|
||||
@@ -114,8 +106,7 @@ class LoginService with LogMixin {
|
||||
return false;
|
||||
}
|
||||
|
||||
ServiceLocator.registerApiClient(serverEndpoint);
|
||||
ServiceLocator.registerPostValidationServices();
|
||||
await ServiceLocator.registerApiClient(serverEndpoint);
|
||||
ServiceLocator.registerPostGlobalStates();
|
||||
|
||||
final accessToken =
|
||||
@@ -124,10 +115,10 @@ class LoginService with LogMixin {
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Set token to interceptor
|
||||
// Set token to interceptor
|
||||
await di<ImApiClient>().init(accessToken: accessToken);
|
||||
|
||||
final user = await di<UserService>().getMyUser().timeout(
|
||||
final user = await di<IUserApiRepository>().getMyUser().timeout(
|
||||
const Duration(seconds: 10),
|
||||
// ignore: function-always-returns-null
|
||||
onTimeout: () {
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
import 'package:immich_mobile/domain/models/server-info/server_config.model.dart';
|
||||
import 'package:immich_mobile/domain/models/server-info/server_features.model.dart';
|
||||
import 'package:immich_mobile/utils/mixins/log.mixin.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
class ServerInfoService with LogMixin {
|
||||
final ServerApi _serverInfo;
|
||||
|
||||
const ServerInfoService(this._serverInfo);
|
||||
|
||||
Future<ServerFeatures?> getServerFeatures() async {
|
||||
try {
|
||||
final dto = await _serverInfo.getServerFeatures();
|
||||
if (dto != null) {
|
||||
return ServerFeatures.fromDto(dto);
|
||||
}
|
||||
} catch (e, s) {
|
||||
log.e("Error while fetching server features", e, s);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<ServerConfig?> getServerConfig() async {
|
||||
try {
|
||||
final dto = await _serverInfo.getServerConfig();
|
||||
if (dto != null) {
|
||||
return ServerConfig.fromDto(dto);
|
||||
}
|
||||
} catch (e, s) {
|
||||
log.e("Error while fetching server config", e, s);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
import 'package:immich_mobile/domain/models/user.model.dart';
|
||||
import 'package:immich_mobile/utils/mixins/log.mixin.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
class UserService with LogMixin {
|
||||
final UsersApi _userApi;
|
||||
|
||||
const UserService(this._userApi);
|
||||
|
||||
Future<User?> getMyUser() async {
|
||||
try {
|
||||
final [
|
||||
userDto as UserAdminResponseDto?,
|
||||
preferencesDto as UserPreferencesResponseDto?
|
||||
] = await Future.wait([
|
||||
_userApi.getMyUser(),
|
||||
_userApi.getMyPreferences(),
|
||||
]);
|
||||
|
||||
if (userDto == null) {
|
||||
log.e("Cannot fetch my user.");
|
||||
return null;
|
||||
}
|
||||
|
||||
return User.fromAdminDto(userDto, preferencesDto);
|
||||
} catch (e, s) {
|
||||
log.e("Error while fetching my user", e, s);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user