From 6db4615efa2bcb1eb4ffd02bbfb876203a15e559 Mon Sep 17 00:00:00 2001 From: Dang Dat Date: Wed, 23 Sep 2026 10:43:41 +0700 Subject: [PATCH] TF-4827 Add a recovery seam to the size-rejection path Ref: #4848 TF-4827 Route inline files to the upload recovery Addresses: 4182803889 https://github.com/linagora/tmail-flutter/pull/4910#discussion_r4182803889 TF-4827 Skip the upload recovery when a rejection carries no files Addresses: 4191301057 https://github.com/linagora/tmail-flutter/pull/4910#discussion_r4191301057 TF-4827 Document the upload recovery contract Addresses: 4191300814, 4191300935, 4191301151 https://github.com/linagora/tmail-flutter/pull/4910#discussion_r4191300814 https://github.com/linagora/tmail-flutter/pull/4910#discussion_r4191300935 https://github.com/linagora/tmail-flutter/pull/4910#discussion_r4191301151 TF-4827 Align ADR-0109 with inline files reaching the recovery Addresses: 4191308467 https://github.com/linagora/tmail-flutter/pull/4910#discussion_r4191308467 --- .../0109-oversize-attachment-recovery-seam.md | 6 +- .../validator/attachment_upload_request.dart | 10 +- .../attachment_upload_request_factory.dart | 1 + .../validator/attachment_upload_gate.dart | 20 +++ .../validator/attachment_upload_recovery.dart | 16 ++ .../attachment_upload_validation_service.dart | 12 +- .../validator/validation_pipeline_test.dart | 6 +- .../attachment_upload_gate_test.dart | 137 ++++++++++++++++++ 8 files changed, 200 insertions(+), 8 deletions(-) create mode 100644 lib/features/upload/presentation/validator/attachment_upload_recovery.dart diff --git a/docs/adr/0109-oversize-attachment-recovery-seam.md b/docs/adr/0109-oversize-attachment-recovery-seam.md index bc53417d2..d94b5fb62 100644 --- a/docs/adr/0109-oversize-attachment-recovery-seam.md +++ b/docs/adr/0109-oversize-attachment-recovery-seam.md @@ -62,12 +62,12 @@ Accepted - The rule rejects on the running total, so a rejected batch can hold no oversize file at all: 24 MB already attached plus a 2 MB file under a 25 MB cap. - A recovery therefore takes the whole rejected batch, not the files over the cap. -- Inline files are excluded: a pasted screenshot stays in the body, never becomes a link. -- A batch with no regular file carries nothing to act on, so it shows the dialog. +- Inline files are included: an oversize pasted image reaches the recovery like any picked file. +- A recovery that does not handle a batch returns false, so the dialog shows. ``` on ValidationRejected(failure): - if request.regularFiles.isNotEmpty: + if request.files.isNotEmpty: try: if await recovery?.recover(failure, request) == true: return false # recovery owns the UX from here diff --git a/lib/features/upload/domain/validator/attachment_upload_request.dart b/lib/features/upload/domain/validator/attachment_upload_request.dart index 7eaf7122f..d0aba886a 100644 --- a/lib/features/upload/domain/validator/attachment_upload_request.dart +++ b/lib/features/upload/domain/validator/attachment_upload_request.dart @@ -1,4 +1,5 @@ +import 'package:model/upload/file_info.dart'; import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_failure.dart'; import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_limits.dart'; import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_prompt.dart'; @@ -11,7 +12,14 @@ final class AttachmentUploadRequest { final AttachmentUploadSizeSnapshot sizes; final AttachmentUploadLimits limits; - const AttachmentUploadRequest({required this.sizes, required this.limits}); + /// Empty when built from byte counts only (re-attaching an uploaded attachment). + final List files; + + AttachmentUploadRequest({ + required this.sizes, + required this.limits, + Iterable files = const [], + }) : files = List.unmodifiable(files); } typedef AttachmentUploadDecision = ValidationDecision; diff --git a/lib/features/upload/domain/validator/attachment_upload_request_factory.dart b/lib/features/upload/domain/validator/attachment_upload_request_factory.dart index f19fff2d4..0927cf22a 100644 --- a/lib/features/upload/domain/validator/attachment_upload_request_factory.dart +++ b/lib/features/upload/domain/validator/attachment_upload_request_factory.dart @@ -25,6 +25,7 @@ final class AttachmentUploadRequestFactory { proposedRegularAttachmentBytes: proposedRegularBytes, ), limits: _limitsOf(state), + files: candidate, ); } diff --git a/lib/features/upload/presentation/validator/attachment_upload_gate.dart b/lib/features/upload/presentation/validator/attachment_upload_gate.dart index 68c237b23..295320852 100644 --- a/lib/features/upload/presentation/validator/attachment_upload_gate.dart +++ b/lib/features/upload/presentation/validator/attachment_upload_gate.dart @@ -1,6 +1,9 @@ +import 'package:core/utils/app_logger.dart'; +import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_failure.dart'; import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_request.dart'; import 'package:tmail_ui_user/features/upload/domain/validator/validation_decision.dart'; +import 'package:tmail_ui_user/features/upload/presentation/validator/attachment_upload_recovery.dart'; import 'package:tmail_ui_user/features/upload/presentation/validator/attachment_validation_feedback.dart'; final class AttachmentUploadGate { @@ -11,11 +14,15 @@ final class AttachmentUploadGate { Future permits({ required AttachmentUploadRequest request, required AttachmentValidationFeedback? Function() feedbackFactory, + AttachmentUploadRecovery? Function()? recoveryFactory, }) async { switch (_validator.validate(request)) { case ValidationAllowed(): return true; case ValidationRejected(:final failure): + // Byte-only re-attach has no file for a recovery to act on. + final recovery = request.files.isNotEmpty ? recoveryFactory?.call() : null; + if (recovery != null && await _recovered(recovery, failure, request)) return false; final feedback = feedbackFactory(); if (feedback == null) return false; await feedback.showFailure(failure); @@ -26,4 +33,17 @@ final class AttachmentUploadGate { return feedback.confirmAll(prompts); } } + + Future _recovered( + AttachmentUploadRecovery recovery, + AttachmentUploadFailure failure, + AttachmentUploadRequest request, + ) async { + try { + return await recovery.recover(failure, request); + } catch (e) { + logError('AttachmentUploadGate::_recovered: $e'); + return false; + } + } } diff --git a/lib/features/upload/presentation/validator/attachment_upload_recovery.dart b/lib/features/upload/presentation/validator/attachment_upload_recovery.dart new file mode 100644 index 000000000..75eb1d5d9 --- /dev/null +++ b/lib/features/upload/presentation/validator/attachment_upload_recovery.dart @@ -0,0 +1,16 @@ +import 'package:flutter/material.dart'; +import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_failure.dart'; +import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_request.dart'; + +/// An alternative to the failure dialog, e.g. upload oversize to Drive instead. +/// +/// Called only for rejections with files. False (not handled) shows the dialog. +/// True means it took over and owns all feedback; completes at takeover, not +/// at upload end. A throw declines only before any UI, after that own errors. +abstract interface class AttachmentUploadRecovery { + Future recover(AttachmentUploadFailure failure, AttachmentUploadRequest request); +} + +/// Only assigns fields; lookups go in `recover`, inside the gate's try. +typedef AttachmentUploadRecoveryBuilder = + AttachmentUploadRecovery Function(BuildContext context); diff --git a/lib/features/upload/presentation/validator/attachment_upload_validation_service.dart b/lib/features/upload/presentation/validator/attachment_upload_validation_service.dart index be342eb3c..c2ed63991 100644 --- a/lib/features/upload/presentation/validator/attachment_upload_validation_service.dart +++ b/lib/features/upload/presentation/validator/attachment_upload_validation_service.dart @@ -7,6 +7,7 @@ import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_request_factory.dart'; import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_state_source.dart'; import 'package:tmail_ui_user/features/upload/presentation/validator/attachment_upload_gate.dart'; +import 'package:tmail_ui_user/features/upload/presentation/validator/attachment_upload_recovery.dart'; import 'package:tmail_ui_user/features/upload/presentation/validator/attachment_validation_feedback.dart'; import 'package:tmail_ui_user/features/upload/presentation/validator/attachment_validation_feedback_impl.dart'; import 'package:tmail_ui_user/main/routes/route_navigation.dart'; @@ -29,15 +30,18 @@ class AttachmentUploadValidationService { final AttachmentUploadStateSource _stateSource; final AttachmentUploadGate _gate; final AttachmentValidationFeedbackBuilder _feedbackBuilder; + final AttachmentUploadRecoveryBuilder? _recoveryBuilder; AttachmentUploadValidationService({ required AttachmentUploadStateSource stateSource, AttachmentUploadValidator? validator, AttachmentValidationFeedbackBuilder? feedbackBuilder, + AttachmentUploadRecoveryBuilder? recoveryBuilder, }) : _stateSource = stateSource, _gate = AttachmentUploadGate( validator ?? AttachmentUploadValidator([const AttachmentSizeLimitRule()])), - _feedbackBuilder = feedbackBuilder ?? AttachmentValidationFeedbackImpl.new; + _feedbackBuilder = feedbackBuilder ?? AttachmentValidationFeedbackImpl.new, + _recoveryBuilder = recoveryBuilder; /// Bytes still attachable under the server cap, `null` when it advertises none. int? get remainingCapacityBytes { @@ -104,6 +108,12 @@ class AttachmentUploadValidationService { final resolvedContext = context ?? currentContext; return resolvedContext == null ? null : _feedbackBuilder(resolvedContext); }, + recoveryFactory: () { + final resolvedContext = context ?? currentContext; + return resolvedContext == null || _recoveryBuilder == null + ? null + : _recoveryBuilder(resolvedContext); + }, ); if (allowed) { onAllowed(); diff --git a/test/features/upload/domain/validator/validation_pipeline_test.dart b/test/features/upload/domain/validator/validation_pipeline_test.dart index 506798826..18ad9c40d 100644 --- a/test/features/upload/domain/validator/validation_pipeline_test.dart +++ b/test/features/upload/domain/validator/validation_pipeline_test.dart @@ -6,14 +6,14 @@ import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_size_snapshot.dart'; import 'package:tmail_ui_user/features/upload/domain/validator/validation_decision.dart'; -const _request = AttachmentUploadRequest( - sizes: AttachmentUploadSizeSnapshot( +final _request = AttachmentUploadRequest( + sizes: const AttachmentUploadSizeSnapshot( currentAllAttachmentBytes: 0, proposedAllAttachmentBytes: 0, currentRegularAttachmentBytes: 0, proposedRegularAttachmentBytes: 0, ), - limits: AttachmentUploadLimits(warningLimitBytes: 1000000000), + limits: const AttachmentUploadLimits(warningLimitBytes: 1000000000), ); class _StubAttachmentUploadRule implements AttachmentUploadRule { diff --git a/test/features/upload/presentation/validator/attachment_upload_gate_test.dart b/test/features/upload/presentation/validator/attachment_upload_gate_test.dart index a4e26d17e..7accc3009 100644 --- a/test/features/upload/presentation/validator/attachment_upload_gate_test.dart +++ b/test/features/upload/presentation/validator/attachment_upload_gate_test.dart @@ -5,11 +5,14 @@ import 'package:filesize/filesize.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:get/get.dart'; +import 'package:model/upload/file_info.dart'; import 'package:tmail_ui_user/features/upload/domain/validator/attachment_size_limit_rule.dart'; +import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_failure.dart'; import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_limits.dart'; import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_request.dart'; import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_size_snapshot.dart'; import 'package:tmail_ui_user/features/upload/presentation/validator/attachment_upload_gate.dart'; +import 'package:tmail_ui_user/features/upload/presentation/validator/attachment_upload_recovery.dart'; import 'package:tmail_ui_user/features/upload/presentation/validator/attachment_validation_feedback_impl.dart'; import 'package:tmail_ui_user/main/localizations/app_localizations.dart'; @@ -19,6 +22,7 @@ AttachmentUploadRequest _request({ int proposedAllAttachmentBytes = 0, int proposedRegularAttachmentBytes = 0, int? hardLimitBytes, + List files = const [], }) { return AttachmentUploadRequest( sizes: AttachmentUploadSizeSnapshot( @@ -28,9 +32,26 @@ AttachmentUploadRequest _request({ proposedRegularAttachmentBytes: proposedRegularAttachmentBytes, ), limits: AttachmentUploadLimits(warningLimitBytes: 1000000, hardLimitBytes: hardLimitBytes), + files: files, ); } +const _oversizeFile = FilePlaceholderInfo(fileName: 'big.zip', fileSize: 200); +const _oversizeInlineFile = FilePlaceholderInfo(fileName: 'shot.png', fileSize: 200, isInline: true); + +class _StubRecovery implements AttachmentUploadRecovery { + final Future Function(AttachmentUploadFailure failure, AttachmentUploadRequest request) onRecover; + bool called = false; + + _StubRecovery(this.onRecover); + + @override + Future recover(AttachmentUploadFailure failure, AttachmentUploadRequest request) { + called = true; + return onRecover(failure, request); + } +} + void main() { setUp(() { Get.testMode = true; @@ -118,4 +139,120 @@ void main() { expect(await future, isFalse); }); }); + + group('AttachmentUploadGate.permits with recovery', () { + testWidgets('no builder shows the failure dialog', (tester) async { + await tester.pumpWidget(WidgetFixtures.makeTestableWidget(child: const SizedBox.shrink())); + await tester.pump(); + final context = tester.element(find.byType(SizedBox)); + + final future = gate.permits( + request: _request( + proposedAllAttachmentBytes: 200, hardLimitBytes: 100, files: [_oversizeFile]), + feedbackFactory: () => AttachmentValidationFeedbackImpl(context), + ); + await tester.pump(); + + expect(find.byKey(const Key('confirm_dialog_action')), findsOneWidget); + await tester.tap(find.text(AppLocalizations.of(context).got_it)); + await tester.pumpAndSettle(); + expect(await future, isFalse); + }); + + testWidgets('recovery returning true skips the dialog and returns false', (tester) async { + await tester.pumpWidget(WidgetFixtures.makeTestableWidget(child: const SizedBox.shrink())); + await tester.pump(); + final context = tester.element(find.byType(SizedBox)); + final recovery = _StubRecovery((_, __) async => true); + + final allowed = await gate.permits( + request: _request( + proposedAllAttachmentBytes: 200, hardLimitBytes: 100, files: [_oversizeFile]), + feedbackFactory: () => AttachmentValidationFeedbackImpl(context), + recoveryFactory: () => recovery, + ); + + expect(recovery.called, isTrue); + expect(allowed, isFalse); + expect(find.byKey(const Key('confirm_dialog_action')), findsNothing); + }); + + testWidgets('recovery returning false falls back to the dialog', (tester) async { + await tester.pumpWidget(WidgetFixtures.makeTestableWidget(child: const SizedBox.shrink())); + await tester.pump(); + final context = tester.element(find.byType(SizedBox)); + final recovery = _StubRecovery((_, __) async => false); + + final future = gate.permits( + request: _request( + proposedAllAttachmentBytes: 200, hardLimitBytes: 100, files: [_oversizeFile]), + feedbackFactory: () => AttachmentValidationFeedbackImpl(context), + recoveryFactory: () => recovery, + ); + await tester.pump(); + + expect(find.byKey(const Key('confirm_dialog_action')), findsOneWidget); + await tester.tap(find.text(AppLocalizations.of(context).got_it)); + await tester.pumpAndSettle(); + expect(await future, isFalse); + }); + + testWidgets('recovery throwing falls back to the dialog', (tester) async { + await tester.pumpWidget(WidgetFixtures.makeTestableWidget(child: const SizedBox.shrink())); + await tester.pump(); + final context = tester.element(find.byType(SizedBox)); + final recovery = _StubRecovery((_, __) async => throw Exception('declined')); + + final future = gate.permits( + request: _request( + proposedAllAttachmentBytes: 200, hardLimitBytes: 100, files: [_oversizeFile]), + feedbackFactory: () => AttachmentValidationFeedbackImpl(context), + recoveryFactory: () => recovery, + ); + await tester.pump(); + + expect(find.byKey(const Key('confirm_dialog_action')), findsOneWidget); + await tester.tap(find.text(AppLocalizations.of(context).got_it)); + await tester.pumpAndSettle(); + expect(await future, isFalse); + }); + + testWidgets('inline-only files reach the recovery', (tester) async { + await tester.pumpWidget(WidgetFixtures.makeTestableWidget(child: const SizedBox.shrink())); + await tester.pump(); + final context = tester.element(find.byType(SizedBox)); + final recovery = _StubRecovery((_, __) async => true); + + final allowed = await gate.permits( + request: _request( + proposedAllAttachmentBytes: 200, hardLimitBytes: 100, files: [_oversizeInlineFile]), + feedbackFactory: () => AttachmentValidationFeedbackImpl(context), + recoveryFactory: () => recovery, + ); + + expect(recovery.called, isTrue); + expect(allowed, isFalse); + expect(find.byKey(const Key('confirm_dialog_action')), findsNothing); + }); + + testWidgets('rejection without files skips the recovery', (tester) async { + await tester.pumpWidget(WidgetFixtures.makeTestableWidget(child: const SizedBox.shrink())); + await tester.pump(); + final context = tester.element(find.byType(SizedBox)); + final recovery = _StubRecovery((_, __) async => true); + + final future = gate.permits( + request: _request(proposedAllAttachmentBytes: 200, hardLimitBytes: 100), + feedbackFactory: () => AttachmentValidationFeedbackImpl(context), + recoveryFactory: () => recovery, + ); + await tester.pump(); + + expect(recovery.called, isFalse); + expect(find.byKey(const Key('confirm_dialog_action')), findsOneWidget); + await tester.tap(find.text(AppLocalizations.of(context).got_it)); + await tester.pumpAndSettle(); + expect(await future, isFalse); + }); + }); }