- IPC-канал messages:search + тип в IpcChannelMap - IMAP: server-side SEARCH OR SUBJECT FROM по всем папкам - Gmail: search через API q-параметр - Exchange: stub (пустой результат) - messages.store: состояние searchQuery/searchResults/searchLoading + actions search/clearSearch - Сброс поиска при смене папки - MessageList: поисковая строка в хедере (Ctrl+F / кнопка) - Результаты с бейджем папки, дебаунс 300ms, Escape закрывает - Иконки search/close в AppIcon Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
88 lines
3.4 KiB
TypeScript
88 lines
3.4 KiB
TypeScript
import { app, dialog, BrowserWindow } from 'electron'
|
|
import { writeFile, access } from 'fs/promises'
|
|
import { join, extname, basename } from 'path'
|
|
import { registerHandler } from './registry'
|
|
import type { AccountManager } from '../services/account-manager'
|
|
|
|
async function resolveDownloadPath(folder: string, filename: string): Promise<string> {
|
|
const ext = extname(filename)
|
|
const base = basename(filename, ext)
|
|
let resolved = join(folder, filename)
|
|
let counter = 1
|
|
while (true) {
|
|
try {
|
|
await access(resolved)
|
|
// файл существует — добавляем суффикс
|
|
resolved = join(folder, `${base} (${counter})${ext}`)
|
|
counter++
|
|
} catch {
|
|
// файл не существует — путь свободен
|
|
break
|
|
}
|
|
}
|
|
return resolved
|
|
}
|
|
|
|
function sendProgress(attachmentId: string, stage: 'downloading' | 'saving' | 'done' | 'error', progress: number, error?: string): void {
|
|
const win = BrowserWindow.getFocusedWindow() ?? BrowserWindow.getAllWindows()[0]
|
|
win?.webContents.send('attachment:progress', { attachmentId, stage, progress, error })
|
|
}
|
|
|
|
export function registerMessageHandlers(manager: AccountManager): void {
|
|
registerHandler('messages:list', async ({ accountId, folderId, offset, limit, unseenOnly }) => {
|
|
return manager.listMessages(accountId, folderId, offset, limit, unseenOnly ?? false)
|
|
})
|
|
|
|
registerHandler('messages:poll', async ({ accountId }) => {
|
|
await manager.pollNewMessages(accountId)
|
|
})
|
|
|
|
registerHandler('messages:search', async ({ accountId, query, limit }) => {
|
|
return manager.searchMessages(accountId, query, limit)
|
|
})
|
|
|
|
registerHandler('messages:body', async ({ accountId, folderId, messageId }) => {
|
|
return manager.fetchMessageBody(accountId, folderId, messageId)
|
|
})
|
|
|
|
registerHandler('messages:setFlags', async ({ accountId, folderId, messageIds, flags }) => {
|
|
await manager.updateFlags(accountId, folderId, messageIds, flags)
|
|
})
|
|
|
|
registerHandler('messages:delete', async ({ accountId, folderId, messageIds }) => {
|
|
await manager.deleteMessages(accountId, folderId, messageIds)
|
|
})
|
|
|
|
registerHandler('messages:attachment:download', async ({ accountId, folderId, messageId, attachmentId, filename, downloadFolder }) => {
|
|
try {
|
|
sendProgress(attachmentId, 'downloading', 10)
|
|
const data = await manager.downloadAttachment(accountId, folderId, messageId, attachmentId)
|
|
sendProgress(attachmentId, 'saving', 70)
|
|
|
|
let filePath: string
|
|
|
|
if (downloadFolder) {
|
|
// Папка задана — сохраняем без диалога
|
|
filePath = await resolveDownloadPath(downloadFolder, filename)
|
|
} else {
|
|
// Папка не задана — спрашиваем
|
|
const folder = app.getPath('downloads')
|
|
const win = BrowserWindow.getFocusedWindow() ?? BrowserWindow.getAllWindows()[0]
|
|
const result = await dialog.showSaveDialog(win, { defaultPath: join(folder, filename) })
|
|
if (result.canceled || !result.filePath) {
|
|
sendProgress(attachmentId, 'error', 0)
|
|
return { success: false }
|
|
}
|
|
filePath = result.filePath
|
|
}
|
|
|
|
await writeFile(filePath, data)
|
|
sendProgress(attachmentId, 'done', 100)
|
|
return { success: true, savedPath: filePath }
|
|
} catch (e) {
|
|
const msg = (e as Error).message
|
|
sendProgress(attachmentId, 'error', 0, msg)
|
|
return { success: false, error: msg }
|
|
}
|
|
})
|
|
}
|