Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Clear Rewind cache on logout

laurenspriem7243c11ced05
diff --git a/mobile/apps/photos/lib/core/configuration.dart b/mobile/apps/photos/lib/core/configuration.dart
index 479b9468e6..bd136cc086 100644
--- a/mobile/apps/photos/lib/core/configuration.dart
+++ b/mobile/apps/photos/lib/core/configuration.dart
@@ -251,6 +251,18 @@ class Configuration {
);
}
+ // Reset Ente Rewind caches and services
+ try {
+ wrappedService.resetForLogout();
+ } catch (e) {
+ _logger.info("WrappedService not initialized or failed to reset", e);
+ }
+ try {
+ await wrappedCacheService.clearAll();
+ } catch (e) {
+ _logger.info("WrappedCacheService not initialized or failed to clear", e);
+ }
+
if (!autoLogout) {
// Following services won't be initialized if it's the case of autoLogout
FileUploader.instance.clearCachedUploadURLs();
diff --git a/mobile/apps/photos/lib/services/wrapped/wrapped_cache_service.dart b/mobile/apps/photos/lib/services/wrapped/wrapped_cache_service.dart
index a2abc57ed6..15b0efea90 100644
--- a/mobile/apps/photos/lib/services/wrapped/wrapped_cache_service.dart
+++ b/mobile/apps/photos/lib/services/wrapped/wrapped_cache_service.dart
@@ -18,13 +18,16 @@ class WrappedCacheService {
Directory? _cacheDirectory;
+ Future<Directory> _cacheDirectoryPath() async {
+ final Directory baseDir = await getApplicationSupportDirectory();
+ return Directory(path.join(baseDir.path, "wrapped_cache"));
+ }
+
Future<Directory> _ensureCacheDirectory() async {
if (_cacheDirectory != null) {
return _cacheDirectory!;
}
- final Directory baseDir = await getApplicationSupportDirectory();
- final Directory directory =
- Directory(path.join(baseDir.path, "wrapped_cache"));
+ final Directory directory = await _cacheDirectoryPath();
if (!await directory.exists()) {
await directory.create(recursive: true);
}
@@ -107,4 +110,26 @@ class WrappedCacheService {
}
});
}
+
+ Future<void> clearAll() {
+ return _lock.synchronized(() async {
+ final Directory directory = await _cacheDirectoryPath();
+ if (!await directory.exists()) {
+ _cacheDirectory = null;
+ return;
+ }
+ try {
+ await directory.delete(recursive: true);
+ _logger.info("Deleted Wrapped cache directory at ${directory.path}");
+ } catch (error, stackTrace) {
+ _logger.warning(
+ "Failed to delete Wrapped cache directory at ${directory.path}",
+ error,
+ stackTrace,
+ );
+ } finally {
+ _cacheDirectory = null;
+ }
+ });
+ }
}
diff --git a/mobile/apps/photos/lib/services/wrapped/wrapped_service.dart b/mobile/apps/photos/lib/services/wrapped/wrapped_service.dart
index 17e1f31012..fac01e4cdf 100644
--- a/mobile/apps/photos/lib/services/wrapped/wrapped_service.dart
+++ b/mobile/apps/photos/lib/services/wrapped/wrapped_service.dart
@@ -63,6 +63,7 @@ class WrappedService {
final WrappedCacheService _cacheService;
final Lock _computeLock;
final ValueNotifier<WrappedEntryState> _state;
+ int _sessionGeneration = 0;
bool _initialLoadScheduled = false;
ValueListenable<WrappedEntryState> get stateListenable => _state;
@@ -86,6 +87,18 @@ class WrappedService {
bool get shouldShowDiscoveryEntry =>
isEnabled && state.hasResult && state.isComplete;
+ void resetForLogout() {
+ _logger.info("Resetting Wrapped state for logout");
+ _sessionGeneration++;
+ _initialLoadScheduled = false;
+ WrappedMediaPreloader.instance.reset();
+ _state.value = const WrappedEntryState(
+ result: null,
+ resumeIndex: 0,
+ isComplete: false,
+ );
+ }
+
void scheduleInitialLoad() {
if (!isEnabled) {
return;
@@ -95,15 +108,35 @@ class WrappedService {
}
_initialLoadScheduled = true;
_logger.info("Scheduling Wrapped initial load after $_kInitialDelay");
- unawaited(Future<void>.delayed(_kInitialDelay, _bootstrap));
+ final int generation = _sessionGeneration;
+ unawaited(
+ Future<void>.delayed(
+ _kInitialDelay,
+ () async {
+ if (generation != _sessionGeneration) {
+ return;
+ }
+ await _bootstrap();
+ },
+ ),
+ );
}
Future<void> _bootstrap() async {
+ final int generation = _sessionGeneration;
try {
final WrappedResult? cached =
await _cacheService.read(year: _kWrappedYear);
+ if (generation != _sessionGeneration) {
+ _logger.info("Wrapped bootstrap aborted due to session change");
+ return;
+ }
if (cached != null) {
await WrappedMediaPreloader.instance.prime(cached);
+ if (generation != _sessionGeneration) {
+ _logger.info("Wrapped bootstrap aborted due to session change");
+ return;
+ }
_logger.info(
"Loaded Wrapped cache for $_kWrappedYear with "
"${cached.cards.length} cards",
@@ -121,6 +154,10 @@ class WrappedService {
_logger.severe("Failed to load Wrapped cache", error, stackTrace);
}
+ if (generation != _sessionGeneration) {
+ _logger.info("Wrapped bootstrap aborted due to session change");
+ return;
+ }
if (!isEnabled) {
_logger.info(
"Wrapped unavailable; skipping initial compute for $_kWrappedYear",
@@ -156,6 +193,7 @@ class WrappedService {
required String reason,
required bool bypassFlag,
}) async {
+ final int generation = _sessionGeneration;
if (!bypassFlag && !isEnabled) {
_logger.info(
"Wrapped unavailable; skipping compute for $_kWrappedYear ($reason)",
@@ -167,8 +205,26 @@ class WrappedService {
try {
final WrappedResult result =
await WrappedEngine.compute(year: _kWrappedYear);
+ if (generation != _sessionGeneration) {
+ _logger.info(
+ "Discarding Wrapped compute result ($reason) due to session change",
+ );
+ return;
+ }
await WrappedMediaPreloader.instance.prime(result);
+ if (generation != _sessionGeneration) {
+ _logger.info(
+ "Discarding Wrapped compute result ($reason) due to session change",
+ );
+ return;
+ }
await _cacheService.write(result: result);
+ if (generation != _sessionGeneration) {
+ _logger.info(
+ "Discarding Wrapped compute result ($reason) due to session change",
+ );
+ return;
+ }
updateResult(result);
_logger.info("Wrapped compute completed ($reason)");
} catch (error, stackTrace) {