Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mob][photos] Handle public album link 429 with typed errors

Neeraj Gupta802b0596b675
diff --git a/mobile/apps/photos/lib/gateways/collections/collection_share_gateway.dart b/mobile/apps/photos/lib/gateways/collections/collection_share_gateway.dart
index 522f27d3ee..1564c4bfcf 100644
--- a/mobile/apps/photos/lib/gateways/collections/collection_share_gateway.dart
+++ b/mobile/apps/photos/lib/gateways/collections/collection_share_gateway.dart
@@ -140,14 +140,38 @@ class CollectionShareGateway {
/// [authToken] - The public access token from the share URL.
///
/// Returns the raw response data containing collection information.
+ ///
+ /// Throws:
+ /// - [PublicCollectionInfoUnauthorizedException] when the token is invalid.
+ /// - [PublicCollectionInfoExpiredException] when the link has expired/disabled.
+ /// - [PublicCollectionDeviceLimitExceededException] when the link device
+ /// limit is reached.
+ /// - [PublicCollectionRateLimitedException] when API rate limits are hit.
Future<Map<String, dynamic>> getPublicCollectionInfo(String authToken) async {
- final response = await _enteDio.get(
- "/public-collection/info",
- options: Options(
- headers: {"X-Auth-Access-Token": authToken},
- ),
- );
- return response.data;
+ try {
+ final response = await _enteDio.get(
+ "/public-collection/info",
+ options: Options(
+ headers: {"X-Auth-Access-Token": authToken},
+ ),
+ );
+ return response.data;
+ } on DioException catch (e) {
+ switch (e.response?.statusCode) {
+ case 401:
+ throw PublicCollectionInfoUnauthorizedException();
+ case 410:
+ throw PublicCollectionInfoExpiredException();
+ case 429:
+ final errorMessage = _extractErrorMessage(e.response?.data);
+ if (errorMessage?.toLowerCase().contains("device limit") ?? false) {
+ throw PublicCollectionDeviceLimitExceededException();
+ }
+ throw PublicCollectionRateLimitedException();
+ default:
+ rethrow;
+ }
+ }
}
/// Verifies the password for a password-protected public collection.
@@ -188,3 +212,21 @@ class CollectionShareGateway {
return response.data;
}
}
+
+String? _extractErrorMessage(dynamic data) {
+ if (data is Map<String, dynamic>) {
+ return data["error"]?.toString();
+ }
+ if (data is Map) {
+ return data["error"]?.toString();
+ }
+ return null;
+}
+
+class PublicCollectionInfoUnauthorizedException implements Exception {}
+
+class PublicCollectionInfoExpiredException implements Exception {}
+
+class PublicCollectionDeviceLimitExceededException implements Exception {}
+
+class PublicCollectionRateLimitedException implements Exception {}
diff --git a/mobile/apps/photos/lib/l10n/intl_en.arb b/mobile/apps/photos/lib/l10n/intl_en.arb
index 72b17bd548..8181383dfd 100644
--- a/mobile/apps/photos/lib/l10n/intl_en.arb
+++ b/mobile/apps/photos/lib/l10n/intl_en.arb
@@ -290,6 +290,7 @@
"copyLink": "Copy link",
"copyEmbedHtml": "Copy embed HTML",
"linkHasExpired": "Link has expired",
+ "linkRequestLimitExceeded": "This album has been viewed on too many devices",
"publicLinkEnabled": "Public link enabled",
"shareALink": "Share a link",
"sharedAlbumSectionDescription": "Create shared and collaborative albums with other Ente users, including users on free plans.",
diff --git a/mobile/apps/photos/lib/services/collections_service.dart b/mobile/apps/photos/lib/services/collections_service.dart
index 3bec27d04b..2c091a65c7 100644
--- a/mobile/apps/photos/lib/services/collections_service.dart
+++ b/mobile/apps/photos/lib/services/collections_service.dart
@@ -23,6 +23,7 @@ import 'package:photos/events/collection_updated_event.dart';
import 'package:photos/events/files_updated_event.dart';
import 'package:photos/events/force_reload_home_gallery_event.dart';
import 'package:photos/events/local_photos_updated_event.dart';
+import "package:photos/gateways/collections/collection_share_gateway.dart";
import 'package:photos/gateways/collections/models/collection_file_item.dart';
import 'package:photos/gateways/collections/models/create_request.dart';
import "package:photos/gateways/collections/models/metadata.dart";
@@ -1491,7 +1492,7 @@ class CollectionsService {
}
}
- Future<Collection> getCollectionFromPublicLink(
+ Future<Collection?> getCollectionFromPublicLink(
BuildContext context,
Uri uri,
) async {
@@ -1530,22 +1531,43 @@ class CollectionsService {
collection.setName(_getDecryptedCollectionName(collection));
return collection;
+ } on PublicCollectionInfoExpiredException catch (e, s) {
+ _logger.warning("Public collection link expired", e, s);
+ await showInfoDialog(
+ context,
+ title: AppLocalizations.of(context).linkExpired,
+ body:
+ AppLocalizations.of(context).theLinkYouAreTryingToAccessHasExpired,
+ );
+ return null;
+ } on PublicCollectionDeviceLimitExceededException catch (e, s) {
+ _logger.warning("Public collection link device limit reached", e, s);
+ await showErrorDialog(
+ context,
+ AppLocalizations.of(context).canNotOpenTitle,
+ AppLocalizations.of(context).linkRequestLimitExceeded,
+ );
+ return null;
+ } on PublicCollectionRateLimitedException catch (e, s) {
+ _logger.warning("Public collection link request rate limited", e, s);
+ await showErrorDialog(
+ context,
+ AppLocalizations.of(context).canNotOpenTitle,
+ AppLocalizations.of(context).linkRequestLimitExceeded,
+ );
+ return null;
+ } on PublicCollectionInfoUnauthorizedException catch (e, s) {
+ _logger.warning("Public collection link is unauthorized", e, s);
+ await showErrorDialog(
+ context,
+ AppLocalizations.of(context).canNotOpenTitle,
+ AppLocalizations.of(context).canNotOpenBody,
+ );
+ return null;
} catch (e, s) {
_logger.warning(e, s);
_logger.severe("Failed to fetch public collection");
- if (e is DioException && e.response?.statusCode == 410) {
- await showInfoDialog(
- context,
- title: AppLocalizations.of(context).linkExpired,
- body: AppLocalizations.of(context)
- .theLinkYouAreTryingToAccessHasExpired,
- );
- throw UnauthorizedError();
- }
await showGenericErrorDialog(context: context, error: e);
- if (e is DioException && e.response?.statusCode == 401) {
- throw UnauthorizedError();
- }
rethrow;
}
}
diff --git a/mobile/apps/photos/lib/ui/tabs/home_widget.dart b/mobile/apps/photos/lib/ui/tabs/home_widget.dart
index f326e51aec..a23720432a 100644
--- a/mobile/apps/photos/lib/ui/tabs/home_widget.dart
+++ b/mobile/apps/photos/lib/ui/tabs/home_widget.dart
@@ -355,8 +355,11 @@ class _HomeWidgetState extends State<HomeWidget> {
}
_linkedPublicAlbums[uri] = (isInitialStream, currentTime);
- final Collection collection = await CollectionsService.instance
+ final Collection? collection = await CollectionsService.instance
.getCollectionFromPublicLink(context, uri);
+ if (collection == null) {
+ return;
+ }
final existingCollection =
CollectionsService.instance.getCollectionByID(collection.id);