TF-4268 Save Sentry user to cache to re-init in onBackground of FCM

This commit is contained in:
dab246 authored and Dat H. Pham committed 2026-02-24 14:44:10 +07:00
1 parent 2d84955845
commit ee84efba1d
12 files changed
+176 -9

No files matched your search

+35
View File
@@ -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<void> 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<SentryUser?> 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;
}
}
}
@@ -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<SentryUserCache> {
@override
String get tableName => CachingConstants.sentryConfigurationCacheBoxName;
@override
bool get encryption => true;
}
@@ -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<SentryUserCache>(
SentryUserCacheAdapter(),
CachingConstants.SENTRY_USER_CACHE_ID,
isolated: isolated,
);
}
void registerCacheAdapter<T>(
@@ -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<Object?> get props => [
id,
name,
username,
email,
];
}
@@ -1,3 +1,5 @@
class NotFoundDataWithThisKeyException implements Exception {}
class NotFoundSentryConfigurationException implements Exception {}
class NotFoundSentryConfigurationException implements Exception {}
class NotFoundSentryUserException implements Exception {}
@@ -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,
);
}
}
@@ -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 ?? '',
);
}
}
@@ -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<SentryConfigurationCache> 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<void> saveSentryConfiguration(
SentryConfigurationCache sentryConfigurationCache,
) async {
await _cacheClient.insertItem(_cacheKey, sentryConfigurationCache);
await _configurationCacheClient.insertItem(
_configurationCacheKey,
sentryConfigurationCache,
);
}
Future<SentryUserCache> getSentryUser() async {
final sentryUserCache = await _userCacheClient.getItem(_userCacheKey);
if (sentryUserCache == null) {
throw NotFoundSentryUserException();
} else {
return sentryUserCache;
}
}
Future<void> saveSentryUser(
SentryUserCache sentryUserCache,
) async {
await _userCacheClient.insertItem(_userCacheKey, sentryUserCache);
}
}
@@ -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';
@@ -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<void> _saveSentryConfiguration(SentryConfig sentryConfig) async {
Future<void> _saveSentryConfiguration({
required SentryConfig sentryConfig,
SentryUser? sentryUser,
}) async {
final cachingManager = getBinding<CachingManager>();
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');
}
}
}
@@ -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',
@@ -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<SharedPreferences>()));
Get.put(SettingCacheManager(Get.find<SharedPreferences>()));
Get.put(SentryConfigurationCacheClient());
Get.put(SentryUserCacheClient());
Get.put(
SentryConfigurationCacheManager(
Get.find<SentryConfigurationCacheClient>(),
Get.find<SentryUserCacheClient>(),
),
);
Get.put(CachingManager(