more fixes

This commit is contained in:
shenlong-tanwen
2025-09-04 18:28:02 +05:30
parent 254ca4a13d
commit 9cf5d83707
6 changed files with 41 additions and 29 deletions
@@ -1,5 +1,3 @@
import 'dart:convert';
enum RemoteAssetMetadataKey {
mobileApp("mobile-app");
@@ -8,25 +6,30 @@ enum RemoteAssetMetadataKey {
const RemoteAssetMetadataKey(this.key);
}
abstract class RemoteAssetMetadataValue {
const RemoteAssetMetadataValue();
Map<String, dynamic> toJson();
}
class RemoteAssetMetadataItem {
final RemoteAssetMetadataKey key;
final Object value;
final RemoteAssetMetadataValue value;
const RemoteAssetMetadataItem({required this.key, required this.value});
Map<String, Object?> toMap() {
Map<String, Object?> toJson() {
return {'key': key.key, 'value': value};
}
String toJson() => json.encode(toMap());
}
class RemoteAssetMobileAppMetadata {
class RemoteAssetMobileAppMetadata extends RemoteAssetMetadataValue {
final String? cloudId;
const RemoteAssetMobileAppMetadata({this.cloudId});
Map<String, Object?> toMap() {
@override
Map<String, dynamic> toJson() {
final map = <String, Object?>{};
if (cloudId != null) {
map["iCloudId"] = cloudId;
@@ -34,6 +37,4 @@ class RemoteAssetMobileAppMetadata {
return map;
}
String toJson() => json.encode(toMap());
}
+18 -5
View File
@@ -7,6 +7,8 @@ import 'package:immich_mobile/platform/native_sync_api.g.dart';
import 'package:immich_mobile/providers/api.provider.dart';
import 'package:immich_mobile/providers/infrastructure/db.provider.dart';
import 'package:immich_mobile/providers/infrastructure/sync.provider.dart';
import 'package:immich_mobile/providers/user.provider.dart';
import 'package:logging/logging.dart';
// ignore: import_rule_openapi
import 'package:openapi/api.dart';
@@ -19,14 +21,24 @@ Future<void> migrateCloudIds(ProviderContainer ref) async {
await ref.read(syncStreamServiceProvider).sync();
// Fetch the mapping for backed up assets that have a cloud ID locally but do not have a cloud ID on the server
final mappingsToUpdate = await _fetchCloudIdMappings(db);
final currentUser = ref.read(currentUserProvider);
if (currentUser == null) {
Logger('migrateCloudIds').warning('Current user is null. Aborting cloudId migration.');
return;
}
final mappingsToUpdate = await _fetchCloudIdMappings(db, currentUser.id);
final assetApi = ref.read(apiServiceProvider).assetsApi;
for (final mapping in mappingsToUpdate) {
final mobileMeta = AssetMetadataUpsertItemDto(
key: AssetMetadataKey.mobileApp,
value: RemoteAssetMobileAppMetadata(cloudId: mapping.cloudId).toMap(),
value: RemoteAssetMobileAppMetadata(cloudId: mapping.cloudId),
);
await assetApi.updateAssetMetadata(mapping.assetId, AssetMetadataUpsertDto(items: [mobileMeta]));
try {
await assetApi.updateAssetMetadata(mapping.assetId, AssetMetadataUpsertDto(items: [mobileMeta]));
} catch (error, stack) {
Logger('migrateCloudIds').warning('Failed to update metadata for asset ${mapping.assetId}', error, stack);
}
}
}
@@ -41,7 +53,7 @@ Future<void> _populateCloudIds(Drift drift) async {
typedef _CloudIdMapping = ({String assetId, String cloudId});
Future<List<_CloudIdMapping>> _fetchCloudIdMappings(Drift drift) async {
Future<List<_CloudIdMapping>> _fetchCloudIdMappings(Drift drift, String userId) async {
final query =
drift.remoteAssetEntity.selectOnly().join([
leftOuterJoin(
@@ -58,7 +70,8 @@ Future<List<_CloudIdMapping>> _fetchCloudIdMappings(Drift drift) async {
])
..addColumns([drift.remoteAssetEntity.id, drift.localAssetEntity.cloudId])
..where(
drift.localAssetEntity.id.isNotNull() &
drift.remoteAssetEntity.ownerId.equals(userId) &
drift.localAssetEntity.id.isNotNull() &
drift.localAssetEntity.cloudId.isNotNull() &
drift.remoteAssetMetadataEntity.cloudId.isNull(),
);