diff --git a/lib/features/caching/caching_manager.dart b/lib/features/caching/caching_manager.dart index f28f8018d..01b2335ce 100644 --- a/lib/features/caching/caching_manager.dart +++ b/lib/features/caching/caching_manager.dart @@ -2,10 +2,13 @@ import 'package:core/utils/app_logger.dart'; import 'package:core/utils/file_utils.dart'; import 'package:core/utils/platform_info.dart'; import 'package:core/utils/sentry/sentry_config.dart'; +import 'package:sentry_flutter/sentry_flutter.dart'; import 'package:tmail_ui_user/features/caching/clients/hive_cache_version_client.dart'; import 'package:tmail_ui_user/features/caching/config/hive_cache_config.dart'; import 'package:tmail_ui_user/features/caching/extensions/sentry_config_extension.dart'; import 'package:tmail_ui_user/features/caching/extensions/sentry_configuration_cache_extension.dart'; +import 'package:tmail_ui_user/features/caching/extensions/sentry_user_cache_extension.dart'; +import 'package:tmail_ui_user/features/caching/extensions/sentry_user_extension.dart'; import 'package:tmail_ui_user/features/caching/manager/sentry_configuration_cache_manager.dart'; import 'package:tmail_ui_user/features/caching/manager/session_cache_manger.dart'; import 'package:tmail_ui_user/features/caching/utils/caching_constants.dart'; @@ -251,4 +254,36 @@ class CachingManager { return null; } } + + Future saveSentryUser(SentryUser sentryUser) async { + try { + await _sentryConfigurationCacheManager.saveSentryUser( + sentryUser.toSentryUserCache(), + ); + log('CachingManager::saveSentryConfiguration: Sentry user saved successfully'); + } catch (e, st) { + logError( + 'CachingManager::saveSentryUser: Cannot save sentry user', + exception: e, + stackTrace: st, + ); + } + } + + Future getSentryUser() async { + try { + final sentryUserCache = + await _sentryConfigurationCacheManager.getSentryUser(); + final sentryUser = sentryUserCache.toSentryUser(); + log('CachingManager::getSentryUser: Sentry user: $sentryUser'); + return sentryUser; + } catch (e, st) { + logError( + 'CachingManager::getSentryUser: Cannot get sentry user', + exception: e, + stackTrace: st, + ); + return null; + } + } } diff --git a/lib/features/caching/clients/sentry_user_cache_client.dart b/lib/features/caching/clients/sentry_user_cache_client.dart new file mode 100644 index 000000000..f862979af --- /dev/null +++ b/lib/features/caching/clients/sentry_user_cache_client.dart @@ -0,0 +1,12 @@ +import 'package:tmail_ui_user/features/caching/config/hive_cache_client.dart'; +import 'package:tmail_ui_user/features/caching/entries/sentry_user_cache.dart'; +import 'package:tmail_ui_user/features/caching/utils/caching_constants.dart'; + +class SentryUserCacheClient + extends HiveCacheClient { + @override + String get tableName => CachingConstants.sentryConfigurationCacheBoxName; + + @override + bool get encryption => true; +} diff --git a/lib/features/caching/config/hive_cache_config.dart b/lib/features/caching/config/hive_cache_config.dart index 82f38d53c..726079e0d 100644 --- a/lib/features/caching/config/hive_cache_config.dart +++ b/lib/features/caching/config/hive_cache_config.dart @@ -23,6 +23,7 @@ import 'package:tmail_ui_user/features/caching/caching_manager.dart'; import 'package:tmail_ui_user/features/caching/config/cache_version.dart'; import 'package:tmail_ui_user/features/caching/config/fcm_isolate_name_server.dart'; import 'package:tmail_ui_user/features/caching/entries/sentry_configuration_cache.dart'; +import 'package:tmail_ui_user/features/caching/entries/sentry_user_cache.dart'; import 'package:tmail_ui_user/features/caching/utils/caching_constants.dart'; import 'package:tmail_ui_user/features/home/data/model/session_hive_obj.dart'; import 'package:tmail_ui_user/features/login/data/local/encryption_key_cache_manager.dart'; @@ -245,6 +246,11 @@ class HiveCacheConfig { CachingConstants.SENTRY_CONFIGURATION_CACHE_ID, isolated: isolated, ); + registerCacheAdapter( + SentryUserCacheAdapter(), + CachingConstants.SENTRY_USER_CACHE_ID, + isolated: isolated, + ); } void registerCacheAdapter( diff --git a/lib/features/caching/entries/sentry_user_cache.dart b/lib/features/caching/entries/sentry_user_cache.dart new file mode 100644 index 000000000..6f167cf2a --- /dev/null +++ b/lib/features/caching/entries/sentry_user_cache.dart @@ -0,0 +1,35 @@ +import 'package:equatable/equatable.dart'; +import 'package:hive_ce/hive.dart'; +import 'package:tmail_ui_user/features/caching/utils/caching_constants.dart'; + +part 'sentry_user_cache.g.dart'; + +@HiveType(typeId: CachingConstants.SENTRY_USER_CACHE_ID) +class SentryUserCache extends HiveObject with EquatableMixin { + @HiveField(0) + final String id; + + @HiveField(1) + final String name; + + @HiveField(2) + final String username; + + @HiveField(3) + final String email; + + SentryUserCache({ + required this.id, + required this.name, + required this.username, + required this.email, + }); + + @override + List get props => [ + id, + name, + username, + email, + ]; +} diff --git a/lib/features/caching/exceptions/local_storage_exception.dart b/lib/features/caching/exceptions/local_storage_exception.dart index 7ce6d5d8d..07c4477b0 100644 --- a/lib/features/caching/exceptions/local_storage_exception.dart +++ b/lib/features/caching/exceptions/local_storage_exception.dart @@ -1,3 +1,5 @@ class NotFoundDataWithThisKeyException implements Exception {} -class NotFoundSentryConfigurationException implements Exception {} \ No newline at end of file +class NotFoundSentryConfigurationException implements Exception {} + +class NotFoundSentryUserException implements Exception {} \ No newline at end of file diff --git a/lib/features/caching/extensions/sentry_user_cache_extension.dart b/lib/features/caching/extensions/sentry_user_cache_extension.dart new file mode 100644 index 000000000..d4ca8e9c2 --- /dev/null +++ b/lib/features/caching/extensions/sentry_user_cache_extension.dart @@ -0,0 +1,13 @@ +import 'package:sentry_flutter/sentry_flutter.dart'; +import 'package:tmail_ui_user/features/caching/entries/sentry_user_cache.dart'; + +extension SentryUserCacheExtension on SentryUserCache { + SentryUser toSentryUser() { + return SentryUser( + id: id, + name: name, + username: username, + email: email, + ); + } +} diff --git a/lib/features/caching/extensions/sentry_user_extension.dart b/lib/features/caching/extensions/sentry_user_extension.dart new file mode 100644 index 000000000..1e20f1f09 --- /dev/null +++ b/lib/features/caching/extensions/sentry_user_extension.dart @@ -0,0 +1,13 @@ +import 'package:sentry_flutter/sentry_flutter.dart'; +import 'package:tmail_ui_user/features/caching/entries/sentry_user_cache.dart'; + +extension SentryUserExtension on SentryUser { + SentryUserCache toSentryUserCache() { + return SentryUserCache( + id: id ?? '', + name: name ?? '', + username: username ?? '', + email: email ?? '', + ); + } +} diff --git a/lib/features/caching/manager/sentry_configuration_cache_manager.dart b/lib/features/caching/manager/sentry_configuration_cache_manager.dart index 0b1671fc1..636de93fe 100644 --- a/lib/features/caching/manager/sentry_configuration_cache_manager.dart +++ b/lib/features/caching/manager/sentry_configuration_cache_manager.dart @@ -1,16 +1,25 @@ import 'package:tmail_ui_user/features/caching/clients/sentry_configuration_cache_client.dart'; +import 'package:tmail_ui_user/features/caching/clients/sentry_user_cache_client.dart'; import 'package:tmail_ui_user/features/caching/entries/sentry_configuration_cache.dart'; +import 'package:tmail_ui_user/features/caching/entries/sentry_user_cache.dart'; import 'package:tmail_ui_user/features/caching/exceptions/local_storage_exception.dart'; import 'package:tmail_ui_user/features/caching/utils/caching_constants.dart'; class SentryConfigurationCacheManager { - final SentryConfigurationCacheClient _cacheClient; - final String _cacheKey = CachingConstants.sentryConfigurationCacheKeyName; + final SentryConfigurationCacheClient _configurationCacheClient; + final SentryUserCacheClient _userCacheClient; + final String _configurationCacheKey = + CachingConstants.sentryConfigurationCacheKeyName; + final String _userCacheKey = CachingConstants.sentryUserCacheKeyName; - SentryConfigurationCacheManager(this._cacheClient); + SentryConfigurationCacheManager( + this._configurationCacheClient, + this._userCacheClient, + ); Future getSentryConfiguration() async { - final sentryConfigCache = await _cacheClient.getItem(_cacheKey); + final sentryConfigCache = + await _configurationCacheClient.getItem(_configurationCacheKey); if (sentryConfigCache == null) { throw NotFoundSentryConfigurationException(); } else { @@ -21,6 +30,24 @@ class SentryConfigurationCacheManager { Future saveSentryConfiguration( SentryConfigurationCache sentryConfigurationCache, ) async { - await _cacheClient.insertItem(_cacheKey, sentryConfigurationCache); + await _configurationCacheClient.insertItem( + _configurationCacheKey, + sentryConfigurationCache, + ); + } + + Future getSentryUser() async { + final sentryUserCache = await _userCacheClient.getItem(_userCacheKey); + if (sentryUserCache == null) { + throw NotFoundSentryUserException(); + } else { + return sentryUserCache; + } + } + + Future saveSentryUser( + SentryUserCache sentryUserCache, + ) async { + await _userCacheClient.insertItem(_userCacheKey, sentryUserCache); } } diff --git a/lib/features/caching/utils/caching_constants.dart b/lib/features/caching/utils/caching_constants.dart index c7f642c8b..c05505a7f 100644 --- a/lib/features/caching/utils/caching_constants.dart +++ b/lib/features/caching/utils/caching_constants.dart @@ -21,6 +21,7 @@ class CachingConstants { static const int SESSION_HIVE_CACHE_ID = 19; static const int OIDC_CONFIGURATION_CACHE_ID = 20; static const int SENTRY_CONFIGURATION_CACHE_ID = 21; + static const int SENTRY_USER_CACHE_ID = 22; static const String fcmCacheBoxName = 'fcm_cache_box'; static const String newEmailCacheBoxName = 'new_email_cache_box'; @@ -33,6 +34,7 @@ class CachingConstants { static const String oidcConfigurationCacheKeyName = 'oidc_configuration_cache_key'; static const String sentryConfigurationCacheKeyName = 'sentry_configuration_cache_key'; + static const String sentryUserCacheKeyName = 'sentry_user_cache_key'; static const String newEmailsContentFolderName = 'new_emails'; static const String openedEmailContentFolderName = 'opened_email'; diff --git a/lib/features/mailbox_dashboard/presentation/mixin/sentry_ecosystem_mixin.dart b/lib/features/mailbox_dashboard/presentation/mixin/sentry_ecosystem_mixin.dart index cc6e82a26..6e63d927d 100644 --- a/lib/features/mailbox_dashboard/presentation/mixin/sentry_ecosystem_mixin.dart +++ b/lib/features/mailbox_dashboard/presentation/mixin/sentry_ecosystem_mixin.dart @@ -43,7 +43,10 @@ mixin SentryEcosystemMixin { _setupSentryUser(); - await _saveSentryConfiguration(sentryConfig); + await _saveSentryConfiguration( + sentryConfig: sentryConfig, + sentryUser: _sentryUser, + ); } void _setupSentryUser() { @@ -52,12 +55,20 @@ mixin SentryEcosystemMixin { SentryManager.instance.setUser(_sentryUser!); } - Future _saveSentryConfiguration(SentryConfig sentryConfig) async { + Future _saveSentryConfiguration({ + required SentryConfig sentryConfig, + SentryUser? sentryUser, + }) async { final cachingManager = getBinding(); if (cachingManager != null) { await cachingManager.saveSentryConfiguration(sentryConfig); + + if (sentryUser != null) { + await cachingManager.saveSentryUser(sentryUser); + } } else { - logTrace('SentryEcosystemMixin::saveSentryConfiguration: CachingManager is null'); + logTrace( + 'SentryEcosystemMixin::saveSentryConfiguration: CachingManager is null'); } } } diff --git a/lib/features/push_notification/presentation/controller/fcm_message_controller.dart b/lib/features/push_notification/presentation/controller/fcm_message_controller.dart index 5492b44d1..4bef2e580 100644 --- a/lib/features/push_notification/presentation/controller/fcm_message_controller.dart +++ b/lib/features/push_notification/presentation/controller/fcm_message_controller.dart @@ -146,6 +146,14 @@ class FcmMessageController extends PushBaseController { } await SentryManager.instance.initializeWithSentryConfig(sentryConfig); + + final sentryUser = await cachingManager.getSentryUser(); + if (sentryUser == null) { + logTrace('FcmMessageController::setUpSentryConfiguration: Sentry user is null'); + return; + } + + SentryManager.instance.setUser(sentryUser); } catch (e, st) { logError( 'FcmMessageController::_initialAppConfig: throw exception', diff --git a/lib/main/bindings/local/local_bindings.dart b/lib/main/bindings/local/local_bindings.dart index f0477c809..4aa9b7fee 100644 --- a/lib/main/bindings/local/local_bindings.dart +++ b/lib/main/bindings/local/local_bindings.dart @@ -20,6 +20,7 @@ import 'package:tmail_ui_user/features/caching/clients/recent_login_username_cac import 'package:tmail_ui_user/features/caching/clients/recent_search_cache_client.dart'; import 'package:tmail_ui_user/features/caching/clients/sending_email_hive_cache_client.dart'; import 'package:tmail_ui_user/features/caching/clients/sentry_configuration_cache_client.dart'; +import 'package:tmail_ui_user/features/caching/clients/sentry_user_cache_client.dart'; import 'package:tmail_ui_user/features/caching/clients/session_hive_cache_client.dart'; import 'package:tmail_ui_user/features/caching/clients/state_cache_client.dart'; import 'package:tmail_ui_user/features/caching/clients/token_oidc_cache_client.dart'; @@ -99,9 +100,11 @@ class LocalBindings extends Bindings { Get.put(LocalSortOrderManager(Get.find())); Get.put(SettingCacheManager(Get.find())); Get.put(SentryConfigurationCacheClient()); + Get.put(SentryUserCacheClient()); Get.put( SentryConfigurationCacheManager( Get.find(), + Get.find(), ), ); Get.put(CachingManager(