Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mob][photos] Ensure Uncategorized album after sync

ashil-pilot6c13d81c0095
diff --git a/mobile/apps/photos/lib/services/collections_service.dart b/mobile/apps/photos/lib/services/collections_service.dart
index b3e88b7b40..70e2d3aeee 100644
--- a/mobile/apps/photos/lib/services/collections_service.dart
+++ b/mobile/apps/photos/lib/services/collections_service.dart
@@ -41,7 +41,6 @@ import "package:photos/models/metadata/collection_magic.dart";
import "package:photos/service_locator.dart";
import 'package:photos/services/app_lifecycle_service.dart';
import "package:photos/services/favorites_service.dart";
-import "package:photos/services/hidden_service.dart";
import 'package:photos/services/memory_share_service.dart';
import 'package:photos/services/sync/local_sync_service.dart';
import 'package:photos/services/sync/remote_sync_service.dart';
@@ -81,7 +80,9 @@ class CollectionsService {
final _cachedUserIdToUser = <int, User>{};
Collection? cachedDefaultHiddenCollection;
Future<Map<int, int>>? _collectionIDToNewestFileTime;
- Collection? cachedUncategorizedCollection;
+ Collection? _cachedUncategorizedCollection;
+ Future<Collection>? _uncategorizedCollectionFuture;
+ int? _uncategorizedCollectionFutureOwnerID;
final Map<String, EnteFile> _coverCache = <String, EnteFile>{};
final Map<int, int> _countCache = <int, int>{};
@@ -153,6 +154,7 @@ class CollectionsService {
watch.log("remote fetch collections ${fetchedCollections.length}");
if (fetchedCollections.isEmpty) {
+ await _ensureUncategorizedCollection();
return;
}
final updatedCollections = <Collection>[];
@@ -211,13 +213,17 @@ class CollectionsService {
),
);
}
+
+ await _ensureUncategorizedCollection();
}
void clearCache() {
_localPathToCollectionID.clear();
_collectionIDToCollections.clear();
cachedDefaultHiddenCollection = null;
- cachedUncategorizedCollection = null;
+ _cachedUncategorizedCollection = null;
+ _uncategorizedCollectionFuture = null;
+ _uncategorizedCollectionFutureOwnerID = null;
_cachedPublicAlbumToken.clear();
_cachedPublicAlbumJWT.clear();
_cachedPublicCollectionID.clear();
@@ -537,6 +543,69 @@ class CollectionsService {
.toList();
}
+ Future<Collection> getUncategorizedCollection() {
+ final int? userID = _config.getUserID();
+ if (userID == null) {
+ return Future.error(
+ StateError("Cannot get Uncategorized without a configured user"),
+ );
+ }
+
+ final cachedCollection = _cachedUncategorizedCollection;
+ if (cachedCollection != null &&
+ !cachedCollection.isDeleted &&
+ cachedCollection.isOwner(userID)) {
+ return Future.value(cachedCollection);
+ }
+ _cachedUncategorizedCollection = null;
+
+ final matchedCollection = _collectionIDToCollections.values
+ .firstWhereOrNull(
+ (collection) =>
+ !collection.isDeleted &&
+ collection.type == CollectionType.uncategorized &&
+ collection.isOwner(userID),
+ );
+ if (matchedCollection != null) {
+ _cachedUncategorizedCollection = matchedCollection;
+ return Future.value(matchedCollection);
+ }
+
+ final inFlightCreation = _uncategorizedCollectionFuture;
+ if (inFlightCreation != null &&
+ _uncategorizedCollectionFutureOwnerID == userID) {
+ return inFlightCreation;
+ }
+
+ late final Future<Collection> creation;
+ creation = _createUncategorizedCollection()
+ .then((collection) {
+ if (_config.getUserID() == userID) {
+ _cachedUncategorizedCollection = collection;
+ }
+ return collection;
+ })
+ .whenComplete(() {
+ if (identical(_uncategorizedCollectionFuture, creation)) {
+ _uncategorizedCollectionFuture = null;
+ _uncategorizedCollectionFutureOwnerID = null;
+ }
+ });
+ _uncategorizedCollectionFutureOwnerID = userID;
+ _uncategorizedCollectionFuture = creation;
+ return creation;
+ }
+
+ Future<void> _ensureUncategorizedCollection() async {
+ try {
+ await getUncategorizedCollection();
+ } catch (e, s) {
+ // This is retried by the next collection sync. An empty system album
+ // should not prevent remote collections and files from being synced.
+ _logger.warning("Failed to ensure Uncategorized collection", e, s);
+ }
+ }
+
Set<int> nonHiddenOwnedCollections() {
final int ownerID = _config.getUserID()!;
return _collectionIDToCollections.values
@@ -2560,6 +2629,28 @@ class CollectionsService {
return cachedCollection;
}
+ Future<Collection> _createUncategorizedCollection() async {
+ final uncategorizedCollectionKey = CryptoUtil.generateKey();
+ final encKey = CryptoUtil.encryptSync(
+ uncategorizedCollectionKey,
+ _config.getKey()!,
+ );
+ final encName = CryptoUtil.encryptSync(
+ utf8.encode("Uncategorized"),
+ uncategorizedCollectionKey,
+ );
+ return createAndCacheCollection(
+ CreateRequest(
+ encryptedKey: CryptoUtil.bin2base64(encKey.encryptedData!),
+ keyDecryptionNonce: CryptoUtil.bin2base64(encKey.nonce!),
+ encryptedName: CryptoUtil.bin2base64(encName.encryptedData!),
+ nameDecryptionNonce: CryptoUtil.bin2base64(encName.nonce!),
+ type: CollectionType.uncategorized,
+ attributes: CollectionAttributes(),
+ ),
+ );
+ }
+
@Deprecated("Use _cacheLocalPathAndCollection instead")
Collection _cacheCollectionAttributes(Collection collection) {
final String decryptedName = _getDecryptedCollectionName(collection);
diff --git a/mobile/apps/photos/lib/services/hidden_service.dart b/mobile/apps/photos/lib/services/hidden_service.dart
index bc8d627ba0..ac4e48c1a5 100644
--- a/mobile/apps/photos/lib/services/hidden_service.dart
+++ b/mobile/apps/photos/lib/services/hidden_service.dart
@@ -1,7 +1,6 @@
import "dart:async";
import 'dart:convert';
-import 'package:collection/collection.dart';
import 'package:ente_crypto/ente_crypto.dart';
import "package:ente_strings/ente_strings.dart";
import 'package:flutter/material.dart';
@@ -127,24 +126,6 @@ extension HiddenService on CollectionsService {
return defaultHidden;
}
- Future<Collection> getUncategorizedCollection() async {
- if (cachedUncategorizedCollection != null) {
- return cachedUncategorizedCollection!;
- }
- final int userID = config.getUserID()!;
- final Collection? matchedCollection = collectionIDToCollections.values
- .firstWhereOrNull(
- (element) =>
- element.type == CollectionType.uncategorized &&
- element.owner.id == userID,
- );
- if (matchedCollection != null) {
- cachedUncategorizedCollection = matchedCollection;
- return cachedUncategorizedCollection!;
- }
- return _createUncategorizedCollection();
- }
-
Future<void> movePendingRemovalActionsToUncategorized() async {
try {
final pendingActions = await fetchPendingRemovalActions();
@@ -320,30 +301,6 @@ extension HiddenService on CollectionsService {
return collectionFromServer;
}
- Future<Collection> _createUncategorizedCollection() async {
- final uncategorizedCollectionKey = CryptoUtil.generateKey();
- final encKey = CryptoUtil.encryptSync(
- uncategorizedCollectionKey,
- config.getKey()!,
- );
- final encName = CryptoUtil.encryptSync(
- utf8.encode("Uncategorized"),
- uncategorizedCollectionKey,
- );
- final collection = await createAndCacheCollection(
- CreateRequest(
- encryptedKey: CryptoUtil.bin2base64(encKey.encryptedData!),
- keyDecryptionNonce: CryptoUtil.bin2base64(encKey.nonce!),
- encryptedName: CryptoUtil.bin2base64(encName.encryptedData!),
- nameDecryptionNonce: CryptoUtil.bin2base64(encName.nonce!),
- type: CollectionType.uncategorized,
- attributes: CollectionAttributes(),
- ),
- );
- cachedUncategorizedCollection = collection;
- return cachedUncategorizedCollection!;
- }
-
Future<CreateRequest> buildCollectionCreateRequest(
String name, {
required int visibility,
diff --git a/mobile/apps/photos/lib/ui/actions/collection/collection_file_actions.dart b/mobile/apps/photos/lib/ui/actions/collection/collection_file_actions.dart
index d40ca79da9..cad94c9993 100644
--- a/mobile/apps/photos/lib/ui/actions/collection/collection_file_actions.dart
+++ b/mobile/apps/photos/lib/ui/actions/collection/collection_file_actions.dart
@@ -13,7 +13,6 @@ import 'package:photos/models/selected_files.dart';
import "package:photos/module/upload/service/file_uploader.dart";
import "package:photos/services/collections_service.dart";
import 'package:photos/services/favorites_service.dart';
-import "package:photos/services/hidden_service.dart";
import "package:photos/services/ignored_files_service.dart";
import "package:photos/services/sync/remote_sync_service.dart";
import 'package:photos/ui/actions/collection/collection_sharing_actions.dart';
diff --git a/mobile/apps/photos/lib/ui/tabs/albums/albums_manage_sheet.dart b/mobile/apps/photos/lib/ui/tabs/albums/albums_manage_sheet.dart
index 1a0c050a94..f6d3278ef8 100644
--- a/mobile/apps/photos/lib/ui/tabs/albums/albums_manage_sheet.dart
+++ b/mobile/apps/photos/lib/ui/tabs/albums/albums_manage_sheet.dart
@@ -10,7 +10,6 @@ import "package:hugeicons/hugeicons.dart";
import "package:photos/core/configuration.dart";
import "package:photos/models/collection/collection.dart";
import "package:photos/services/collections_service.dart";
-import "package:photos/services/hidden_service.dart";
import "package:photos/ui/tabs/shared/all_links_page.dart";
import "package:photos/ui/viewer/gallery/archive_page.dart";
import "package:photos/ui/viewer/gallery/hidden_page.dart";
diff --git a/mobile/apps/photos/lib/ui/viewer/file_details/upload_icon_widget.dart b/mobile/apps/photos/lib/ui/viewer/file_details/upload_icon_widget.dart
index 13ff38ddd9..4a5604c8ca 100644
--- a/mobile/apps/photos/lib/ui/viewer/file_details/upload_icon_widget.dart
+++ b/mobile/apps/photos/lib/ui/viewer/file_details/upload_icon_widget.dart
@@ -14,7 +14,6 @@ import "package:photos/events/files_updated_event.dart";
import 'package:photos/models/file/file.dart';
import "package:photos/models/ignored_file.dart";
import "package:photos/services/collections_service.dart";
-import "package:photos/services/hidden_service.dart";
import "package:photos/services/ignored_files_service.dart";
import "package:photos/services/sync/remote_sync_service.dart";
import "package:photos/services/sync/sync_service.dart";