Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5464efe684 | ||
|
|
499774d1a6 | ||
|
|
132b6aeb9a | ||
|
|
efd5a1fc17 |
@@ -27,6 +27,7 @@ abstract class EmailRepository {
|
|||||||
Future<EmailBody> getEmailBody(String emailId);
|
Future<EmailBody> getEmailBody(String emailId);
|
||||||
Future<SyncEmailsResult> syncEmails(String accountId, String mailboxPath);
|
Future<SyncEmailsResult> syncEmails(String accountId, String mailboxPath);
|
||||||
Future<void> setFlag(String emailId, {bool? seen, bool? flagged});
|
Future<void> setFlag(String emailId, {bool? seen, bool? flagged});
|
||||||
|
Future<void> markAllAsRead(String accountId, String mailboxPath);
|
||||||
Future<void> moveEmail(String emailId, String destMailboxPath);
|
Future<void> moveEmail(String emailId, String destMailboxPath);
|
||||||
|
|
||||||
/// Deletes the email. Returns the path of the mailbox it was moved to
|
/// Deletes the email. Returns the path of the mailbox it was moved to
|
||||||
|
|||||||
@@ -1520,6 +1520,63 @@ class EmailRepositoryImpl implements EmailRepository {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> markAllAsRead(String accountId, String mailboxPath) async {
|
||||||
|
final account = (await _accounts.getAccount(accountId))!;
|
||||||
|
final unread = await (_db.select(_db.emails)
|
||||||
|
..where(
|
||||||
|
(t) =>
|
||||||
|
t.accountId.equals(accountId) &
|
||||||
|
t.mailboxPath.equals(mailboxPath) &
|
||||||
|
t.isSeen.equals(false),
|
||||||
|
))
|
||||||
|
.get();
|
||||||
|
if (unread.isEmpty) return;
|
||||||
|
|
||||||
|
await _db.transaction(() async {
|
||||||
|
for (final row in unread) {
|
||||||
|
if (account.type == account_model.AccountType.jmap) {
|
||||||
|
await _enqueueChange(
|
||||||
|
accountId,
|
||||||
|
row.id,
|
||||||
|
'flag_seen',
|
||||||
|
jsonEncode({'seen': true}),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
await _enqueueChange(
|
||||||
|
accountId,
|
||||||
|
row.id,
|
||||||
|
'flag_seen',
|
||||||
|
jsonEncode({
|
||||||
|
'uid': row.uid,
|
||||||
|
'mailboxPath': row.mailboxPath,
|
||||||
|
'seen': true,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bulk mark all unread emails in this mailbox as seen.
|
||||||
|
await (_db.update(_db.emails)
|
||||||
|
..where(
|
||||||
|
(t) =>
|
||||||
|
t.accountId.equals(accountId) &
|
||||||
|
t.mailboxPath.equals(mailboxPath) &
|
||||||
|
t.isSeen.equals(false),
|
||||||
|
))
|
||||||
|
.write(const EmailsCompanion(isSeen: Value(true)));
|
||||||
|
|
||||||
|
// Update all threads in this mailbox to reflect no unread.
|
||||||
|
await (_db.update(_db.threads)
|
||||||
|
..where(
|
||||||
|
(t) =>
|
||||||
|
t.accountId.equals(accountId) &
|
||||||
|
t.mailboxPath.equals(mailboxPath),
|
||||||
|
))
|
||||||
|
.write(const ThreadsCompanion(hasUnread: Value(false)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> moveEmail(String emailId, String destMailboxPath) async {
|
Future<void> moveEmail(String emailId, String destMailboxPath) async {
|
||||||
final row = await (_db.select(
|
final row = await (_db.select(
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_html/flutter_html.dart';
|
import 'package:flutter_html/flutter_html.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
@@ -60,20 +61,27 @@ class _EmailDetailScreenState extends ConsumerState<EmailDetailScreen> {
|
|||||||
tooltip: 'Reply',
|
tooltip: 'Reply',
|
||||||
onPressed: header == null
|
onPressed: header == null
|
||||||
? null
|
? null
|
||||||
: () => _reply(context, header, body, replyAll: false),
|
: () {
|
||||||
|
unawaited(_reply(context, header, body, replyAll: false));
|
||||||
|
},
|
||||||
),
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(Icons.reply_all),
|
icon: const Icon(Icons.reply_all),
|
||||||
tooltip: 'Reply all',
|
tooltip: 'Reply all',
|
||||||
onPressed: header == null
|
onPressed: header == null
|
||||||
? null
|
? null
|
||||||
: () => _reply(context, header, body, replyAll: true),
|
: () {
|
||||||
|
unawaited(_reply(context, header, body, replyAll: true));
|
||||||
|
},
|
||||||
),
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(Icons.forward),
|
icon: const Icon(Icons.forward),
|
||||||
tooltip: 'Forward',
|
tooltip: 'Forward',
|
||||||
onPressed:
|
onPressed: header == null
|
||||||
header == null ? null : () => _forward(context, header, body),
|
? null
|
||||||
|
: () {
|
||||||
|
unawaited(_forward(context, header, body));
|
||||||
|
},
|
||||||
),
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(Icons.mark_email_unread_outlined),
|
icon: const Icon(Icons.mark_email_unread_outlined),
|
||||||
@@ -263,26 +271,31 @@ class _EmailDetailScreenState extends ConsumerState<EmailDetailScreen> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
String _quotedBody(Email header, EmailBody? body) {
|
Future<String> _quotedBody(Email header, EmailBody? body) async {
|
||||||
final date = header.sentAt != null ? _dateFmt.format(header.sentAt!) : '';
|
final date = header.sentAt != null ? _dateFmt.format(header.sentAt!) : '';
|
||||||
final from =
|
final from =
|
||||||
header.from.isNotEmpty ? header.from.first.toString() : '(unknown)';
|
header.from.isNotEmpty ? header.from.first.toString() : '(unknown)';
|
||||||
final text = body?.textBody ?? htmlToPlain(body?.htmlBody ?? '');
|
final rawText = body?.textBody;
|
||||||
|
final text = (rawText != null && rawText.isNotEmpty)
|
||||||
|
? rawText
|
||||||
|
: await compute(htmlToPlain, body?.htmlBody ?? '');
|
||||||
final quoted = text.trim().split('\n').map((l) => '> $l').join('\n');
|
final quoted = text.trim().split('\n').map((l) => '> $l').join('\n');
|
||||||
return '\n\n— On $date, $from wrote:\n$quoted';
|
return '\n\n— On $date, $from wrote:\n$quoted';
|
||||||
}
|
}
|
||||||
|
|
||||||
void _reply(
|
Future<void> _reply(
|
||||||
BuildContext context,
|
BuildContext context,
|
||||||
Email header,
|
Email header,
|
||||||
EmailBody? body, {
|
EmailBody? body, {
|
||||||
required bool replyAll,
|
required bool replyAll,
|
||||||
}) {
|
}) async {
|
||||||
final to = header.from.isNotEmpty ? header.from.first.email : '';
|
final to = header.from.isNotEmpty ? header.from.first.email : '';
|
||||||
final subject = (header.subject?.startsWith('Re:') ?? false)
|
final subject = (header.subject?.startsWith('Re:') ?? false)
|
||||||
? header.subject!
|
? header.subject!
|
||||||
: 'Re: ${header.subject ?? ''}';
|
: 'Re: ${header.subject ?? ''}';
|
||||||
final cc = replyAll ? header.to.map((a) => a.email).join(', ') : '';
|
final cc = replyAll ? header.to.map((a) => a.email).join(', ') : '';
|
||||||
|
final quoted = await _quotedBody(header, body);
|
||||||
|
if (!context.mounted) return;
|
||||||
unawaited(
|
unawaited(
|
||||||
context.push(
|
context.push(
|
||||||
'/compose',
|
'/compose',
|
||||||
@@ -290,23 +303,29 @@ class _EmailDetailScreenState extends ConsumerState<EmailDetailScreen> {
|
|||||||
'replyToEmailId': widget.emailId,
|
'replyToEmailId': widget.emailId,
|
||||||
'prefillTo': to,
|
'prefillTo': to,
|
||||||
'prefillSubject': subject,
|
'prefillSubject': subject,
|
||||||
'prefillBody': _quotedBody(header, body),
|
'prefillBody': quoted,
|
||||||
if (cc.isNotEmpty) 'prefillCc': cc,
|
if (cc.isNotEmpty) 'prefillCc': cc,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _forward(BuildContext context, Email header, EmailBody? body) {
|
Future<void> _forward(
|
||||||
|
BuildContext context,
|
||||||
|
Email header,
|
||||||
|
EmailBody? body,
|
||||||
|
) async {
|
||||||
final subject = (header.subject?.startsWith('Fwd:') ?? false)
|
final subject = (header.subject?.startsWith('Fwd:') ?? false)
|
||||||
? header.subject!
|
? header.subject!
|
||||||
: 'Fwd: ${header.subject ?? ''}';
|
: 'Fwd: ${header.subject ?? ''}';
|
||||||
|
final quoted = await _quotedBody(header, body);
|
||||||
|
if (!context.mounted) return;
|
||||||
unawaited(
|
unawaited(
|
||||||
context.push(
|
context.push(
|
||||||
'/compose',
|
'/compose',
|
||||||
extra: {
|
extra: {
|
||||||
'prefillSubject': subject,
|
'prefillSubject': subject,
|
||||||
'prefillBody': _quotedBody(header, body),
|
'prefillBody': quoted,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -193,6 +193,22 @@ class _EmailListScreenState extends ConsumerState<EmailListScreen> {
|
|||||||
extra: {'accountId': widget.accountId},
|
extra: {'accountId': widget.accountId},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
PopupMenuButton<String>(
|
||||||
|
onSelected: (value) async {
|
||||||
|
if (value == 'mark_all_read') {
|
||||||
|
await emailRepo.markAllAsRead(
|
||||||
|
widget.accountId,
|
||||||
|
widget.mailboxPath,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
itemBuilder: (_) => const [
|
||||||
|
PopupMenuItem(
|
||||||
|
value: 'mark_all_read',
|
||||||
|
child: Text('Mark all as read'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
],
|
],
|
||||||
bottom: PreferredSize(
|
bottom: PreferredSize(
|
||||||
preferredSize: const Size.fromHeight(60),
|
preferredSize: const Size.fromHeight(60),
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:enough_mail/enough_mail.dart' as imap;
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:sharedinbox/core/models/account.dart';
|
import 'package:sharedinbox/core/models/account.dart';
|
||||||
import 'package:sharedinbox/core/models/email.dart';
|
import 'package:sharedinbox/core/models/email.dart';
|
||||||
@@ -10,8 +12,16 @@ import 'package:sharedinbox/core/repositories/mailbox_repository.dart';
|
|||||||
import 'package:sharedinbox/core/repositories/sync_log_repository.dart';
|
import 'package:sharedinbox/core/repositories/sync_log_repository.dart';
|
||||||
import 'package:sharedinbox/core/sync/account_sync_manager.dart';
|
import 'package:sharedinbox/core/sync/account_sync_manager.dart';
|
||||||
|
|
||||||
|
Future<imap.ImapClient> _fakeImapConnect(
|
||||||
|
Account account,
|
||||||
|
String username,
|
||||||
|
String password,
|
||||||
|
) async =>
|
||||||
|
throw const SocketException('fake — no real IMAP server in tests');
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
test('AccountSyncManager schedules sync for multiple accounts', () async {
|
test('AccountSyncManager schedules IMAP sync for multiple accounts',
|
||||||
|
() async {
|
||||||
final accounts = _FakeAccounts('pw');
|
final accounts = _FakeAccounts('pw');
|
||||||
final mailboxes = _FakeMailboxes();
|
final mailboxes = _FakeMailboxes();
|
||||||
final emails = _FakeEmails();
|
final emails = _FakeEmails();
|
||||||
@@ -22,6 +32,7 @@ void main() {
|
|||||||
mailboxes,
|
mailboxes,
|
||||||
emails,
|
emails,
|
||||||
syncLog: logs,
|
syncLog: logs,
|
||||||
|
imapConnect: _fakeImapConnect,
|
||||||
);
|
);
|
||||||
|
|
||||||
final a1 = _account('1');
|
final a1 = _account('1');
|
||||||
@@ -38,6 +49,34 @@ void main() {
|
|||||||
|
|
||||||
manager.dispose();
|
manager.dispose();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('AccountSyncManager schedules JMAP sync for multiple accounts',
|
||||||
|
() async {
|
||||||
|
final accounts = _FakeAccounts('pw');
|
||||||
|
final mailboxes = _FakeMailboxes();
|
||||||
|
final emails = _FakeEmails();
|
||||||
|
final logs = _FakeLogs();
|
||||||
|
|
||||||
|
final manager = AccountSyncManager(
|
||||||
|
accounts,
|
||||||
|
mailboxes,
|
||||||
|
emails,
|
||||||
|
syncLog: logs,
|
||||||
|
);
|
||||||
|
|
||||||
|
final a1 = _jmapAccount('1');
|
||||||
|
final a2 = _jmapAccount('2');
|
||||||
|
|
||||||
|
manager.start();
|
||||||
|
accounts.push([a1, a2]);
|
||||||
|
|
||||||
|
await Future<void>.delayed(const Duration(milliseconds: 100));
|
||||||
|
|
||||||
|
expect(emails.syncCounts['1'], greaterThanOrEqualTo(1));
|
||||||
|
expect(emails.syncCounts['2'], greaterThanOrEqualTo(1));
|
||||||
|
|
||||||
|
manager.dispose();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Account _account(String id) => Account(
|
Account _account(String id) => Account(
|
||||||
@@ -52,6 +91,17 @@ Account _account(String id) => Account(
|
|||||||
smtpSsl: false,
|
smtpSsl: false,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Account _jmapAccount(String id) => Account(
|
||||||
|
id: id,
|
||||||
|
displayName: 'Account $id',
|
||||||
|
email: '$id@example.com',
|
||||||
|
type: AccountType.jmap,
|
||||||
|
jmapUrl: 'http://localhost:8080/.well-known/jmap',
|
||||||
|
smtpHost: 'localhost',
|
||||||
|
smtpPort: 25,
|
||||||
|
smtpSsl: false,
|
||||||
|
);
|
||||||
|
|
||||||
class _FakeAccounts implements AccountRepository {
|
class _FakeAccounts implements AccountRepository {
|
||||||
_FakeAccounts(this.password);
|
_FakeAccounts(this.password);
|
||||||
final String password;
|
final String password;
|
||||||
@@ -140,6 +190,9 @@ class _FakeEmails implements EmailRepository {
|
|||||||
@override
|
@override
|
||||||
Future<void> setFlag(String id, {bool? seen, bool? flagged}) async {}
|
Future<void> setFlag(String id, {bool? seen, bool? flagged}) async {}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> markAllAsRead(String accountId, String mailboxPath) async {}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> moveEmail(String id, String dest) async {}
|
Future<void> moveEmail(String id, String dest) async {}
|
||||||
|
|
||||||
|
|||||||
@@ -61,6 +61,8 @@ class FakeEmailRepository implements EmailRepository {
|
|||||||
@override
|
@override
|
||||||
Future<void> setFlag(String id, {bool? seen, bool? flagged}) async {}
|
Future<void> setFlag(String id, {bool? seen, bool? flagged}) async {}
|
||||||
@override
|
@override
|
||||||
|
Future<void> markAllAsRead(String accountId, String mailboxPath) async {}
|
||||||
|
@override
|
||||||
Future<void> moveEmail(String id, String dest) async {}
|
Future<void> moveEmail(String id, String dest) async {}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -337,6 +337,23 @@ class MockEmailRepository extends _i1.Mock implements _i9.EmailRepository {
|
|||||||
returnValueForMissingStub: _i4.Future<void>.value(),
|
returnValueForMissingStub: _i4.Future<void>.value(),
|
||||||
) as _i4.Future<void>);
|
) as _i4.Future<void>);
|
||||||
|
|
||||||
|
@override
|
||||||
|
_i4.Future<void> markAllAsRead(
|
||||||
|
String? accountId,
|
||||||
|
String? mailboxPath,
|
||||||
|
) =>
|
||||||
|
(super.noSuchMethod(
|
||||||
|
Invocation.method(
|
||||||
|
#markAllAsRead,
|
||||||
|
[
|
||||||
|
accountId,
|
||||||
|
mailboxPath,
|
||||||
|
],
|
||||||
|
),
|
||||||
|
returnValue: _i4.Future<void>.value(),
|
||||||
|
returnValueForMissingStub: _i4.Future<void>.value(),
|
||||||
|
) as _i4.Future<void>);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
_i4.Future<void> moveEmail(
|
_i4.Future<void> moveEmail(
|
||||||
String? emailId,
|
String? emailId,
|
||||||
|
|||||||
@@ -126,6 +126,35 @@ abstract class EmailRepositoryContract {
|
|||||||
expect(email!.isFlagged, isTrue);
|
expect(email!.isFlagged, isTrue);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('markAllAsRead marks every unread email in the mailbox', () async {
|
||||||
|
final repo = await makeRepo();
|
||||||
|
await insertEmail(
|
||||||
|
repo,
|
||||||
|
id: 'er-acc:20',
|
||||||
|
mailboxPath: 'INBOX',
|
||||||
|
isSeen: false,
|
||||||
|
);
|
||||||
|
await insertEmail(
|
||||||
|
repo,
|
||||||
|
id: 'er-acc:21',
|
||||||
|
mailboxPath: 'INBOX',
|
||||||
|
isSeen: false,
|
||||||
|
);
|
||||||
|
await insertEmail(
|
||||||
|
repo,
|
||||||
|
id: 'er-acc:22',
|
||||||
|
mailboxPath: 'Sent',
|
||||||
|
isSeen: false,
|
||||||
|
);
|
||||||
|
|
||||||
|
await repo.markAllAsRead(_account.id, 'INBOX');
|
||||||
|
|
||||||
|
expect((await repo.getEmail('er-acc:20'))!.isSeen, isTrue);
|
||||||
|
expect((await repo.getEmail('er-acc:21'))!.isSeen, isTrue);
|
||||||
|
// Email in a different mailbox should be untouched.
|
||||||
|
expect((await repo.getEmail('er-acc:22'))!.isSeen, isFalse);
|
||||||
|
});
|
||||||
|
|
||||||
test('observeThreads starts empty', () async {
|
test('observeThreads starts empty', () async {
|
||||||
final repo = await makeRepo();
|
final repo = await makeRepo();
|
||||||
expect(
|
expect(
|
||||||
|
|||||||
@@ -103,6 +103,8 @@ class _CountingEmails implements EmailRepository {
|
|||||||
@override
|
@override
|
||||||
Future<void> setFlag(String id, {bool? seen, bool? flagged}) async {}
|
Future<void> setFlag(String id, {bool? seen, bool? flagged}) async {}
|
||||||
@override
|
@override
|
||||||
|
Future<void> markAllAsRead(String accountId, String mailboxPath) async {}
|
||||||
|
@override
|
||||||
Future<void> moveEmail(String id, String dest) async {}
|
Future<void> moveEmail(String id, String dest) async {}
|
||||||
@override
|
@override
|
||||||
Future<String?> deleteEmail(String id) async => null;
|
Future<String?> deleteEmail(String id) async => null;
|
||||||
|
|||||||
@@ -197,6 +197,23 @@ class MockEmailRepository extends _i1.Mock implements _i3.EmailRepository {
|
|||||||
returnValueForMissingStub: _i4.Future<void>.value(),
|
returnValueForMissingStub: _i4.Future<void>.value(),
|
||||||
) as _i4.Future<void>);
|
) as _i4.Future<void>);
|
||||||
|
|
||||||
|
@override
|
||||||
|
_i4.Future<void> markAllAsRead(
|
||||||
|
String? accountId,
|
||||||
|
String? mailboxPath,
|
||||||
|
) =>
|
||||||
|
(super.noSuchMethod(
|
||||||
|
Invocation.method(
|
||||||
|
#markAllAsRead,
|
||||||
|
[
|
||||||
|
accountId,
|
||||||
|
mailboxPath,
|
||||||
|
],
|
||||||
|
),
|
||||||
|
returnValue: _i4.Future<void>.value(),
|
||||||
|
returnValueForMissingStub: _i4.Future<void>.value(),
|
||||||
|
) as _i4.Future<void>);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
_i4.Future<void> moveEmail(
|
_i4.Future<void> moveEmail(
|
||||||
String? emailId,
|
String? emailId,
|
||||||
|
|||||||
@@ -214,6 +214,8 @@ class FakeEmailRepository implements EmailRepository {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> setFlag(String emailId, {bool? seen, bool? flagged}) async {}
|
Future<void> setFlag(String emailId, {bool? seen, bool? flagged}) async {}
|
||||||
|
@override
|
||||||
|
Future<void> markAllAsRead(String accountId, String mailboxPath) async {}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> moveEmail(String emailId, String destMailboxPath) async {}
|
Future<void> moveEmail(String emailId, String destMailboxPath) async {}
|
||||||
|
|||||||
Reference in New Issue
Block a user