Skip to content

armgit / ente

Public Imported from GitHub
0

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

Cache public keys for email

Neeraj Guptaaec098022137
diff --git a/mobile/apps/photos/lib/services/account/user_service.dart b/mobile/apps/photos/lib/services/account/user_service.dart
index 3d0c9a94ab..3543aa4cc7 100644
--- a/mobile/apps/photos/lib/services/account/user_service.dart
+++ b/mobile/apps/photos/lib/services/account/user_service.dart
@@ -45,6 +45,7 @@ import 'package:photos/ui/notification/toast.dart';
import "package:photos/ui/tabs/home_widget.dart";
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/navigation_util.dart';
+import "package:photos/utils/standalone/timed_cache.dart";
import "package:pointycastle/export.dart";
import "package:pointycastle/srp/srp6_client.dart";
import "package:pointycastle/srp/srp6_standard_groups.dart";
@@ -60,6 +61,9 @@ class UserService {
final SRP6GroupParameters kDefaultSrpGroup = SRP6StandardGroups.rfc5054_4096;
final _dio = NetworkClient.instance.getDio();
+ final _emailToPubKeyCache =
+ TimedCache<String, String>(duration: const Duration(seconds: 10));
+
final _enteDio = NetworkClient.instance.enteDio;
final _logger = Logger((UserService).toString());
final _config = Configuration.instance;
@@ -181,12 +185,17 @@ class UserService {
// getPublicKey returns null value if email id is not
// associated with another ente account
Future<String?> getPublicKey(String email) async {
+ final String? cachedPubKey = _emailToPubKeyCache.get(email);
+ if (cachedPubKey != null) {
+ return cachedPubKey;
+ }
try {
final response = await _enteDio.get(
"/users/public-key",
queryParameters: {"email": email},
);
final publicKey = response.data["publicKey"];
+ _emailToPubKeyCache.set(email, publicKey);
return publicKey;
} on DioException catch (e) {
if (e.response != null && e.response?.statusCode == 404) {
diff --git a/mobile/apps/photos/lib/utils/standalone/timed_cache.dart b/mobile/apps/photos/lib/utils/standalone/timed_cache.dart
new file mode 100644
index 0000000000..5745d0afda
--- /dev/null
+++ b/mobile/apps/photos/lib/utils/standalone/timed_cache.dart
@@ -0,0 +1,25 @@
+class TimedCache<K, V> {
+ final Map<K, (V?, DateTime)> _cache = {};
+ final Duration duration;
+
+ TimedCache({required this.duration});
+
+ V? get(K key) {
+ if (!_cache.containsKey(key)) return null;
+
+ final (value, timestamp) = _cache[key]!;
+ if (DateTime.now().difference(timestamp) > duration) {
+ _cache.remove(key);
+ return null;
+ }
+
+ return value;
+ }
+
+ void set(K key, V? value) {
+ _cache[key] = (value, DateTime.now());
+ }
+
+ void clear() => _cache.clear();
+ void remove(K key) => _cache.remove(key);
+}
diff --git a/mobile/apps/photos/scripts/internal_changes.txt b/mobile/apps/photos/scripts/internal_changes.txt
index 81ff1e824a..2d10e88ae4 100644
--- a/mobile/apps/photos/scripts/internal_changes.txt
+++ b/mobile/apps/photos/scripts/internal_changes.txt
@@ -1,3 +1,4 @@
+- Neeraj: Cache public-key for email to avoid rate-limit when sharing bulk albums.
- Laurens: WIP first iteration of Ente Rewind
- Prateek: (i) Optimize home widget image quality with V2 implementation (1024px vs 512px, uses PhotoManager thumbnails for local files, isolate processing for remote images)
- Neeraj: (i) Safeguards for Bitflip errors (md5, immedidate decryption)