72 lines
2.6 KiB
JavaScript
72 lines
2.6 KiB
JavaScript
const { getAddresseId } = require("../sql/mail");
|
|
const { DEBUG } = require("../utils/debug");
|
|
|
|
const { registerMessage, registerMailbox_message, saveHeader_fields, saveAddress_fields } = require('../sql/saveMessage');
|
|
const { getMailboxId, getField } = require('../sql/mail');
|
|
|
|
function saveMessage(message, attributes, mailbox) {
|
|
const timestamp = new Date(attributes.envelope.date).getTime();
|
|
const rfc822size = 0; // todo
|
|
registerMessage(timestamp, rfc822size).then((messageId) => {
|
|
getMailboxId(mailbox).then((mailboxId) => {
|
|
const seen = attributes.flags.includes('Seen') ? 1 : 0; // todo verify
|
|
const deleted = attributes.flags.includes('Deleted') ? 1 : 0; // todo verify
|
|
registerMailbox_message(mailboxId, attributes.uid, messageId, attributes.modseq, seen, deleted).then(() => {
|
|
attributes.struct.forEach(part => {
|
|
// saveBodyparts().then((bodypartId) => {
|
|
// const partText = undefined;
|
|
// savePart_numbers(messageId, partText, bodypartId, part.size, part.lines)
|
|
// });
|
|
});
|
|
const part = ''; // todo
|
|
Object.keys(attributes.envelope).forEach(key => {
|
|
const newKey = keyNormalizer(key);
|
|
if (isHeader(newKey)) {
|
|
getField(newKey).then((fieldId) => {
|
|
saveHeader_fields(messageId, part, 2, fieldId, attributes.envelope[key]);
|
|
});
|
|
} else {
|
|
getField(newKey).then((fieldId) => {
|
|
if (attributes.envelope[key]) {
|
|
attributes.envelope[key].forEach((elt, index) => {
|
|
saveAddress_fields(messageId, part, fieldId, index, getAddresseId(`${elt.mailbox}@${elt.host}`, elt.name));
|
|
});
|
|
}
|
|
});
|
|
}
|
|
});
|
|
// todo add date field
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
function isHeader(key) {
|
|
switch (key) {
|
|
case 'date':
|
|
case 'subject':
|
|
case 'messageId':
|
|
return true;
|
|
case 'from':
|
|
case 'sender':
|
|
case 'replyTo':
|
|
case 'to':
|
|
case 'cc':
|
|
case 'bcc':
|
|
case 'inReplyTo':
|
|
return false;
|
|
default:
|
|
DEBUG.log("Unknown header key: "+key);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
function keyNormalizer(key) {
|
|
// todo
|
|
return key;
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
saveMessage
|
|
} |