Skip to content

Commit

Permalink
TF-3157 Update web socket with background service worker
Browse files Browse the repository at this point in the history
TF-3157 Stub BroadcastChannel for mobile build
  • Loading branch information
tddang-linagora committed Oct 30, 2024
1 parent 3195616 commit 981beb9
Show file tree
Hide file tree
Showing 16 changed files with 414 additions and 53 deletions.
1 change: 1 addition & 0 deletions core/lib/data/constants/constant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ class Constant {
static const octetStreamMimeType = 'application/octet-stream';
static const pdfExtension = '.pdf';
static const imageType = 'image';
static const wsServiceWorkerBroadcastChannel = 'background-message';
}
8 changes: 4 additions & 4 deletions fcm/lib/model/type_name.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import 'package:equatable/equatable.dart';

class TypeName with EquatableMixin {
static final mailboxType = TypeName('Mailbox');
static final emailType = TypeName('Email');
static final emailDelivery = TypeName('EmailDelivery');
static const mailboxType = TypeName('Mailbox');
static const emailType = TypeName('Email');
static const emailDelivery = TypeName('EmailDelivery');

final String value;

TypeName(this.value);
const TypeName(this.value);

@override
List<Object?> get props => [value];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import 'dart:convert';

import 'package:core/data/constants/constant.dart';
import 'package:core/utils/app_logger.dart';
import 'package:core/utils/broadcast_channel/broadcast_channel.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/capability/capability_identifier.dart';
import 'package:jmap_dart_client/jmap/core/capability/websocket_capability.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:model/extensions/session_extension.dart';
import 'package:rxdart/transformers.dart';
import 'package:tmail_ui_user/features/push_notification/data/datasource/web_socket_datasource.dart';
import 'package:tmail_ui_user/features/push_notification/data/model/connect_web_socket_message.dart';
import 'package:tmail_ui_user/features/push_notification/data/network/web_socket_api.dart';
import 'package:tmail_ui_user/features/push_notification/domain/exceptions/web_socket_exceptions.dart';
import 'package:tmail_ui_user/features/push_notification/domain/model/web_socket_action.dart';
import 'package:tmail_ui_user/main/error/capability_validator.dart';
import 'package:tmail_ui_user/main/exceptions/exception_thrower.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:universal_html/html.dart';

class RemoteWebSocketDatasourceImpl implements WebSocketDatasource {
class WebSocketDatasourceImpl implements WebSocketDatasource {
final WebSocketApi _webSocketApi;
final ExceptionThrower _exceptionThrower;

const RemoteWebSocketDatasourceImpl(this._webSocketApi, this._exceptionThrower);
const WebSocketDatasourceImpl(this._webSocketApi, this._exceptionThrower);

static const String _webSocketClosed = 'webSocketClosed';

@override
Stream getWebSocketChannel(Session session, AccountId accountId) {
Expand All @@ -30,32 +35,31 @@ class RemoteWebSocketDatasourceImpl implements WebSocketDatasource {
Stream _getWebSocketChannel(
Session session,
AccountId accountId,
[int retryRemained = 3]
) async* {
) async* {
final broadcastChannel = BroadcastChannel(Constant.wsServiceWorkerBroadcastChannel);
try {
_verifyWebSocketCapabilities(session, accountId);
final webSocketTicket = await _webSocketApi.getWebSocketTicket(session, accountId);
final webSocketUri = _getWebSocketUri(session, accountId);
window.navigator.serviceWorker?.controller?.postMessage(ConnectWebSocketMessage(
webSocketAction: WebSocketAction.connect,
webSocketUrl: webSocketUri.toString(),
webSocketTicket: webSocketTicket
).toJson());

final webSocketChannel = WebSocketChannel.connect(
Uri.parse('$webSocketUri?ticket=$webSocketTicket'));
await webSocketChannel.ready;
webSocketChannel.sink.add(jsonEncode({"@type": "WebSocketPushEnable"}));

yield* webSocketChannel.stream;
yield* _webSocketListener(broadcastChannel);
} catch (e) {
logError('RemoteWebSocketDatasourceImpl::getWebSocketChannel():error: $e');
if (retryRemained > 0) {
yield* _getWebSocketChannel(session, accountId, retryRemained - 1);
} else {
rethrow;
}
rethrow;
}
}

void _verifyWebSocketCapabilities(Session session, AccountId accountId) {
if (!CapabilityIdentifier.jmapWebSocket.isSupported(session, accountId)
|| !CapabilityIdentifier.jmapWebSocketTicket.isSupported(session, accountId)
|| session.getCapabilityProperties<WebSocketCapability>(
accountId,
CapabilityIdentifier.jmapWebSocket)?.supportsPush != true
) {
throw WebSocketPushNotSupportedException();
}
Expand All @@ -73,4 +77,14 @@ class RemoteWebSocketDatasourceImpl implements WebSocketDatasource {

return webSocketUri;
}

Stream _webSocketListener(BroadcastChannel broadcastChannel) {
return broadcastChannel.onMessage.map((event) {
if (event.data == _webSocketClosed) {
throw WebSocketClosedException();
}

return event.data;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:tmail_ui_user/features/push_notification/domain/model/web_socket_action.dart';

part 'connect_web_socket_message.g.dart';

@JsonSerializable()
class ConnectWebSocketMessage with EquatableMixin {
@JsonKey(name: 'action')
final WebSocketAction webSocketAction;
@JsonKey(name: 'url')
final String webSocketUrl;
@JsonKey(name: 'ticket')
final String webSocketTicket;

ConnectWebSocketMessage({
required this.webSocketAction,
required this.webSocketUrl,
required this.webSocketTicket,
});

factory ConnectWebSocketMessage.fromJson(Map<String, dynamic> json)
=> _$ConnectWebSocketMessageFromJson(json);
Map<String, dynamic> toJson() => _$ConnectWebSocketMessageToJson(this);

@override
List<Object?> get props => [webSocketAction, webSocketUrl, webSocketTicket];
}
31 changes: 31 additions & 0 deletions lib/features/push_notification/data/model/web_socket_echo.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:json_annotation/json_annotation.dart';

part 'web_socket_echo.g.dart';

@JsonSerializable(includeIfNull: false)
class WebSocketEcho {
@JsonKey(name: '@type')
final String? type;
final String? requestId;
final List<List<dynamic>>? methodResponses;

WebSocketEcho({
this.type,
this.requestId,
this.methodResponses,
});

factory WebSocketEcho.fromJson(Map<String, dynamic> json) => _$WebSocketEchoFromJson(json);

Map<String, dynamic> toJson() => _$WebSocketEchoToJson(this);

static bool isValid(Map<String, dynamic> json) {
try {
final webSocketEcho = WebSocketEcho.fromJson(json);
final listResponses = webSocketEcho.methodResponses?.firstOrNull;
return listResponses?.contains('Core/echo') ?? false;
} catch (_) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ class WebSocketPushNotSupportedException implements Exception {}

class WebSocketUriUnavailableException implements Exception {}

class WebSocketTicketUnavailableException implements Exception {}
class WebSocketTicketUnavailableException implements Exception {}

class WebSocketClosedException implements Exception {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enum WebSocketAction {connect, disconnect}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import 'package:jmap_dart_client/jmap/push/state_change.dart';
class InitializingWebSocketPushChannel extends LoadingState {}

class WebSocketPushStateReceived extends UIState {
final StateChange stateChange;
final StateChange? stateChange;

WebSocketPushStateReceived(this.stateChange);

@override
List<Object> get props => [stateChange];
List<Object?> get props => [stateChange];
}

class WebSocketConnectionFailed extends FeatureFailure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:dartz/dartz.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/push/state_change.dart';
import 'package:tmail_ui_user/features/push_notification/data/model/web_socket_echo.dart';
import 'package:tmail_ui_user/features/push_notification/domain/repository/web_socket_repository.dart';
import 'package:tmail_ui_user/features/push_notification/domain/state/web_socket_push_state.dart';

Expand All @@ -31,9 +32,21 @@ class ConnectWebSocketInteractor {
}

Either<Failure, Success> _toStateChange(dynamic data) {
if (data is String) {
data = jsonDecode(data);
StateChange? possibleStateChange;
try {
if (data is String) {
data = jsonDecode(data);
}
possibleStateChange = StateChange.fromJson(data);
return Right(WebSocketPushStateReceived(possibleStateChange));
} catch (e) {
logError('ConnectWebSocketInteractor::_toStateChange: '
'websocket message is not StateChange: $data');
final dataIsWebSocketEcho = WebSocketEcho.isValid(data);
if (dataIsWebSocketEcho) {
return Right(WebSocketPushStateReceived(null));
}
return Left(WebSocketConnectionFailed(exception: e));
}
return Right(WebSocketPushStateReceived(StateChange.fromJson(data)));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:get/get.dart';
import 'package:tmail_ui_user/features/base/interactors_bindings.dart';
import 'package:tmail_ui_user/features/push_notification/data/datasource/web_socket_datasource.dart';
import 'package:tmail_ui_user/features/push_notification/data/datasource_impl/remote_web_socket_datasource_impl.dart';
import 'package:tmail_ui_user/features/push_notification/data/datasource_impl/web_socket_datasource_impl.dart';
import 'package:tmail_ui_user/features/push_notification/data/network/web_socket_api.dart';
import 'package:tmail_ui_user/features/push_notification/data/repository/web_socket_repository_impl.dart';
import 'package:tmail_ui_user/features/push_notification/domain/repository/web_socket_repository.dart';
Expand All @@ -11,12 +11,12 @@ import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';
class WebSocketInteractorBindings extends InteractorsBindings {
@override
void bindingsDataSource() {
Get.lazyPut<WebSocketDatasource>(() => Get.find<RemoteWebSocketDatasourceImpl>());
Get.lazyPut<WebSocketDatasource>(() => Get.find<WebSocketDatasourceImpl>());
}

@override
void bindingsDataSourceImpl() {
Get.lazyPut(() => RemoteWebSocketDatasourceImpl(
Get.lazyPut(() => WebSocketDatasourceImpl(
Get.find<WebSocketApi>(),
Get.find<RemoteExceptionThrower>(),
));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:collection/collection.dart';
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
Expand All @@ -17,12 +19,12 @@ abstract class PushBaseController {
Session? session;
AccountId? accountId;

StreamSubscription<Either<Failure, Success>>? _stateStreamSubscription;

void consumeState(Stream<Either<Failure, Success>> newStateStream) {
newStateStream.listen(
_stateStreamSubscription = newStateStream.listen(
_handleStateStream,
onError: (error, stackTrace) {
logError('PushBaseController::consumeState():onError:error: $error | stackTrace: $stackTrace');
}
onError: handleErrorViewState,
);
}

Expand All @@ -34,6 +36,15 @@ abstract class PushBaseController {

void handleSuccessViewState(Success success);

void handleErrorViewState(Object error, StackTrace stackTrace) {
logError('PushBaseController::handleErrorViewState():error: $error | stackTrace: $stackTrace');
}

void cancelStateStreamSubscription() {
_stateStreamSubscription?.cancel();
_stateStreamSubscription = null;
}

void initialize({AccountId? accountId, Session? session}) {
this.accountId = accountId;
this.session = session;
Expand Down Expand Up @@ -87,22 +98,20 @@ abstract class PushBaseController {
{Session? session}
) {
final newState = jmap.State(mapTypeState[typeName.value]);
if (typeName == TypeName.emailType) {
if (isForeground) {
return SynchronizeEmailOnForegroundAction(typeName, newState, accountId, session);
} else {
return StoreEmailStateToRefreshAction(typeName, newState, accountId, userName, session);
}
} else if (typeName == TypeName.emailDelivery) {
if (!isForeground) {
return PushNotificationAction(typeName, newState, session, accountId, userName);
}
} else if (typeName == TypeName.mailboxType) {
if (isForeground) {
return SynchronizeMailboxOnForegroundAction(typeName, newState, accountId);
} else {
return StoreMailboxStateToRefreshAction(typeName, newState, accountId, userName);
}
switch (typeName) {
case TypeName.emailType:
return isForeground
? SynchronizeEmailOnForegroundAction(typeName, newState, accountId, session)
: StoreEmailStateToRefreshAction(typeName, newState, accountId, userName, session);
case TypeName.emailDelivery:
if (!isForeground) {
return PushNotificationAction(typeName, newState, session, accountId, userName);
}
break;
case TypeName.mailboxType:
return isForeground
? SynchronizeMailboxOnForegroundAction(typeName, newState, accountId)
: StoreMailboxStateToRefreshAction(typeName, newState, accountId, userName);
}
return null;
}
Expand Down
Loading

0 comments on commit 981beb9

Please sign in to comment.