Skip to content

Commit

Permalink
TF-3221 Handle mailto with additional cc and bcc
Browse files Browse the repository at this point in the history
  • Loading branch information
tddang-linagora authored and hoangdat committed Nov 15, 2024
1 parent 51a4a1b commit 45581e4
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 60 deletions.
4 changes: 2 additions & 2 deletions docs/adr/0036-mailto-uri-chemes-to-interact-twake-mail.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ Summary of URI schemes that can interact with Twake Mail:

- `/[email protected]`
- `/mailto/?uri=mailto:[email protected]&subject=TwakeMail&body=HelloWorld`
- `/mailto/?uri=mailto:[email protected],[email protected],[email protected]&subject=TwakeMail&body=HelloWorld`
- `/mailto/?uri=mailto:[email protected],[email protected],[email protected]&[email protected],[email protected],[email protected]&[email protected],[email protected],[email protected]&[email protected],[email protected],[email protected]&subject=TwakeMail&body=HelloWorld`

2. URI scheme encoded

- `%2Fmailto%3Furi%3Duser%40example.com`
- `%2Fmailto%2F%3Furi%3Dmailto%3Auser%40example.com%26subject%3DTwakeMail%26body%3DHelloWorld`
- `%2Fmailto%2F%3Furi%3Dmailto%3Auser1%40example.com%2Cuser2%40example.com%2Cuser3%40example.com%26subject%3DTwakeMail%26body%3DHelloWorld`
- `%2Fmailto%2F%3Furi%3Dmailto%3Auser1%40example.com%2Cuser2%40example.com%2Cuser3%40example.com%26to=user1%40example.com%2Cuser2%40example.com%2Cuser3%40example.com%26cc=user1%40example.com%2Cuser2%40example.com%2Cuser3%40example.com%26bcc=user1%40example.com%2Cuser2%40example.com%2Cuser3%40example.com%26subject%3DTwakeMail%26body%3DHelloWorld`

## Consequences

Expand Down
17 changes: 16 additions & 1 deletion lib/features/composer/presentation/composer_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,16 @@ class ComposerController extends BaseController
isInitialRecipient.value = true;
toAddressExpandMode.value = ExpandMode.COLLAPSE;
}
if (arguments.cc?.isNotEmpty == true) {
listCcEmailAddress = arguments.cc!;
ccRecipientState.value = PrefixRecipientState.enabled;
ccAddressExpandMode.value = ExpandMode.COLLAPSE;
}
if (arguments.bcc?.isNotEmpty == true) {
bccRecipientState.value = PrefixRecipientState.enabled;
bccAddressExpandMode.value = ExpandMode.COLLAPSE;
listBccEmailAddress = arguments.bcc!;
}
_getEmailContentFromMailtoUri(arguments.body ?? '');
_updateStatusEmailSendButton();
break;
Expand Down Expand Up @@ -1729,10 +1739,15 @@ class ComposerController extends BaseController
if (bccRecipientState.value == PrefixRecipientState.disabled) {
bccRecipientState.value = PrefixRecipientState.enabled;
}
listBccEmailAddress = listEmailAddress.toList();
if (composerArguments.value?.emailActionType == EmailActionType.composeFromMailtoUri) {
listBccEmailAddress = {...listEmailAddress, ...?composerArguments.value?.bcc}.toList();
} else {
listBccEmailAddress = listEmailAddress.toList();
}
toAddressExpandMode.value = ExpandMode.COLLAPSE;
ccAddressExpandMode.value = ExpandMode.COLLAPSE;
bccAddressExpandMode.value = ExpandMode.COLLAPSE;
bccAddressExpandMode.refresh();
_updateStatusEmailSendButton();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1159,13 +1159,21 @@ class SingleEmailController extends BaseController with AppLoaderMixin {
}

Future<void> openMailToLink(Uri? uri) async {
if (uri == null) return;

final navigationRouter = RouteUtils.generateNavigationRouterFromMailtoLink(uri.toString());
log('SingleEmailController::openMailToLink(): ${uri.toString()}');
String address = uri?.path ?? '';
log('SingleEmailController::openMailToLink(): address: $address');
if (address.isNotEmpty) {
final emailAddress = EmailAddress(null, address);
mailboxDashBoardController.goToComposer(ComposerArguments.fromEmailAddress(emailAddress));
}
if (!RouteUtils.canOpenComposerFromNavigationRouter(navigationRouter)) return;

mailboxDashBoardController.goToComposer(
ComposerArguments.fromMailtoUri(
listEmailAddress: navigationRouter.listEmailAddress,
cc: navigationRouter.cc,
bcc: navigationRouter.bcc,
subject: navigationRouter.subject,
body: navigationRouter.body
)
);
}

void deleteEmailPermanently(BuildContext context, PresentationEmail email) {
Expand Down
33 changes: 25 additions & 8 deletions lib/features/email/presentation/model/composer_arguments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class ComposerArguments extends RouterArguments {
final List<Attachment>? inlineImages;
final bool? hasRequestReadReceipt;
final ScreenDisplayMode displayMode;
final List<EmailAddress>? cc;
final List<EmailAddress>? bcc;

ComposerArguments({
this.emailActionType = EmailActionType.compose,
Expand All @@ -49,7 +51,9 @@ class ComposerArguments extends RouterArguments {
this.selectedIdentityId,
this.inlineImages,
this.hasRequestReadReceipt,
this.displayMode = ScreenDisplayMode.normal
this.displayMode = ScreenDisplayMode.normal,
this.cc,
this.bcc,
});

factory ComposerArguments.fromSendingEmail(SendingEmail sendingEmail) =>
Expand All @@ -76,13 +80,20 @@ class ComposerArguments extends RouterArguments {
listEmailAddress: [emailAddress]
);

factory ComposerArguments.fromMailtoUri({List<EmailAddress>? listEmailAddress, String? subject, String? body}) =>
ComposerArguments(
emailActionType: EmailActionType.composeFromMailtoUri,
listEmailAddress: listEmailAddress,
subject: subject,
body: body,
);
factory ComposerArguments.fromMailtoUri({
List<EmailAddress>? listEmailAddress,
String? subject,
String? body,
List<EmailAddress>? cc,
List<EmailAddress>? bcc
}) => ComposerArguments(
emailActionType: EmailActionType.composeFromMailtoUri,
listEmailAddress: listEmailAddress,
subject: subject,
body: body,
cc: cc,
bcc: bcc,
);

factory ComposerArguments.editDraftEmail(PresentationEmail presentationEmail) =>
ComposerArguments(
Expand Down Expand Up @@ -193,6 +204,8 @@ class ComposerArguments extends RouterArguments {
inlineImages,
hasRequestReadReceipt,
displayMode,
cc,
bcc,
];

ComposerArguments copyWith({
Expand All @@ -214,6 +227,8 @@ class ComposerArguments extends RouterArguments {
List<Attachment>? inlineImages,
bool? hasRequestReadReceipt,
ScreenDisplayMode? displayMode,
List<EmailAddress>? cc,
List<EmailAddress>? bcc,
}) {
return ComposerArguments(
emailActionType: emailActionType ?? this.emailActionType,
Expand All @@ -234,6 +249,8 @@ class ComposerArguments extends RouterArguments {
inlineImages: inlineImages ?? this.inlineImages,
hasRequestReadReceipt: hasRequestReadReceipt ?? this.hasRequestReadReceipt,
displayMode: displayMode ?? this.displayMode,
cc: cc ?? this.cc,
bcc: bcc ?? this.bcc,
);
}
}
2 changes: 2 additions & 0 deletions lib/features/mailbox/presentation/mailbox_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ class MailboxController extends BaseMailboxController with MailboxActionHandlerM
mailboxDashBoardController.goToComposer(
ComposerArguments.fromMailtoUri(
listEmailAddress: _navigationRouter?.listEmailAddress,
cc: _navigationRouter?.cc,
bcc: _navigationRouter?.bcc,
subject: _navigationRouter?.subject,
body: _navigationRouter?.body
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MailtoUrlController extends ReloadableController {
if (parameters.containsKey('uri')) {
final mailtoArgument = MailtoArguments(
session: session,
mailtoUri: parameters['uri']
mailtoUri: Uri.base.toString(),
);
popAndPush(
RouteUtils.generateNavigationRoute(AppRoutes.dashboard),
Expand Down
6 changes: 6 additions & 0 deletions lib/main/routes/navigation_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class NavigationRouter with EquatableMixin {
final String? subject;
final String? body;
final AccountMenuItem accountMenuItem;
final List<EmailAddress>? cc;
final List<EmailAddress>? bcc;

NavigationRouter({
this.emailId,
Expand All @@ -32,6 +34,8 @@ class NavigationRouter with EquatableMixin {
this.subject,
this.body,
this.accountMenuItem = AccountMenuItem.none,
this.cc,
this.bcc,
});

factory NavigationRouter.initial() => NavigationRouter();
Expand All @@ -47,5 +51,7 @@ class NavigationRouter with EquatableMixin {
subject,
body,
accountMenuItem,
cc,
bcc,
];
}
128 changes: 86 additions & 42 deletions lib/main/routes/route_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ abstract class RouteUtils {
static const String paramMailtoAddress = 'mailtoAddress';
static const String paramSubject = 'subject';
static const String paramBody = 'body';
static const String paramTo = 'to';
static const String paramCc = 'cc';
static const String paramBcc = 'bcc';

static const String mailtoPrefix = 'mailto:';
static const String mailtoPrefix = 'mailto';
static const String uriPrefix = 'uri';
static const String ADDRESS_SEPARATOR = ',';
static const String INVALID_VALUE = 'invalid';

Expand Down Expand Up @@ -107,6 +111,8 @@ abstract class RouteUtils {
final queryParam = parameters[paramQuery];
final routeName = parameters[paramRouteName];
final mailtoAddress = parameters[paramMailtoAddress];
final mailtoCc = parameters[paramCc];
final mailtoBcc = parameters[paramBcc];
final subject = parameters[paramSubject];
final body = parameters[paramBody];

Expand All @@ -116,18 +122,11 @@ abstract class RouteUtils {
final dashboardType = DashboardType.values.firstWhereOrNull((type) => type.name == typeParam) ?? DashboardType.normal;
final settingType = AccountMenuItem.values.firstWhereOrNull((type) => type.getAliasBrowser() == typeParam) ?? AccountMenuItem.none;
List<EmailAddress>? listEmailAddress;
if (mailtoAddress is List<String>) {
listEmailAddress = mailtoAddress
.map((address) => EmailAddress(
null,
GetUtils.isEmail(address) ? address : INVALID_VALUE
))
.toList();
} else if (mailtoAddress is String) {
listEmailAddress = [
EmailAddress(null, GetUtils.isEmail(mailtoAddress) ? mailtoAddress : INVALID_VALUE)
];
}
List<EmailAddress>? cc;
List<EmailAddress>? bcc;
listEmailAddress = _emailAddressesFromMailtoAddress(mailtoAddress);
cc = _emailAddressesFromMailtoAddress(mailtoCc);
bcc = _emailAddressesFromMailtoAddress(mailtoBcc);
log('RouteUtils::parsingRouteParametersToNavigationRouter:listEmailAddress = $listEmailAddress');
return NavigationRouter(
emailId: emailId,
Expand All @@ -136,12 +135,27 @@ abstract class RouteUtils {
dashboardType: dashboardType,
routeName: routeName,
listEmailAddress: listEmailAddress,
cc: cc,
bcc: bcc,
subject: subject,
body: body,
accountMenuItem: settingType,
);
}

static List<EmailAddress>? _emailAddressesFromMailtoAddress(dynamic mailtoAddress) {
if (mailtoAddress is List<String>) {
return mailtoAddress
.map((address) => EmailAddress(null, address))
.toList();
} else if (mailtoAddress is String) {
return [
EmailAddress(null, mailtoAddress)
];
}
return null;
}

static void replaceBrowserHistory({required String title, required Uri url}) {
log('RouteUtils::replaceBrowserHistory(): title: $title | url: $url');
html.window.history.replaceState(null, title, url.toString());
Expand All @@ -152,37 +166,59 @@ abstract class RouteUtils {
final mapMailto = <String, dynamic>{
RouteUtils.paramRouteName: AppRoutes.mailtoURL,
};
if (mailtoUri?.startsWith(mailtoPrefix) == true) {
final mailtoUrlDecoded = Uri.decodeFull(mailtoUri!);
log('RouteUtils::parseMapMailtoFromUri:mailtoUrlDecoded = $mailtoUrlDecoded');
final uri = Uri.tryParse(mailtoUrlDecoded);
if (uri == null) return mapMailto;

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

if (mailtoAddress.contains(ADDRESS_SEPARATOR)) {
final listAddress = mailtoAddress.split(ADDRESS_SEPARATOR);
log('RouteUtils::parseMapMailtoFromUri:listAddress = $listAddress');
mapMailto[paramMailtoAddress] = listAddress;
} else {
log('RouteUtils::parseMapMailtoFromUri:mailtoAddress = $mailtoAddress');
mapMailto[paramMailtoAddress] = mailtoAddress;
}
if (mapQueryParam.containsKey(paramSubject)) {
mapMailto[paramSubject] = mapQueryParam[paramSubject];
}
if (mapQueryParam.containsKey(paramBody)) {
mapMailto[paramBody] = mapQueryParam[paramBody];
}
} else if (mailtoUri != null) {
final mailtoUrlDecoded = Uri.decodeFull(mailtoUri);
log('RouteUtils::parseMapMailtoFromUri:mailtoUrlDecoded = $mailtoUrlDecoded');
mapMailto[paramMailtoAddress] = mailtoUrlDecoded;
mailtoUri = mailtoUri == null ? null : Uri.decodeFull(mailtoUri);
final parsedMailToUri = Uri.tryParse(mailtoUri ?? '');

if (parsedMailToUri?.scheme == mailtoPrefix) {
final to = <String>{
...?parsedMailToUri?.path.split(ADDRESS_SEPARATOR),
...?parsedMailToUri?.queryParameters[paramTo]?.split(ADDRESS_SEPARATOR)
}.toList();
final cc = {
...?parsedMailToUri?.queryParameters[paramCc]?.split(ADDRESS_SEPARATOR),
}.toList();
final bcc = {
...?parsedMailToUri?.queryParameters[paramBcc]?.split(ADDRESS_SEPARATOR),
}.toList();
final subject = parsedMailToUri?.queryParameters[paramSubject];
final body = parsedMailToUri?.queryParameters[paramBody];
mapMailto[paramMailtoAddress] = to;
mapMailto[paramCc] = cc;
mapMailto[paramBcc] = bcc;
mapMailto[paramSubject] = subject;
mapMailto[paramBody] = body;
} else if (parsedMailToUri?.path == "/$mailtoPrefix" || parsedMailToUri?.path == "/$mailtoPrefix/") {
final to = {
...?parsedMailToUri?.queryParameters[uriPrefix]?.split('$mailtoPrefix:').last.split(ADDRESS_SEPARATOR),
...?parsedMailToUri?.queryParameters[paramTo]?.split(ADDRESS_SEPARATOR)
}.toList();
final cc = {
...?parsedMailToUri?.queryParameters[paramCc]?.split(ADDRESS_SEPARATOR),
}.toList();
final bcc = {
...?parsedMailToUri?.queryParameters[paramBcc]?.split(ADDRESS_SEPARATOR),
}.toList();
final subject = parsedMailToUri?.queryParameters[paramSubject];
final body = parsedMailToUri?.queryParameters[paramBody];
mapMailto[paramMailtoAddress] = to;
mapMailto[paramCc] = cc;
mapMailto[paramBcc] = bcc;
mapMailto[paramSubject] = subject;
mapMailto[paramBody] = body;
} else {
mapMailto[paramMailtoAddress] = mailtoUri;
mapMailto[paramMailtoAddress] = mailtoUri?.split(ADDRESS_SEPARATOR);
}
log('RouteUtils::parseMapMailtoFromUri:mapMailto: $mapMailto');

if (mapMailto[paramMailtoAddress]?.length == 1) {
mapMailto[paramMailtoAddress] = mapMailto[paramMailtoAddress].first;
}

log('RouteUtils::parseMapMailtoFromUri:paramMailtoAddress = ${mapMailto[paramMailtoAddress]}');
log('RouteUtils::parseMapMailtoFromUri:paramCc = ${mapMailto[paramCc]}');
log('RouteUtils::parseMapMailtoFromUri:paramBcc = ${mapMailto[paramBcc]}');
log('RouteUtils::parseMapMailtoFromUri:paramSubject = ${mapMailto[paramSubject]}');
log('RouteUtils::parseMapMailtoFromUri:paramBody = ${mapMailto[paramBody]}');

return mapMailto;
}

Expand All @@ -192,4 +228,12 @@ abstract class RouteUtils {
log('RouteUtils::generateNavigationRouterFromMailtoLink:navigationRouter: $navigationRouter');
return navigationRouter;
}

static bool canOpenComposerFromNavigationRouter(NavigationRouter navigationRouter) {
return navigationRouter.listEmailAddress?.isNotEmpty == true
|| navigationRouter.cc?.isNotEmpty == true
|| navigationRouter.bcc?.isNotEmpty == true
|| navigationRouter.subject?.isNotEmpty == true
|| navigationRouter.body?.isNotEmpty == true;
}
}
Loading

0 comments on commit 45581e4

Please sign in to comment.