Files
Immich/mobile/lib/utils/openapi_patching.dart
T
Paul MaklesandCodex 925aaed54c feat: backups (#32268)
Signed-off-by: izzy <me@insrt.uk>
Co-authored-by: Zack Pollard <zack@futo.org>
(cherry picked from commit 177b57b689be293bad96b8092e473dc03e068e9c)
2026-10-31 07:21:01 +03:00

101 lines
3.5 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:openapi/api.dart';
abstract interface class _Dynamic {
Object? resolve();
}
class _CurrentTimestamp implements _Dynamic {
const _CurrentTimestamp();
@override
Object? resolve() => DateTime.now().toIso8601String();
}
const _now = _CurrentTimestamp();
@visibleForTesting
final Map<String, Map<String, Object?>> openApiPatches = {
'UserPreferencesResponseDto': {
'download.includeEmbeddedVideos': false,
'folders': FoldersResponse(enabled: false, sidebarWeb: false).toJson(),
'memories': MemoriesResponse(enabled: true, duration: 5, sidebarWeb: false).toJson(),
'ratings': RatingsResponse(enabled: false).toJson(),
'people': PeopleResponse(enabled: true, sidebarWeb: false, updateStrategy: PersonUpdateStrategy.everyone).toJson(),
'tags': TagsResponse(enabled: false, sidebarWeb: false).toJson(),
'sharedLinks': SharedLinksResponse(enabled: true, sidebarWeb: false).toJson(),
'cast': CastResponse(gCastEnabled: false).toJson(),
'albums': {'defaultAssetOrder': 'desc'},
'recentlyAdded': RecentlyAddedResponse(sidebarWeb: false).toJson(),
},
'PublicConfigDto': {
'map': PublicConfigMapDto(
enabled: true,
lightStyle: 'https://tiles.immich.cloud/v1/style/light.json',
darkStyle: 'https://tiles.immich.cloud/v1/style/dark.json',
).toJson(),
'ffmpeg': {
'realtime': {'enabled': false},
},
'passkey': {'enabled': false},
},
'ServerConfigDto': {
'mapLightStyleUrl': 'https://tiles.immich.cloud/v1/style/light.json',
'mapDarkStyleUrl': 'https://tiles.immich.cloud/v1/style/dark.json',
'minFaces': 3,
},
'UserResponseDto': {'profileChangedAt': _now},
'AssetResponseDto': {'visibility': 'timeline', 'createdAt': _now, 'isEdited': false},
'UserAdminResponseDto': {'profileChangedAt': _now, 'clusterGroupId': ''},
'LoginResponseDto': {'isOnboarded': false},
'SyncUserV1': {'profileChangedAt': _now, 'hasProfileImage': false},
'SyncAssetV1': {'isEdited': false},
'ServerFeaturesDto': {'ocr': false, 'realtimeTranscoding': false, 'backups': false},
'SearchAssetResponseDto': {'nextCursor': null},
'MemoriesResponse': {'duration': 5, 'sidebarWeb': false},
'PeopleResponse': {'updateStrategy': 'everyone'},
'PersonResponseDto': {'otherPeople': const [], 'sharedBy': const [], 'sharedWith': const []},
'ServerVersionResponseDto': {'featureLevel': 0},
'WorkflowResponseDto': {'logging': false},
'AdminConfigDto': {
'passkey': {'enabled': false, 'domain': null, 'additionalDomains': const []},
},
'UserConfigDto': {
'notifications': {
'smtp': {'enabled': false},
},
'passkey': {'enabled': false},
},
};
// ignore: unused-code
void upgradeDto(dynamic value, String targetType) {
if (value is! Map) {
return;
}
final fields = openApiPatches[targetType];
if (fields == null) {
return;
}
fields.forEach((key, defaultValue) {
addDefault(value, key, defaultValue is _Dynamic ? defaultValue.resolve() : defaultValue);
});
}
void addDefault(dynamic value, String keys, dynamic defaultValue) {
// Loop through the keys and assign the default value if the key is not present
final List<String> keyList = keys.split('.');
dynamic current = value;
for (int i = 0; i < keyList.length - 1; i++) {
if (current[keyList[i]] == null) {
current[keyList[i]] = {};
}
current = current[keyList[i]];
}
if (current[keyList.last] == null) {
current[keyList.last] = defaultValue;
}
}