Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions apps/client/lib/core/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import '../features/accounts/account_mailbox_page.dart';
import '../features/accounts/accounts_page.dart';
import '../features/message/compose_page.dart';
import '../features/message/message_page.dart';
import '../features/message/reply_draft.dart';
import '../features/settings/settings_page.dart';

final appRouterProvider = Provider<GoRouter>((ref) {
Expand Down Expand Up @@ -49,19 +50,15 @@ final appRouterProvider = Provider<GoRouter>((ref) {
state: state,
child: ComposePage(
initialAccountId: state.pathParameters['accountId'],
initialTo: state.uri.queryParameters['to'],
initialSubject: state.uri.queryParameters['subject'],
reply: state.extra is ReplyDraft ? state.extra as ReplyDraft : null,
),
),
),
GoRoute(
path: '/compose',
pageBuilder: (context, state) => _verticalPage(
state: state,
child: ComposePage(
initialTo: state.uri.queryParameters['to'],
initialSubject: state.uri.queryParameters['subject'],
),
child: const ComposePage(),
),
),
GoRoute(
Expand Down
10 changes: 10 additions & 0 deletions apps/client/lib/core/i18n/language_controller.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/intl.dart';
import 'package:shared_preferences/shared_preferences.dart';

enum AppLanguage {
Expand Down Expand Up @@ -74,6 +75,15 @@ class AppStrings {
String get send => _ko ? '보내기' : 'Send';
String get message => _ko ? '메일' : 'Message';
String get reply => _ko ? '답장' : 'Reply';
String get replyAll => _ko ? '전체 답장' : 'Reply all';
String get cc => _ko ? '참조' : 'Cc';

/// Absolute local date-time for the reply quote header.
String quoteDate(DateTime time) => _ko
? DateFormat('yyyy-MM-dd (E) HH:mm', 'ko').format(time)
: DateFormat("E, MMM d, y 'at' HH:mm", 'en').format(time);
String quoteHeader(String date, String sender) =>
_ko ? '$date $sender 님이 작성:' : 'On $date, $sender wrote:';
String get back => _ko ? '뒤로' : 'Back';
String get from => _ko ? '보낸 사람' : 'From';
String get to => _ko ? '받는 사람' : 'To';
Expand Down
43 changes: 31 additions & 12 deletions apps/client/lib/features/message/compose_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:go_router/go_router.dart';
import '../../core/i18n/language_controller.dart';
import '../../services/mail_api_client.dart';
import '../../widgets/app_shell.dart';
import 'reply_draft.dart';
import '../accounts/accounts_page.dart' show friendlyError;

final composeAccountsProvider = FutureProvider<List<MailAccountSummary>>((ref) {
Expand All @@ -15,22 +16,20 @@ class ComposePage extends ConsumerStatefulWidget {
const ComposePage({
super.key,
this.initialAccountId,
this.initialTo,
this.initialSubject,
this.reply,
});

final String? initialAccountId;
final String? initialTo;
final String? initialSubject;
final ReplyDraft? reply;

@override
ConsumerState<ComposePage> createState() => _ComposePageState();
}

class _ComposePageState extends ConsumerState<ComposePage> {
late final _toController = TextEditingController(text: widget.initialTo);
late final _subjectController =
TextEditingController(text: widget.initialSubject);
final _toController = TextEditingController();
final _ccController = TextEditingController();
final _subjectController = TextEditingController();
final _bodyController = TextEditingController();
String? _accountId;
bool _sending = false;
Expand All @@ -39,11 +38,19 @@ class _ComposePageState extends ConsumerState<ComposePage> {
void initState() {
super.initState();
_accountId = widget.initialAccountId;
final reply = widget.reply;
if (reply != null) {
_toController.text = reply.to.join(', ');
_ccController.text = reply.cc.join(', ');
_subjectController.text = reply.subject;
_bodyController.text = reply.body;
}
}

@override
void dispose() {
_toController.dispose();
_ccController.dispose();
_subjectController.dispose();
_bodyController.dispose();
super.dispose();
Expand Down Expand Up @@ -109,6 +116,13 @@ class _ComposePageState extends ConsumerState<ComposePage> {
controller: _toController,
decoration: InputDecoration(labelText: strings.to),
),
if (_ccController.text.isNotEmpty) ...[
const SizedBox(height: 12),
TextField(
controller: _ccController,
decoration: InputDecoration(labelText: strings.cc),
),
],
const SizedBox(height: 12),
TextField(
controller: _subjectController,
Expand Down Expand Up @@ -150,11 +164,7 @@ class _ComposePageState extends ConsumerState<ComposePage> {
void report(String message) =>
messenger.showSnackBar(SnackBar(content: Text(message)));

final to = _toController.text
.split(',')
.map((item) => item.trim())
.where((item) => item.isNotEmpty)
.toList();
final to = _addresses(_toController.text);
if (to.isEmpty) {
report(strings.recipientRequired);
return;
Expand All @@ -165,8 +175,11 @@ class _ComposePageState extends ConsumerState<ComposePage> {
await ref.read(mailApiClientProvider).sendMail(
accountId: accountId,
to: to,
cc: _addresses(_ccController.text),
subject: _subjectController.text,
body: _bodyController.text,
inReplyTo: widget.reply?.inReplyTo,
references: widget.reply?.references ?? const [],
);
} catch (error) {
// The draft stays on screen so the user can fix it and retry.
Expand All @@ -177,4 +190,10 @@ class _ComposePageState extends ConsumerState<ComposePage> {

if (mounted) context.go('/accounts/$accountId/mail');
}

List<String> _addresses(String value) => value
.split(',')
.map((item) => item.trim())
.where((item) => item.isNotEmpty)
.toList();
}
48 changes: 25 additions & 23 deletions apps/client/lib/features/message/message_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import '../../core/i18n/language_controller.dart';
import '../../services/mail_api_client.dart';
import '../../widgets/app_shell.dart';
import 'linked_message_body.dart';
import 'reply_draft.dart';

typedef MessageRequest = ({String messageId, String accountId});

Expand All @@ -18,19 +19,6 @@ final messageProvider =
);
});

/// Opens the composer addressed to the sender with a "Re:" subject.
String replyLocation({String? accountId, required MessageDetail message}) {
final subject = message.subject;
return Uri(
path: accountId == null ? '/compose' : '/accounts/$accountId/compose',
queryParameters: {
'to': message.sender,
'subject':
subject.toLowerCase().startsWith('re:') ? subject : 'Re: $subject',
},
).toString();
}

class MessagePage extends ConsumerWidget {
const MessagePage({
super.key,
Expand Down Expand Up @@ -63,16 +51,18 @@ class MessagePage extends ConsumerWidget {
),
title: Text(strings.message),
actions: [
IconButton(
tooltip: strings.reply,
onPressed: switch (message.asData?.value) {
final item? => () => context.go(
replyLocation(accountId: accountId, message: item),
),
null => null,
},
icon: const Icon(Icons.reply),
),
if (message.value case final item?) ...[
IconButton(
tooltip: strings.reply,
onPressed: () => _reply(context, item, strings, all: false),
icon: const Icon(Icons.reply),
),
IconButton(
tooltip: strings.replyAll,
onPressed: () => _reply(context, item, strings, all: true),
icon: const Icon(Icons.reply_all),
),
],
],
),
Expanded(
Expand Down Expand Up @@ -105,4 +95,16 @@ class MessagePage extends ConsumerWidget {
child: content,
);
}

void _reply(
BuildContext context,
MessageDetail item,
AppStrings strings, {
required bool all,
}) {
context.go(
'/accounts/${item.accountId ?? accountId}/compose',
extra: ReplyDraft.from(item, strings, all: all),
);
}
}
47 changes: 47 additions & 0 deletions apps/client/lib/features/message/reply_draft.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import '../../core/i18n/language_controller.dart';
import '../../services/mail_api_client.dart';

/// Prefilled compose state for a reply, passed through the router `extra`.
class ReplyDraft {
const ReplyDraft({
required this.to,
required this.cc,
required this.subject,
required this.body,
this.inReplyTo,
this.references = const [],
});

final List<String> to;
final List<String> cc;
final String subject;
final String body;
final String? inReplyTo;
final List<String> references;

factory ReplyDraft.from(
MessageDetail message,
AppStrings strings, {
required bool all,
}) {
final quoted = message.body.split('\n').map((line) => '> $line').join('\n');
final receivedAt = message.receivedAt;
final quoteTime =
receivedAt == null ? message.receivedAtLabel : strings.quoteDate(receivedAt);
return ReplyDraft(
to: all ? message.replyAllTo : message.replyTo,
cc: all ? message.replyAllCc : const [],
subject: replySubject(message.subject),
body: '\n\n${strings.quoteHeader(quoteTime, message.sender)}'
'\n$quoted',
inReplyTo: message.messageId,
references: message.references,
);
}
}

/// Adds one `Re:` prefix; `RE:`/`re:` already present is kept as is.
String replySubject(String subject) =>
RegExp(r'^\s*re:', caseSensitive: false).hasMatch(subject)
? subject
: 'Re: $subject';
2 changes: 2 additions & 0 deletions apps/client/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/date_symbol_data_local.dart';

import 'core/app_router.dart';
import 'core/app_theme.dart';
Expand All @@ -9,6 +10,7 @@ import 'services/push_service.dart';

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await initializeDateFormatting();

final pushService = PushService();
await pushService.initialize();
Expand Down
31 changes: 31 additions & 0 deletions apps/client/lib/services/mail_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,18 @@ class MailApiClient {
required List<String> to,
required String subject,
required String body,
List<String> cc = const [],
String? inReplyTo,
List<String> references = const [],
}) async {
await _dio.post('/mail/send', data: {
'accountId': accountId,
'to': to,
if (cc.isNotEmpty) 'cc': cc,
'subject': subject,
'text': body,
if (inReplyTo != null) 'inReplyTo': inReplyTo,
if (inReplyTo != null && references.isNotEmpty) 'references': references,
});
}
}
Expand Down Expand Up @@ -286,14 +292,28 @@ class MessageDetail {
required this.body,
required this.links,
required this.receivedAtLabel,
this.receivedAt,
this.accountId,
this.messageId,
this.references = const [],
this.replyTo = const [],
this.replyAllTo = const [],
this.replyAllCc = const [],
});

final String? accountId;
final String? messageId;
final List<String> references;
final List<String> replyTo;
final List<String> replyAllTo;
final List<String> replyAllCc;
final String id;
final String sender;
final String subject;
final String body;
final List<MessageLink> links;
final String receivedAtLabel;
final DateTime? receivedAt;

factory MessageDetail.fromJson(Map<String, dynamic> json) {
return MessageDetail(
Expand All @@ -306,10 +326,21 @@ class MessageDetail {
.map(MessageLink.fromJson)
.toList(growable: false),
receivedAtLabel: _formatReceivedAt(json['receivedAt'] as String?),
receivedAt:
DateTime.tryParse(json['receivedAt'] as String? ?? '')?.toLocal(),
accountId: json['accountId'] as String?,
messageId: json['messageId'] as String?,
references: _strings(json['references']),
replyTo: _strings(json['replyTo']),
replyAllTo: _strings(json['replyAllTo']),
replyAllCc: _strings(json['replyAllCc']),
);
}
}

List<String> _strings(Object? value) =>
(value as List<dynamic>? ?? const []).whereType<String>().toList();

class MessageLink {
const MessageLink({required this.url, required this.label});

Expand Down
3 changes: 3 additions & 0 deletions apps/client/test/compose_send_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ class _SendingApi extends MailApiClient {
Future<void> sendMail({
required String accountId,
required List<String> to,
List<String> cc = const [],
required String subject,
required String body,
String? inReplyTo,
List<String> references = const [],
}) {
sent.add(to);
return reply.future;
Expand Down
Loading
Loading