mirror of
https://github.com/immich-app/immich.git
synced 2025-05-31 20:25:32 -04:00
feat(mobile): new sync (#16556)
* feat(mobile): new sync * refactor * refactor * refactor * refactor * refactor * refactor * update analysis option * remove database operation * pr feedback
This commit is contained in:
parent
2875303b4c
commit
1ed1a0a1fc
@ -89,10 +89,13 @@ custom_lint:
|
|||||||
allowed:
|
allowed:
|
||||||
# required / wanted
|
# required / wanted
|
||||||
- lib/repositories/*_api.repository.dart
|
- lib/repositories/*_api.repository.dart
|
||||||
|
- lib/infrastructure/repositories/*_api.repository.dart
|
||||||
# acceptable exceptions for the time being
|
# acceptable exceptions for the time being
|
||||||
- lib/entities/{album,asset,exif_info,user}.entity.dart # to convert DTOs to entities
|
- lib/entities/{album,asset,exif_info,user}.entity.dart # to convert DTOs to entities
|
||||||
- lib/utils/{image_url_builder,openapi_patching}.dart # utils are fine
|
- lib/utils/{image_url_builder,openapi_patching}.dart # utils are fine
|
||||||
- test/modules/utils/openapi_patching_test.dart # filename is self-explanatory...
|
- test/modules/utils/openapi_patching_test.dart # filename is self-explanatory...
|
||||||
|
- lib/domain/services/sync_stream.service.dart # Making sure to comply with the type from database
|
||||||
|
|
||||||
# refactor
|
# refactor
|
||||||
- lib/models/map/map_marker.model.dart
|
- lib/models/map/map_marker.model.dart
|
||||||
- lib/models/server_info/server_{config,disk_info,features,version}.model.dart
|
- lib/models/server_info/server_{config,disk_info,features,version}.model.dart
|
||||||
|
7
mobile/lib/domain/interfaces/sync_api.interface.dart
Normal file
7
mobile/lib/domain/interfaces/sync_api.interface.dart
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import 'package:immich_mobile/domain/models/sync/sync_event.model.dart';
|
||||||
|
|
||||||
|
abstract interface class ISyncApiRepository {
|
||||||
|
Future<void> ack(String data);
|
||||||
|
|
||||||
|
Stream<List<SyncEvent>> watchUserSyncEvent();
|
||||||
|
}
|
14
mobile/lib/domain/models/sync/sync_event.model.dart
Normal file
14
mobile/lib/domain/models/sync/sync_event.model.dart
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
class SyncEvent {
|
||||||
|
// dynamic
|
||||||
|
final dynamic data;
|
||||||
|
|
||||||
|
final String ack;
|
||||||
|
|
||||||
|
SyncEvent({
|
||||||
|
required this.data,
|
||||||
|
required this.ack,
|
||||||
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() => 'SyncEvent(data: $data, ack: $ack)';
|
||||||
|
}
|
49
mobile/lib/domain/services/sync_stream.service.dart
Normal file
49
mobile/lib/domain/services/sync_stream.service.dart
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:immich_mobile/domain/interfaces/sync_api.interface.dart';
|
||||||
|
import 'package:openapi/api.dart';
|
||||||
|
|
||||||
|
class SyncStreamService {
|
||||||
|
final ISyncApiRepository _syncApiRepository;
|
||||||
|
|
||||||
|
SyncStreamService(this._syncApiRepository);
|
||||||
|
|
||||||
|
StreamSubscription? _userSyncSubscription;
|
||||||
|
|
||||||
|
void syncUsers() {
|
||||||
|
_userSyncSubscription =
|
||||||
|
_syncApiRepository.watchUserSyncEvent().listen((events) async {
|
||||||
|
for (final event in events) {
|
||||||
|
if (event.data is SyncUserV1) {
|
||||||
|
final data = event.data as SyncUserV1;
|
||||||
|
debugPrint("User Update: $data");
|
||||||
|
|
||||||
|
// final user = await _userRepository.get(data.id);
|
||||||
|
|
||||||
|
// if (user == null) {
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// user.name = data.name;
|
||||||
|
// user.email = data.email;
|
||||||
|
// user.updatedAt = DateTime.now();
|
||||||
|
|
||||||
|
// await _userRepository.update(user);
|
||||||
|
// await _syncApiRepository.ack(event.ack);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.data is SyncUserDeleteV1) {
|
||||||
|
final data = event.data as SyncUserDeleteV1;
|
||||||
|
|
||||||
|
debugPrint("User delete: $data");
|
||||||
|
// await _syncApiRepository.ack(event.ack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> dispose() async {
|
||||||
|
await _userSyncSubscription?.cancel();
|
||||||
|
}
|
||||||
|
}
|
112
mobile/lib/infrastructure/repositories/sync_api.repository.dart
Normal file
112
mobile/lib/infrastructure/repositories/sync_api.repository.dart
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:immich_mobile/domain/interfaces/sync_api.interface.dart';
|
||||||
|
import 'package:immich_mobile/domain/models/sync/sync_event.model.dart';
|
||||||
|
import 'package:immich_mobile/services/api.service.dart';
|
||||||
|
import 'package:openapi/api.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
|
class SyncApiRepository implements ISyncApiRepository {
|
||||||
|
final ApiService _api;
|
||||||
|
const SyncApiRepository(this._api);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Stream<List<SyncEvent>> watchUserSyncEvent() {
|
||||||
|
return _getSyncStream(
|
||||||
|
SyncStreamDto(types: [SyncRequestType.usersV1]),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> ack(String data) {
|
||||||
|
return _api.syncApi.sendSyncAck(SyncAckSetDto(acks: [data]));
|
||||||
|
}
|
||||||
|
|
||||||
|
Stream<List<SyncEvent>> _getSyncStream(
|
||||||
|
SyncStreamDto dto, {
|
||||||
|
int batchSize = 5000,
|
||||||
|
}) async* {
|
||||||
|
final client = http.Client();
|
||||||
|
final endpoint = "${_api.apiClient.basePath}/sync/stream";
|
||||||
|
|
||||||
|
final headers = <String, String>{
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Accept': 'application/jsonlines+json',
|
||||||
|
};
|
||||||
|
|
||||||
|
final queryParams = <QueryParam>[];
|
||||||
|
final headerParams = <String, String>{};
|
||||||
|
await _api.applyToParams(queryParams, headerParams);
|
||||||
|
headers.addAll(headerParams);
|
||||||
|
|
||||||
|
final request = http.Request('POST', Uri.parse(endpoint));
|
||||||
|
request.headers.addAll(headers);
|
||||||
|
request.body = jsonEncode(dto.toJson());
|
||||||
|
|
||||||
|
String previousChunk = '';
|
||||||
|
List<String> lines = [];
|
||||||
|
|
||||||
|
try {
|
||||||
|
final response = await client.send(request);
|
||||||
|
|
||||||
|
if (response.statusCode != 200) {
|
||||||
|
final errorBody = await response.stream.bytesToString();
|
||||||
|
throw ApiException(
|
||||||
|
response.statusCode,
|
||||||
|
'Failed to get sync stream: $errorBody',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await for (final chunk in response.stream.transform(utf8.decoder)) {
|
||||||
|
previousChunk += chunk;
|
||||||
|
final parts = previousChunk.split('\n');
|
||||||
|
previousChunk = parts.removeLast();
|
||||||
|
lines.addAll(parts);
|
||||||
|
|
||||||
|
if (lines.length < batchSize) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
yield await compute(_parseSyncResponse, lines);
|
||||||
|
lines.clear();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (lines.isNotEmpty) {
|
||||||
|
yield await compute(_parseSyncResponse, lines);
|
||||||
|
}
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const _kResponseMap = <SyncEntityType, Function(dynamic)>{
|
||||||
|
SyncEntityType.userV1: SyncUserV1.fromJson,
|
||||||
|
SyncEntityType.userDeleteV1: SyncUserDeleteV1.fromJson,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Need to be outside of the class to be able to use compute
|
||||||
|
List<SyncEvent> _parseSyncResponse(List<String> lines) {
|
||||||
|
final List<SyncEvent> data = [];
|
||||||
|
|
||||||
|
for (var line in lines) {
|
||||||
|
try {
|
||||||
|
final jsonData = jsonDecode(line);
|
||||||
|
final type = SyncEntityType.fromJson(jsonData['type'])!;
|
||||||
|
final dataJson = jsonData['data'];
|
||||||
|
final ack = jsonData['ack'];
|
||||||
|
final converter = _kResponseMap[type];
|
||||||
|
if (converter == null) {
|
||||||
|
debugPrint("[_parseSyncReponse] Unknown type $type");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
data.add(SyncEvent(data: converter(dataJson), ack: ack));
|
||||||
|
} catch (error, stack) {
|
||||||
|
debugPrint("[_parseSyncReponse] Error parsing json $error $stack");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||||
|
import 'package:immich_mobile/domain/services/sync_stream.service.dart';
|
||||||
|
import 'package:immich_mobile/infrastructure/repositories/sync_api.repository.dart';
|
||||||
|
import 'package:immich_mobile/providers/api.provider.dart';
|
||||||
|
|
||||||
|
final syncStreamServiceProvider = Provider(
|
||||||
|
(ref) {
|
||||||
|
final instance = SyncStreamService(
|
||||||
|
ref.watch(syncApiRepositoryProvider),
|
||||||
|
);
|
||||||
|
|
||||||
|
ref.onDispose(() => unawaited(instance.dispose()));
|
||||||
|
|
||||||
|
return instance;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
final syncApiRepositoryProvider = Provider(
|
||||||
|
(ref) => SyncApiRepository(
|
||||||
|
ref.watch(apiServiceProvider),
|
||||||
|
),
|
||||||
|
);
|
@ -10,6 +10,7 @@ import 'package:immich_mobile/models/backup/backup_state.model.dart';
|
|||||||
import 'package:immich_mobile/models/server_info/server_info.model.dart';
|
import 'package:immich_mobile/models/server_info/server_info.model.dart';
|
||||||
import 'package:immich_mobile/providers/backup/backup.provider.dart';
|
import 'package:immich_mobile/providers/backup/backup.provider.dart';
|
||||||
import 'package:immich_mobile/providers/immich_logo_provider.dart';
|
import 'package:immich_mobile/providers/immich_logo_provider.dart';
|
||||||
|
import 'package:immich_mobile/providers/infrastructure/sync_stream.provider.dart';
|
||||||
import 'package:immich_mobile/providers/server_info.provider.dart';
|
import 'package:immich_mobile/providers/server_info.provider.dart';
|
||||||
import 'package:immich_mobile/routing/router.dart';
|
import 'package:immich_mobile/routing/router.dart';
|
||||||
import 'package:immich_mobile/widgets/common/app_bar_dialog/app_bar_dialog.dart';
|
import 'package:immich_mobile/widgets/common/app_bar_dialog/app_bar_dialog.dart';
|
||||||
@ -185,6 +186,12 @@ class ImmichAppBar extends ConsumerWidget implements PreferredSizeWidget {
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
actions: [
|
actions: [
|
||||||
|
IconButton(
|
||||||
|
onPressed: () {
|
||||||
|
ref.read(syncStreamServiceProvider).syncUsers();
|
||||||
|
},
|
||||||
|
icon: const Icon(Icons.sync),
|
||||||
|
),
|
||||||
if (actions != null)
|
if (actions != null)
|
||||||
...actions!.map(
|
...actions!.map(
|
||||||
(action) => Padding(
|
(action) => Padding(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user