Skip to content

Commit

Permalink
TF-2168 Fix mailto invalid recipient
Browse files Browse the repository at this point in the history
  • Loading branch information
dab246 committed Sep 27, 2023
1 parent e53a91d commit c559dae
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 39 deletions.
16 changes: 9 additions & 7 deletions lib/features/composer/presentation/composer_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -440,14 +440,16 @@ class ComposerController extends BaseController {
toAddressExpandMode.value = ExpandMode.COLLAPSE;
_updateStatusEmailSendButton();
break;
case EmailActionType.composeFromMailtoURL:
if (GetUtils.isEmail(arguments.uri ?? '')) {
listToEmailAddress.add(EmailAddress(null, arguments.uri));
} else {
listToEmailAddress.add(EmailAddress(null, 'invalid'));
case EmailActionType.composeFromMailtoUri:
if (arguments.subject != null) {
setSubjectEmail(arguments.subject!);
subjectEmailInputController.text = arguments.subject!;
}
if (arguments.emailAddress != null) {
listToEmailAddress.add(arguments.emailAddress!);
isInitialRecipient.value = true;
toAddressExpandMode.value = ExpandMode.COLLAPSE;
}
isInitialRecipient.value = true;
toAddressExpandMode.value = ExpandMode.COLLAPSE;
_updateStatusEmailSendButton();
break;
case EmailActionType.reply:
Expand Down
13 changes: 7 additions & 6 deletions lib/features/email/presentation/model/composer_arguments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ComposerArguments extends RouterArguments {
final List<Attachment>? attachments;
final Role? mailboxRole;
final SendingEmail? sendingEmail;
final String? uri;
final String? subject;
final MessageIdsHeaderValue? messageId;
final MessageIdsHeaderValue? references;

Expand All @@ -29,7 +29,7 @@ class ComposerArguments extends RouterArguments {
this.emailAddress,
this.listSharedMediaFile,
this.sendingEmail,
this.uri,
this.subject,
this.messageId,
this.references,
});
Expand Down Expand Up @@ -58,10 +58,11 @@ class ComposerArguments extends RouterArguments {
emailAddress: emailAddress
);

factory ComposerArguments.fromMailtoURL(String? uri) =>
factory ComposerArguments.fromMailtoUri({EmailAddress? emailAddress, String? subject}) =>
ComposerArguments(
emailActionType: EmailActionType.composeFromMailtoURL,
uri: uri
emailActionType: EmailActionType.composeFromMailtoUri,
emailAddress: emailAddress,
subject: subject,
);

factory ComposerArguments.editDraftEmail(PresentationEmail presentationEmail) =>
Expand Down Expand Up @@ -140,7 +141,7 @@ class ComposerArguments extends RouterArguments {
emailAddress,
listSharedMediaFile,
sendingEmail,
uri,
subject,
messageId,
references,
];
Expand Down
5 changes: 4 additions & 1 deletion lib/features/mailbox/presentation/mailbox_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,10 @@ class MailboxController extends BaseMailboxController with MailboxActionHandlerM
if (isHasDataFromRoute) {
if (isRedirectToMailtoURL) {
mailboxDashBoardController.goToComposer(
ComposerArguments.fromMailtoURL(navigationRouter?.emailAddress)
ComposerArguments.fromMailtoUri(
emailAddress: navigationRouter?.emailAddress,
subject: navigationRouter?.subject
)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class MailtoUrlController extends ReloadableController {
if (parameters.containsKey('uri')) {
final mailtoArgument = MailtoArguments(
session: session,
emailAddress: parameters['uri']
mailtoUri: parameters['uri']
);
popAndPush(
RouteUtils.generateNavigationRoute(
Expand Down
15 changes: 5 additions & 10 deletions lib/features/mailto/presentation/model/mailto_arguments.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@

import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:tmail_ui_user/main/routes/app_routes.dart';
import 'package:tmail_ui_user/main/routes/route_utils.dart';
import 'package:tmail_ui_user/main/routes/router_arguments.dart';

class MailtoArguments extends RouterArguments {

final Session session;
final String? emailAddress;
final String? mailtoUri;

MailtoArguments({required this.session, this.emailAddress});
MailtoArguments({required this.session, this.mailtoUri});

Map<String, String?> toMapRouter() {
return {
'routeName': AppRoutes.mailtoURL,
'emailAddress': emailAddress
};
}
Map<String, String?> toMapRouter() => RouteUtils.parseMapMailtoFromUri(mailtoUri);

@override
List<Object?> get props => [session, emailAddress];
List<Object?> get props => [session, mailtoUri];
}
6 changes: 5 additions & 1 deletion lib/main/routes/navigation_router.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import 'package:equatable/equatable.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:jmap_dart_client/jmap/mail/email/email_address.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:tmail_ui_user/features/thread/domain/model/search_query.dart';

Expand All @@ -15,7 +16,8 @@ class NavigationRouter with EquatableMixin {
final DashboardType dashboardType;
final SearchQuery? searchQuery;
final String? routeName;
final String? emailAddress;
final EmailAddress? emailAddress;
final String? subject;

NavigationRouter({
this.emailId,
Expand All @@ -24,6 +26,7 @@ class NavigationRouter with EquatableMixin {
this.dashboardType = DashboardType.normal,
this.routeName,
this.emailAddress,
this.subject,
});

factory NavigationRouter.initial() => NavigationRouter();
Expand All @@ -36,5 +39,6 @@ class NavigationRouter with EquatableMixin {
dashboardType,
routeName,
emailAddress,
subject,
];
}
71 changes: 59 additions & 12 deletions lib/main/routes/route_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,31 @@ import 'package:core/data/model/query/query_parameter.dart';
import 'package:core/data/network/config/service_path.dart';
import 'package:core/utils/app_logger.dart';
import 'package:core/utils/platform_info.dart';
import 'package:get/get.dart';
import 'package:jmap_dart_client/jmap/core/id.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:jmap_dart_client/jmap/mail/email/email_address.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:tmail_ui_user/features/login/data/extensions/service_path_extension.dart';
import 'package:tmail_ui_user/features/thread/domain/model/search_query.dart';
import 'package:tmail_ui_user/main/routes/app_routes.dart';
import 'package:tmail_ui_user/main/routes/navigation_router.dart';
import 'package:universal_html/html.dart' as html;

abstract class RouteUtils {

static const String paramID = 'id';
static const String paramType = 'type';
static const String paramContext = 'context';
static const String paramQuery = 'q';
static const String paramRouteName = 'routeName';
static const String paramMailtoAddress = 'mailtoAddress';
static const String paramSubject = 'subject';

static const String mailtoPrefix = 'mailto:';

static const String INVALID_VALUE = 'invalid';

static String get baseOriginUrl => Uri.base.origin;

static String get baseUrl => Uri.base.path;
Expand All @@ -24,11 +39,11 @@ abstract class RouteUtils {
servicePath = servicePath.withPathParameter(router.emailId!.id.value);
}
servicePath = servicePath.withQueryParameters([
StringQueryParameter('type', router.dashboardType.name),
StringQueryParameter(paramType, router.dashboardType.name),
if (router.mailboxId != null)
StringQueryParameter('context', router.mailboxId!.id.value),
StringQueryParameter(paramContext, router.mailboxId!.id.value),
if (router.searchQuery != null)
StringQueryParameter('q', router.searchQuery!.value),
StringQueryParameter(paramQuery, router.searchQuery!.value),
]);
return servicePath.path;
} else {
Expand All @@ -43,30 +58,34 @@ abstract class RouteUtils {
servicePath = servicePath.withPathParameter(router.emailId!.id.value);
}
servicePath = servicePath.withQueryParameters([
StringQueryParameter('type', router.dashboardType.name),
StringQueryParameter(paramType, router.dashboardType.name),
if (router.mailboxId != null)
StringQueryParameter('context', router.mailboxId!.id.value),
StringQueryParameter(paramContext, router.mailboxId!.id.value),
if (router.searchQuery != null)
StringQueryParameter('q', router.searchQuery!.value),
StringQueryParameter(paramQuery, router.searchQuery!.value),
]);

return Uri.parse(servicePath.path);
}

static NavigationRouter parsingRouteParametersToNavigationRouter(Map<String, String?> parameters) {
final idParam = parameters['id'];
final typeParam = parameters['type'];
final contextPram = parameters['context'];
final queryParam = parameters['q'];
final routeName = parameters['routeName'];
final emailAddress = parameters['emailAddress'];
final idParam = parameters[paramID];
final typeParam = parameters[paramType];
final contextPram = parameters[paramContext];
final queryParam = parameters[paramQuery];
final routeName = parameters[paramRouteName];
final mailtoAddress = parameters[paramMailtoAddress];
final subject = parameters[paramSubject];

final emailId = idParam != null ? EmailId(Id(idParam)) : null;
final mailboxId = contextPram != null ? MailboxId(Id(contextPram)) : null;
final searchQuery = queryParam != null ? SearchQuery(queryParam) : null;
final dashboardType = typeParam == DashboardType.search.name
? DashboardType.search
: DashboardType.normal;
final emailAddress = mailtoAddress != null && GetUtils.isEmail(mailtoAddress)
? EmailAddress(null, mailtoAddress)
: EmailAddress(null, INVALID_VALUE);

return NavigationRouter(
emailId: emailId,
Expand All @@ -75,6 +94,7 @@ abstract class RouteUtils {
dashboardType: dashboardType,
routeName: routeName,
emailAddress: emailAddress,
subject: subject,
);
}

Expand All @@ -83,4 +103,31 @@ abstract class RouteUtils {
log('RouteUtils::updateRouteOnBrowser(): newRoute: $newRoute');
html.window.history.replaceState(null, title, newRoute.toString());
}

static Map<String, String?> parseMapMailtoFromUri(String? mailtoUri) {
log('RouteUtils::parseMapMailtoFromUri:mailtoUri: $mailtoUri');
final mapMailto = <String, String?>{
RouteUtils.paramRouteName: AppRoutes.mailtoURL,
};
if (mailtoUri?.startsWith(mailtoPrefix) == true) {
final mailtoUrlDecoded = Uri.decodeFull(mailtoUri!);
final uri = Uri.tryParse(mailtoUrlDecoded);
if (uri == null) return mapMailto;

final mailtoAddress = uri.path;
final mapQueryParam = uri.queryParameters;

mapMailto[paramMailtoAddress] = mailtoAddress;
if (mapQueryParam.containsKey(paramSubject)) {
mapMailto[paramSubject] = mapQueryParam[paramSubject];
}
} else if (mailtoUri != null) {
final mailtoUrlDecoded = Uri.decodeFull(mailtoUri);
mapMailto[paramMailtoAddress] = mailtoUrlDecoded;
} else {
mapMailto[paramMailtoAddress] = mailtoUri;
}
log('RouteUtils::parseMapMailtoFromUri:mapMailto: $mapMailto');
return mapMailto;
}
}
2 changes: 1 addition & 1 deletion model/lib/email/email_action_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ enum EmailActionType {
composeFromContentShared,
composeFromFileShared,
composeFromEmailAddress,
composeFromMailtoURL,
composeFromMailtoUri,
reopenComposerBrowser,
moveToTrash,
deletePermanently,
Expand Down

0 comments on commit c559dae

Please sign in to comment.