|
|
@@ -1291,18 +1291,27 @@ export function createInboundMessage(mailbox, message = {}) {
|
|
|
return getInboundMessage(mailbox.userId, result.lastInsertRowid);
|
|
|
}
|
|
|
|
|
|
+export const importedInboundMessageLookupSql = `
|
|
|
+ SELECT id
|
|
|
+ FROM inbound_messages
|
|
|
+ WHERE import_source = ?
|
|
|
+ AND import_source_key = ?
|
|
|
+ AND import_source != ''
|
|
|
+ AND import_source_key != ''
|
|
|
+ LIMIT 1
|
|
|
+`;
|
|
|
+
|
|
|
+function findImportedInboundMessage(importSource, sourceKey) {
|
|
|
+ return requireDb()
|
|
|
+ .prepare(importedInboundMessageLookupSql)
|
|
|
+ .get(importSource, sourceKey);
|
|
|
+}
|
|
|
+
|
|
|
export function hasImportedInboundMessage(importSource, sourceKey) {
|
|
|
const source = normalizeImportSource(importSource);
|
|
|
const key = normalizeImportSourceKey(sourceKey);
|
|
|
if (!source || !key) return false;
|
|
|
- return Boolean(requireDb()
|
|
|
- .prepare(`
|
|
|
- SELECT 1
|
|
|
- FROM inbound_messages
|
|
|
- WHERE import_source = ? AND import_source_key = ?
|
|
|
- LIMIT 1
|
|
|
- `)
|
|
|
- .get(source, key));
|
|
|
+ return Boolean(findImportedInboundMessage(source, key));
|
|
|
}
|
|
|
|
|
|
export function createImportedInboundMessage(mailbox, message = {}) {
|
|
|
@@ -1310,9 +1319,7 @@ export function createImportedInboundMessage(mailbox, message = {}) {
|
|
|
const importSource = normalizeImportSource(message.importSource);
|
|
|
const sourceKey = normalizeImportSourceKey(message.sourceKey);
|
|
|
if (!importSource || !sourceKey) throw new Error('导入邮件缺少稳定来源标识。');
|
|
|
- const existing = requireDb()
|
|
|
- .prepare('SELECT id FROM inbound_messages WHERE import_source = ? AND import_source_key = ? LIMIT 1')
|
|
|
- .get(importSource, sourceKey);
|
|
|
+ const existing = findImportedInboundMessage(importSource, sourceKey);
|
|
|
if (existing) return { created: false, message: { id: Number(existing.id) } };
|
|
|
|
|
|
const receivedAt = normalizeImportedReceivedAt(message.receivedAt);
|
|
|
@@ -1362,9 +1369,7 @@ export function createImportedInboundMessage(mailbox, message = {}) {
|
|
|
if (result.changes) {
|
|
|
return { created: true, message: { id: Number(result.lastInsertRowid) } };
|
|
|
}
|
|
|
- const concurrent = requireDb()
|
|
|
- .prepare('SELECT id FROM inbound_messages WHERE import_source = ? AND import_source_key = ? LIMIT 1')
|
|
|
- .get(importSource, sourceKey);
|
|
|
+ const concurrent = findImportedInboundMessage(importSource, sourceKey);
|
|
|
if (!concurrent) throw new Error('导入邮件写入失败。');
|
|
|
return { created: false, message: { id: Number(concurrent.id) } };
|
|
|
}
|