diff --git a/apps/client/lib/core/i18n/language_controller.dart b/apps/client/lib/core/i18n/language_controller.dart index be9688f..bc80478 100644 --- a/apps/client/lib/core/i18n/language_controller.dart +++ b/apps/client/lib/core/i18n/language_controller.dart @@ -270,6 +270,11 @@ class AppStrings { String get statusError => _ko ? '오류' : 'Error'; String get lastSynced => _ko ? '마지막 동기화' : 'Last synced'; String get neverSynced => _ko ? '아직 없음' : 'Never'; + String get receiveMode => _ko ? '수신 방식' : 'Receive mode'; + String get receiveRealtime => _ko ? '실시간(IDLE)' : 'Realtime (IDLE)'; + String get receivePolling => _ko ? '주기 확인' : 'Periodic check'; + String get receiveUnknown => _ko ? '확인 전' : 'Not checked yet'; + String get capabilitiesChecked => _ko ? '확인' : 'checked'; String get protocol => _ko ? '수신 방식' : 'Protocol'; String get authMode => _ko ? '인증 방식' : 'Auth mode'; String authModeLabel(String mode) => switch (mode) { diff --git a/apps/client/lib/features/accounts/accounts_page.dart b/apps/client/lib/features/accounts/accounts_page.dart index 18136c1..add8169 100644 --- a/apps/client/lib/features/accounts/accounts_page.dart +++ b/apps/client/lib/features/accounts/accounts_page.dart @@ -465,6 +465,13 @@ class _AccountDetail extends ConsumerWidget { ? strings.neverSynced : account.lastSyncedAtLabel, ), + if (account.incomingProtocol == 'imap') ...[ + const SizedBox(height: 8), + _InfoRow( + label: strings.receiveMode, + value: _receiveModeLabel(account, strings), + ), + ], if (account.syncError != null) ...[ const SizedBox(height: 12), Text( @@ -477,6 +484,16 @@ class _AccountDetail extends ConsumerWidget { } } +String _receiveModeLabel(MailAccountSummary account, AppStrings strings) { + final idle = account.idleSupported; + if (idle == null) return strings.receiveUnknown; + final mode = idle ? strings.receiveRealtime : strings.receivePolling; + final checked = account.capabilitiesCheckedAtLabel; + return checked.isEmpty + ? mode + : '$mode · ${strings.capabilitiesChecked} $checked'; +} + class _InfoRow extends StatelessWidget { const _InfoRow({ required this.label, diff --git a/apps/client/lib/services/mail_api_client.dart b/apps/client/lib/services/mail_api_client.dart index be17786..6617a28 100644 --- a/apps/client/lib/services/mail_api_client.dart +++ b/apps/client/lib/services/mail_api_client.dart @@ -471,6 +471,8 @@ class MailAccountSummary { this.syncErrorCode, required this.initialSync, required this.lastSyncedAtLabel, + this.idleSupported, + this.capabilitiesCheckedAtLabel = '', this.notificationsEnabled = true, }); @@ -486,7 +488,14 @@ class MailAccountSummary { final String lastSyncedAtLabel; final bool notificationsEnabled; + /// Null until the server has logged in and read the IMAP capabilities. + final bool? idleSupported; + final String capabilitiesCheckedAtLabel; + factory MailAccountSummary.fromJson(Map json) { + final capabilities = json['incomingCapabilities'] is Map + ? json['incomingCapabilities'] as Map + : null; return MailAccountSummary( id: json['id'] as String, providerId: json['providerId'] as String, @@ -501,6 +510,9 @@ class MailAccountSummary { json['initialSync'] as Map) : null, lastSyncedAtLabel: _formatReceivedAt(json['lastSyncedAt'] as String?), + idleSupported: capabilities?['idle'] as bool?, + capabilitiesCheckedAtLabel: + _formatReceivedAt(capabilities?['checkedAt'] as String?), notificationsEnabled: json['notificationsEnabled'] as bool? ?? true, ); } diff --git a/apps/client/test/widget_test.dart b/apps/client/test/widget_test.dart index 469d2ff..67733a0 100644 --- a/apps/client/test/widget_test.dart +++ b/apps/client/test/widget_test.dart @@ -344,6 +344,43 @@ void main() { await tester.pumpWidget(const SizedBox()); }); + testWidgets('shows the receive mode from IMAP capabilities', + (WidgetTester tester) async { + final json = { + 'id': 'a', + 'providerId': 'naver', + 'emailAddress': 'a@example.com', + 'incomingProtocol': 'imap', + }; + final polling = MailAccountSummary.fromJson({ + ...json, + 'incomingCapabilities': { + 'idle': false, + 'checkedAt': '2020-01-01T00:00:00Z' + }, + }); + final unknown = MailAccountSummary.fromJson(json); + expect(polling.idleSupported, isFalse); + expect(unknown.idleSupported, isNull); + + await tester.binding.setSurfaceSize(const Size(1200, 800)); + addTearDown(() => tester.binding.setSurfaceSize(null)); + await tester.pumpWidget( + ProviderScope( + overrides: [ + accountsProvider.overrideWith((ref) async => [polling]), + providersProvider.overrideWith((ref) async => const []), + ], + child: const DailyPostBoxApp(), + ), + ); + await tester.pumpAndSettle(); + await tester.tap(find.text('계정관리').first); + await tester.pumpAndSettle(); + + expect(find.textContaining('주기 확인'), findsOneWidget); + }); + testWidgets('keeps the syncing mailbox when an account refresh fails', ( WidgetTester tester, ) async { diff --git a/apps/server/package.json b/apps/server/package.json index fd5513d..c40dd00 100644 --- a/apps/server/package.json +++ b/apps/server/package.json @@ -5,7 +5,7 @@ "type": "module", "scripts": { "dev": "tsx src/server.ts", - "test": "tsx --test test/imapSequenceRanges.test.ts test/mailSyncWorker.test.ts test/messageFlags.test.ts test/accountValidate.test.ts test/accountReconnect.test.ts test/searchQuery.test.ts test/oauthTokenManager.test.ts test/apnsPushNotifier.test.ts test/attachments.test.ts test/attachmentAccess.test.ts test/mailSearchAccess.test.ts test/replyRecipients.test.ts test/existsQueue.test.ts test/pop3Unsupported.test.ts test/deletedAccountSync.test.ts test/oauthSessionExpiry.test.ts test/noopPushNotifier.test.ts test/serverExposure.test.ts test/uidValidity.test.ts test/accountScopedMessage.test.ts test/customServerAccount.test.ts", + "test": "tsx --test test/imapSequenceRanges.test.ts test/mailSyncWorker.test.ts test/messageFlags.test.ts test/accountValidate.test.ts test/accountReconnect.test.ts test/searchQuery.test.ts test/oauthTokenManager.test.ts test/apnsPushNotifier.test.ts test/imapCapabilities.test.ts test/attachments.test.ts test/attachmentAccess.test.ts test/mailSearchAccess.test.ts test/replyRecipients.test.ts test/existsQueue.test.ts test/pop3Unsupported.test.ts test/deletedAccountSync.test.ts test/oauthSessionExpiry.test.ts test/noopPushNotifier.test.ts test/serverExposure.test.ts test/uidValidity.test.ts test/accountScopedMessage.test.ts test/customServerAccount.test.ts", "typecheck": "tsc --noEmit", "start": "node dist/server.js", "build": "tsc" diff --git a/apps/server/src/domain/types.ts b/apps/server/src/domain/types.ts index 3ecc8e0..67e8f5a 100644 --- a/apps/server/src/domain/types.ts +++ b/apps/server/src/domain/types.ts @@ -32,6 +32,15 @@ export interface MailProviderConfig { }; } +export interface IncomingCapabilities { + idle: boolean; + move: boolean; + specialUse: boolean; + condstore: boolean; + /** ISO time of the login that reported these capabilities. */ + checkedAt: string; +} + export interface MailAccount { id: string; userId: string; @@ -49,6 +58,7 @@ export interface MailAccount { limit: number | "all"; }; lastSyncedAt?: Date; + incomingCapabilities?: IncomingCapabilities; // Missing means on, so accounts stored before this field keep notifying. notificationsEnabled?: boolean; // Every message up to this UID was checked by a completed sync, in the diff --git a/apps/server/src/mail/ImapReceiver.ts b/apps/server/src/mail/ImapReceiver.ts index 7474264..5452483 100644 --- a/apps/server/src/mail/ImapReceiver.ts +++ b/apps/server/src/mail/ImapReceiver.ts @@ -37,6 +37,7 @@ import { rawAttachmentFetch } from "./attachments.js"; import { extractMessageLinks } from "./messageLinks.js"; +import { detectImapCapabilities } from "./imapCapabilities.js"; import { createExistsQueue } from "./existsQueue.js"; export type { ImapSyncCursor } from "./imapSequenceRanges.js"; @@ -60,6 +61,7 @@ export class ImapReceiver implements MailReceiver { const client = await this.createClient(account); try { await client.connect(); + this.recordCapabilities(account, client); } finally { await client.logout().catch(() => undefined); } @@ -383,6 +385,7 @@ export class ImapReceiver implements MailReceiver { try { await client.connect(); + this.recordCapabilities(account, client); await client.mailboxOpen(mailbox); } catch (error) { stopped = true; @@ -436,6 +439,14 @@ export class ImapReceiver implements MailReceiver { return client; } + // Callers persist the account, so the result survives restarts. + private recordCapabilities(account: MailAccount, client: ImapFlow): void { + const detected = detectImapCapabilities(client.capabilities); + // watch() refuses accounts without supportsImapIdle, so "idle" means IDLE is actually used. + detected.idle &&= supportsImapIdle(account); + account.incomingCapabilities = detected; + } + private async withConnectedClient( account: MailAccount, operation: (client: ImapFlow) => Promise @@ -443,6 +454,7 @@ export class ImapReceiver implements MailReceiver { const client = await this.createClient(account); try { await client.connect(); + this.recordCapabilities(account, client); return await operation(client); } finally { await client.logout().catch(() => undefined); diff --git a/apps/server/src/mail/imapCapabilities.ts b/apps/server/src/mail/imapCapabilities.ts new file mode 100644 index 0000000..7f7a2ac --- /dev/null +++ b/apps/server/src/mail/imapCapabilities.ts @@ -0,0 +1,16 @@ +import type { IncomingCapabilities } from "../domain/types.js"; + +/** Reads the post-login CAPABILITY set exposed by imapflow (`client.capabilities`). */ +export function detectImapCapabilities( + capabilities: ReadonlyMap, + now = new Date() +): IncomingCapabilities { + const has = (name: string) => capabilities.has(name); + return { + idle: has("IDLE"), + move: has("MOVE"), + specialUse: has("SPECIAL-USE"), + condstore: has("CONDSTORE"), + checkedAt: now.toISOString() + }; +} diff --git a/apps/server/src/workers/MailSyncWorker.ts b/apps/server/src/workers/MailSyncWorker.ts index c63bab8..9eed397 100644 --- a/apps/server/src/workers/MailSyncWorker.ts +++ b/apps/server/src/workers/MailSyncWorker.ts @@ -280,6 +280,9 @@ export class MailSyncWorker { scheduleReconnect ); reconnectDelay = this.options.imapReconnectBaseDelayMs; + if (account.incomingCapabilities) { + await this.accounts.updateAccount(account).catch(() => undefined); + } // Mail that arrived while disconnected raised no IDLE event. A sync // already running may have taken its UID snapshot before the outage, // so a fresh one starts after it instead of joining it. diff --git a/apps/server/test/imapCapabilities.test.ts b/apps/server/test/imapCapabilities.test.ts new file mode 100644 index 0000000..e9733dd --- /dev/null +++ b/apps/server/test/imapCapabilities.test.ts @@ -0,0 +1,40 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { detectImapCapabilities } from "../src/mail/imapCapabilities.js"; + +test("detects IMAP capabilities from the CAPABILITY set", () => { + const now = new Date("2026-11-22T00:00:00.000Z"); + const result = detectImapCapabilities( + new Map([["IMAP4REV1", true], ["IDLE", true], ["MOVE", true]]), + now + ); + assert.deepEqual(result, { + idle: true, + move: true, + specialUse: false, + condstore: false, + checkedAt: now.toISOString() + }); +}); + +test("reports no IDLE when the server does not advertise it", () => { + const result = detectImapCapabilities(new Map([["IMAP4REV1", true]])); + assert.equal(result.idle, false); +}); + +test("ImapReceiver records idle=false for providers that never run IDLE", async () => { + const { ImapReceiver } = await import("../src/mail/ImapReceiver.js"); + const receiver = new ImapReceiver({} as never, {} as never) as unknown as { + recordCapabilities(account: unknown, client: unknown): void; + }; + const client = { capabilities: new Map([["IDLE", true]]) }; + const custom: { providerId: string; incomingProtocol: string; incomingCapabilities?: { idle: boolean } } = { + providerId: "custom", + incomingProtocol: "imap" + }; + const gmail: typeof custom = { providerId: "gmail", incomingProtocol: "imap" }; + receiver.recordCapabilities(custom, client); + receiver.recordCapabilities(gmail, client); + assert.equal(custom.incomingCapabilities?.idle, false); + assert.equal(gmail.incomingCapabilities?.idle, true); +});