2022-02-14 18:40:41 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import 'package:immich_mobile/modules/login/providers/authentication.provider.dart';
|
2023-02-04 22:42:42 +02:00
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
2023-11-17 23:46:18 +02:00
|
|
|
import 'package:immich_mobile/shared/models/server_info/server_version.model.dart';
|
2023-03-23 03:36:44 +02:00
|
|
|
import 'package:immich_mobile/shared/models/store.dart';
|
2022-06-22 07:23:35 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/asset.provider.dart';
|
2023-10-06 09:01:14 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/server_info.provider.dart';
|
2023-10-16 20:01:38 +02:00
|
|
|
import 'package:immich_mobile/shared/services/sync.service.dart';
|
|
|
|
import 'package:immich_mobile/utils/debounce.dart';
|
2022-11-27 22:34:19 +02:00
|
|
|
import 'package:logging/logging.dart';
|
2022-07-13 14:23:48 +02:00
|
|
|
import 'package:openapi/api.dart';
|
2022-06-22 07:23:35 +02:00
|
|
|
import 'package:socket_io_client/socket_io_client.dart';
|
2022-02-14 18:40:41 +02:00
|
|
|
|
2023-10-16 20:01:38 +02:00
|
|
|
enum PendingAction {
|
|
|
|
assetDelete,
|
|
|
|
}
|
|
|
|
|
|
|
|
class PendingChange {
|
|
|
|
final PendingAction action;
|
|
|
|
final dynamic value;
|
|
|
|
|
|
|
|
const PendingChange(this.action, this.value);
|
|
|
|
}
|
|
|
|
|
2022-11-27 22:34:19 +02:00
|
|
|
class WebsocketState {
|
2022-02-14 18:40:41 +02:00
|
|
|
final Socket? socket;
|
|
|
|
final bool isConnected;
|
2023-10-16 20:01:38 +02:00
|
|
|
final List<PendingChange> pendingChanges;
|
2022-02-14 18:40:41 +02:00
|
|
|
|
2022-11-27 22:34:19 +02:00
|
|
|
WebsocketState({
|
2022-02-14 18:40:41 +02:00
|
|
|
this.socket,
|
|
|
|
required this.isConnected,
|
2023-10-16 20:01:38 +02:00
|
|
|
required this.pendingChanges,
|
2022-02-14 18:40:41 +02:00
|
|
|
});
|
|
|
|
|
2022-11-27 22:34:19 +02:00
|
|
|
WebsocketState copyWith({
|
2022-02-14 18:40:41 +02:00
|
|
|
Socket? socket,
|
|
|
|
bool? isConnected,
|
2023-10-16 20:01:38 +02:00
|
|
|
List<PendingChange>? pendingChanges,
|
2022-02-14 18:40:41 +02:00
|
|
|
}) {
|
2022-11-27 22:34:19 +02:00
|
|
|
return WebsocketState(
|
2022-02-14 18:40:41 +02:00
|
|
|
socket: socket ?? this.socket,
|
|
|
|
isConnected: isConnected ?? this.isConnected,
|
2023-10-16 20:01:38 +02:00
|
|
|
pendingChanges: pendingChanges ?? this.pendingChanges,
|
2022-02-14 18:40:41 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-06-25 22:12:47 +02:00
|
|
|
String toString() =>
|
2022-11-27 22:34:19 +02:00
|
|
|
'WebsocketState(socket: $socket, isConnected: $isConnected)';
|
2022-02-14 18:40:41 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
bool operator ==(Object other) {
|
|
|
|
if (identical(this, other)) return true;
|
|
|
|
|
2022-11-27 22:34:19 +02:00
|
|
|
return other is WebsocketState &&
|
2022-06-25 22:12:47 +02:00
|
|
|
other.socket == socket &&
|
|
|
|
other.isConnected == isConnected;
|
2022-02-14 18:40:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get hashCode => socket.hashCode ^ isConnected.hashCode;
|
|
|
|
}
|
|
|
|
|
2022-11-27 22:34:19 +02:00
|
|
|
class WebsocketNotifier extends StateNotifier<WebsocketState> {
|
2023-11-13 21:51:16 +02:00
|
|
|
WebsocketNotifier(this._ref)
|
2023-10-16 20:01:38 +02:00
|
|
|
: super(
|
|
|
|
WebsocketState(socket: null, isConnected: false, pendingChanges: []),
|
2023-11-13 21:51:16 +02:00
|
|
|
);
|
2022-02-14 18:40:41 +02:00
|
|
|
|
2023-11-13 21:51:16 +02:00
|
|
|
final _log = Logger('WebsocketNotifier');
|
|
|
|
final Ref _ref;
|
|
|
|
final Debounce _debounce = Debounce(const Duration(milliseconds: 500));
|
2022-02-14 18:40:41 +02:00
|
|
|
|
2023-11-13 21:51:16 +02:00
|
|
|
/// Connects websocket to server unless already connected
|
|
|
|
void connect() {
|
|
|
|
if (state.isConnected) return;
|
|
|
|
final authenticationState = _ref.read(authenticationProvider);
|
2022-02-14 18:40:41 +02:00
|
|
|
|
|
|
|
if (authenticationState.isAuthenticated) {
|
2023-03-23 03:36:44 +02:00
|
|
|
final accessToken = Store.get(StoreKey.accessToken);
|
2022-02-14 18:40:41 +02:00
|
|
|
try {
|
2023-03-23 03:36:44 +02:00
|
|
|
final endpoint = Uri.parse(Store.get(StoreKey.serverEndpoint));
|
2023-01-19 17:45:37 +02:00
|
|
|
|
2022-11-28 22:14:22 +02:00
|
|
|
debugPrint("Attempting to connect to websocket");
|
2022-11-27 22:34:19 +02:00
|
|
|
// Configure socket transports must be specified
|
2022-02-14 18:40:41 +02:00
|
|
|
Socket socket = io(
|
2023-01-19 17:45:37 +02:00
|
|
|
endpoint.origin,
|
2022-02-14 18:40:41 +02:00
|
|
|
OptionBuilder()
|
2023-01-19 17:45:37 +02:00
|
|
|
.setPath("${endpoint.path}/socket.io")
|
2022-02-14 18:40:41 +02:00
|
|
|
.setTransports(['websocket'])
|
|
|
|
.enableReconnection()
|
|
|
|
.enableForceNew()
|
|
|
|
.enableForceNewConnection()
|
|
|
|
.enableAutoConnect()
|
|
|
|
.setExtraHeaders({"Authorization": "Bearer $accessToken"})
|
|
|
|
.build(),
|
|
|
|
);
|
|
|
|
|
|
|
|
socket.onConnect((_) {
|
2022-11-28 22:14:22 +02:00
|
|
|
debugPrint("Established Websocket Connection");
|
2023-10-16 20:01:38 +02:00
|
|
|
state = WebsocketState(
|
|
|
|
isConnected: true,
|
|
|
|
socket: socket,
|
|
|
|
pendingChanges: state.pendingChanges,
|
|
|
|
);
|
2022-02-14 18:40:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.onDisconnect((_) {
|
2022-11-28 22:14:22 +02:00
|
|
|
debugPrint("Disconnect to Websocket Connection");
|
2023-10-16 20:01:38 +02:00
|
|
|
state = WebsocketState(
|
|
|
|
isConnected: false,
|
|
|
|
socket: null,
|
|
|
|
pendingChanges: state.pendingChanges,
|
|
|
|
);
|
2022-02-14 18:40:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('error', (errorMessage) {
|
2023-11-13 21:51:16 +02:00
|
|
|
_log.severe("Websocket Error - $errorMessage");
|
2023-10-16 20:01:38 +02:00
|
|
|
state = WebsocketState(
|
|
|
|
isConnected: false,
|
|
|
|
socket: null,
|
|
|
|
pendingChanges: state.pendingChanges,
|
|
|
|
);
|
2022-02-14 18:40:41 +02:00
|
|
|
});
|
|
|
|
|
2023-02-04 22:42:42 +02:00
|
|
|
socket.on('on_upload_success', _handleOnUploadSuccess);
|
2023-10-06 09:01:14 +02:00
|
|
|
socket.on('on_config_update', _handleOnConfigUpdate);
|
2023-10-16 20:01:38 +02:00
|
|
|
socket.on('on_asset_delete', _handleOnAssetDelete);
|
|
|
|
socket.on('on_asset_trash', _handleServerUpdates);
|
|
|
|
socket.on('on_asset_restore', _handleServerUpdates);
|
2023-10-22 04:38:07 +02:00
|
|
|
socket.on('on_asset_update', _handleServerUpdates);
|
2023-11-17 23:46:18 +02:00
|
|
|
socket.on('on_new_release', _handleReleaseUpdates);
|
2022-02-14 18:40:41 +02:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("[WEBSOCKET] Catch Websocket Error - ${e.toString()}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-13 21:51:16 +02:00
|
|
|
void disconnect() {
|
2022-11-28 22:14:22 +02:00
|
|
|
debugPrint("Attempting to disconnect from websocket");
|
2022-11-27 22:34:19 +02:00
|
|
|
|
2022-02-14 18:40:41 +02:00
|
|
|
var socket = state.socket?.disconnect();
|
2022-07-01 03:08:49 +02:00
|
|
|
|
|
|
|
if (socket?.disconnected == true) {
|
2023-10-16 20:01:38 +02:00
|
|
|
state = WebsocketState(
|
|
|
|
isConnected: false,
|
|
|
|
socket: null,
|
|
|
|
pendingChanges: state.pendingChanges,
|
|
|
|
);
|
2022-02-14 18:40:41 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-03 19:31:45 +02:00
|
|
|
|
2023-11-13 21:51:16 +02:00
|
|
|
void stopListenToEvent(String eventName) {
|
2022-11-28 22:14:22 +02:00
|
|
|
debugPrint("Stop listening to event $eventName");
|
2022-04-03 19:31:45 +02:00
|
|
|
state.socket?.off(eventName);
|
|
|
|
}
|
|
|
|
|
2023-11-13 21:51:16 +02:00
|
|
|
void listenUploadEvent() {
|
2022-11-28 22:14:22 +02:00
|
|
|
debugPrint("Start listening to event on_upload_success");
|
2023-02-04 22:42:42 +02:00
|
|
|
state.socket?.on('on_upload_success', _handleOnUploadSuccess);
|
|
|
|
}
|
2022-07-13 14:23:48 +02:00
|
|
|
|
2023-11-13 21:51:16 +02:00
|
|
|
void addPendingChange(PendingAction action, dynamic value) {
|
2023-10-16 20:01:38 +02:00
|
|
|
state = state.copyWith(
|
|
|
|
pendingChanges: [...state.pendingChanges, PendingChange(action, value)],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-13 21:51:16 +02:00
|
|
|
void handlePendingChanges() {
|
2023-10-16 20:01:38 +02:00
|
|
|
final deleteChanges = state.pendingChanges
|
|
|
|
.where((c) => c.action == PendingAction.assetDelete)
|
|
|
|
.toList();
|
|
|
|
if (deleteChanges.isNotEmpty) {
|
2023-10-24 17:05:42 +02:00
|
|
|
List<String> remoteIds =
|
|
|
|
deleteChanges.map((a) => a.value.toString()).toList();
|
2023-11-13 21:51:16 +02:00
|
|
|
_ref.read(syncServiceProvider).handleRemoteAssetRemoval(remoteIds);
|
2023-10-16 20:01:38 +02:00
|
|
|
state = state.copyWith(
|
|
|
|
pendingChanges: state.pendingChanges
|
|
|
|
.where((c) => c.action != PendingAction.assetDelete)
|
|
|
|
.toList(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-13 21:51:16 +02:00
|
|
|
void _handleOnUploadSuccess(dynamic data) {
|
2023-10-24 17:05:42 +02:00
|
|
|
final dto = AssetResponseDto.fromJson(data);
|
2023-02-04 22:42:42 +02:00
|
|
|
if (dto != null) {
|
|
|
|
final newAsset = Asset.remote(dto);
|
2023-11-13 21:51:16 +02:00
|
|
|
_ref.watch(assetProvider.notifier).onNewAssetUploaded(newAsset);
|
2023-02-04 22:42:42 +02:00
|
|
|
}
|
2022-04-03 19:31:45 +02:00
|
|
|
}
|
2023-10-06 09:01:14 +02:00
|
|
|
|
2023-11-13 21:51:16 +02:00
|
|
|
void _handleOnConfigUpdate(dynamic _) {
|
|
|
|
_ref.read(serverInfoProvider.notifier).getServerFeatures();
|
|
|
|
_ref.read(serverInfoProvider.notifier).getServerConfig();
|
2023-10-06 09:01:14 +02:00
|
|
|
}
|
2023-10-16 20:01:38 +02:00
|
|
|
|
|
|
|
// Refresh updated assets
|
2023-11-13 21:51:16 +02:00
|
|
|
void _handleServerUpdates(dynamic _) {
|
|
|
|
_ref.read(assetProvider.notifier).getAllAsset();
|
2023-10-16 20:01:38 +02:00
|
|
|
}
|
|
|
|
|
2023-11-13 21:51:16 +02:00
|
|
|
void _handleOnAssetDelete(dynamic data) {
|
2023-10-16 20:01:38 +02:00
|
|
|
addPendingChange(PendingAction.assetDelete, data);
|
2023-11-13 21:51:16 +02:00
|
|
|
_debounce(handlePendingChanges);
|
2023-10-16 20:01:38 +02:00
|
|
|
}
|
2023-11-17 23:46:18 +02:00
|
|
|
|
|
|
|
_handleReleaseUpdates(dynamic data) {
|
|
|
|
// Json guard
|
|
|
|
if (data is! Map) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final json = data.cast<String, dynamic>();
|
|
|
|
final serverVersionJson =
|
|
|
|
json.containsKey('serverVersion') ? json['serverVersion'] : null;
|
|
|
|
final releaseVersionJson =
|
|
|
|
json.containsKey('releaseVersion') ? json['releaseVersion'] : null;
|
|
|
|
if (serverVersionJson == null || releaseVersionJson == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final serverVersionDto =
|
|
|
|
ServerVersionResponseDto.fromJson(serverVersionJson);
|
|
|
|
final releaseVersionDto =
|
|
|
|
ServerVersionResponseDto.fromJson(releaseVersionJson);
|
|
|
|
if (serverVersionDto == null || releaseVersionDto == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final serverVersion = ServerVersion.fromDto(serverVersionDto);
|
|
|
|
final releaseVersion = ServerVersion.fromDto(releaseVersionDto);
|
|
|
|
_ref
|
|
|
|
.read(serverInfoProvider.notifier)
|
|
|
|
.handleNewRelease(serverVersion, releaseVersion);
|
|
|
|
}
|
2022-02-14 18:40:41 +02:00
|
|
|
}
|
|
|
|
|
2022-06-25 22:12:47 +02:00
|
|
|
final websocketProvider =
|
2022-11-27 22:34:19 +02:00
|
|
|
StateNotifierProvider<WebsocketNotifier, WebsocketState>((ref) {
|
2022-02-14 18:40:41 +02:00
|
|
|
return WebsocketNotifier(ref);
|
|
|
|
});
|