preliminary auto-login

This commit is contained in:
shenlong-tanwen
2024-09-14 23:05:58 +05:30
parent 6fce1ebb79
commit f1dcfbc594
13 changed files with 193 additions and 63 deletions
@@ -14,7 +14,11 @@ import 'database.repository.drift.dart';
@DriftDatabase(tables: [Logs, Store, LocalAlbum, Asset, User])
class DriftDatabaseRepository extends $DriftDatabaseRepository {
DriftDatabaseRepository([QueryExecutor? executor])
: super(executor ?? driftDatabase(name: 'db'));
: super(executor ??
driftDatabase(
name: 'db',
native: const DriftNativeOptions(shareAcrossIsolates: true),
));
@override
int get schemaVersion => 1;
@@ -4,6 +4,9 @@ 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/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_context.mixin.dart';
@@ -103,4 +106,32 @@ class LoginService with LogContext {
}
return null;
}
Future<bool> tryLoginFromSplash() async {
final serverEndpoint =
await di<IStoreRepository>().tryGet(StoreKey.serverEndpoint);
if (serverEndpoint == null) {
return false;
}
ServiceLocator.registerApiClient(serverEndpoint);
ServiceLocator.registerPostValidationServices();
ServiceLocator.registerPostGlobalStates();
final accessToken =
await di<IStoreRepository>().tryGet(StoreKey.accessToken);
if (accessToken == null) {
return false;
}
/// Set token to interceptor
await di<ImmichApiClient>().init(accessToken: accessToken);
final user = await di<UserService>().getMyUser();
if (user == null) {
return false;
}
return true;
}
}
+37 -41
View File
@@ -1,4 +1,5 @@
import 'package:drift/isolate.dart';
import 'dart:async';
import 'package:immich_mobile/domain/interfaces/asset.interface.dart';
import 'package:immich_mobile/domain/models/asset.model.dart';
import 'package:immich_mobile/domain/models/user.model.dart';
@@ -12,56 +13,51 @@ import 'package:logging/logging.dart';
import 'package:openapi/api.dart';
class SyncService with LogContext {
final DriftDatabaseRepository _db;
SyncService(this._db);
SyncService();
Future<bool> doFullSyncForUserDrift(
User user, {
DateTime? updatedUtil,
int? limit,
}) async {
final helper = IsolateHelper()..preIsolateHandling();
try {
await _db.computeWithDatabase(
connect: (connection) => DriftDatabaseRepository(connection),
computation: (database) async {
helper.postIsolateHandling(database: database);
final logger = Logger("SyncService <Isolate>");
final syncClient = di<ImmichApiClient>().getSyncApi();
return await IsolateHelper.run(() async {
try {
final logger = Logger("SyncService <Isolate>");
final syncClient = di<ImmichApiClient>().getSyncApi();
final chunkSize = limit ?? kFullSyncChunkSize;
final updatedTill = updatedUtil ?? DateTime.now().toUtc();
updatedUtil ??= DateTime.now().toUtc();
String? lastAssetId;
final chunkSize = limit ?? kFullSyncChunkSize;
final updatedTill = updatedUtil ?? DateTime.now().toUtc();
updatedUtil ??= DateTime.now().toUtc();
String? lastAssetId;
while (true) {
logger.info(
"Requesting more chunks from lastId - ${lastAssetId ?? "<initial_fetch>"}",
);
while (true) {
logger.info(
"Requesting more chunks from lastId - ${lastAssetId ?? "<initial_fetch>"}",
);
final assets = await syncClient.getFullSyncForUser(AssetFullSyncDto(
limit: chunkSize,
updatedUntil: updatedTill,
lastId: lastAssetId,
userId: user.id,
));
if (assets == null) {
break;
}
await di<IAssetRepository>().addAll(assets.map(Asset.remote));
lastAssetId = assets.lastOrNull?.id;
if (assets.length != chunkSize) break;
final assets = await syncClient.getFullSyncForUser(AssetFullSyncDto(
limit: chunkSize,
updatedUntil: updatedTill,
lastId: lastAssetId,
userId: user.id,
));
if (assets == null) {
break;
}
return true;
},
);
} catch (e, s) {
log.severe("Error performing full sync for user - ${user.name}", e, s);
}
return false;
await di<IAssetRepository>().addAll(assets.map(Asset.remote));
lastAssetId = assets.lastOrNull?.id;
if (assets.length != chunkSize) break;
}
return true;
} catch (e, s) {
log.severe("Error performing full sync for user - ${user.name}", e, s);
} finally {
await di<DriftDatabaseRepository>().close();
}
return false;
});
}
}
@@ -9,13 +9,19 @@ class UserService with LogContext {
Future<User?> getMyUser() async {
try {
final userDto = await _userApi.getMyUser();
final [
userDto as UserAdminResponseDto?,
preferencesDto as UserPreferencesResponseDto?
] = await Future.wait([
_userApi.getMyUser(),
_userApi.getMyPreferences(),
]);
if (userDto == null) {
log.severe("Cannot fetch my user.");
return null;
}
final preferencesDto = await _userApi.getMyPreferences();
return User.fromAdminDto(userDto, preferencesDto);
} catch (e, s) {
log.severe("Error while fetching my user", e, s);