save message working without reply
This commit is contained in:
@@ -2,9 +2,11 @@ const Imap = require("imap");
|
||||
const { simpleParser } = require("mailparser");
|
||||
const inspect = require("util").inspect;
|
||||
const saveMessage = require("./storeMessage").saveMessage;
|
||||
const registerMessageInApp = require("../app/saveMessage").registerMessageInApp;
|
||||
const imapConfig = require("./config.json").mail;
|
||||
|
||||
const fs = require("fs");
|
||||
const { DEBUG } = require("../utils/debug");
|
||||
const imap = new Imap({
|
||||
user: imapConfig.user,
|
||||
password: imapConfig.password,
|
||||
@@ -20,57 +22,56 @@ let shouldReset = false;
|
||||
// let shouldReset = true;
|
||||
|
||||
if (shouldReset) {
|
||||
|
||||
|
||||
const { execQuery, execQueryAsync } = require("../sql/bdd");
|
||||
const query =
|
||||
"SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE table_schema = 'mail'";
|
||||
const { execQuery, execQueryAsync } = require("../db/db");
|
||||
const query = "SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE table_schema = 'mail'";
|
||||
execQueryAsync(query).then((results) => {
|
||||
execQuery("SET FOREIGN_KEY_CHECKS=0");
|
||||
results.map((table) => {
|
||||
execQuery("DELETE FROM " + table.table_name);
|
||||
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
imap.once("ready", function () {
|
||||
const readOnly = true;
|
||||
imap.openBox("INBOX", readOnly, (err, box) => {
|
||||
// console.log(box); // uidvalidty uidnext, messages total and new
|
||||
imap.search(["ALL"], function (err, results) {
|
||||
console.log(results[results.length - 1]);
|
||||
});
|
||||
// imap.search(["ALL"], function (err, results) {
|
||||
// console.log(results[results.length - 1]);
|
||||
// });
|
||||
|
||||
const f = imap.fetch(970, {
|
||||
// const f = imap.fetch(970, {
|
||||
// size: true,
|
||||
// envelope: true,
|
||||
// });
|
||||
const promises = [];
|
||||
const mails = [];
|
||||
var f = imap.seq.fetch('1:10', {
|
||||
size: true,
|
||||
envelope: true,
|
||||
});
|
||||
let messageIDs = [];
|
||||
// var f = imap.seq.fetch('1:3', {
|
||||
// bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
|
||||
// struct: true
|
||||
// });
|
||||
envelope: true
|
||||
});
|
||||
f.on("message", function (msg, seqno) {
|
||||
msg.once("attributes", (attrs) => {
|
||||
// todo find boxId
|
||||
const boxId = 1;
|
||||
messageIDs.push(attrs.envelope.messageId)
|
||||
saveMessage(attrs, boxId, imap);
|
||||
mails.push(attrs);
|
||||
promises.push(saveMessage(attrs, boxId, imap));
|
||||
});
|
||||
});
|
||||
|
||||
f.once("error", function (err) {
|
||||
console.log("Fetch error: " + err);
|
||||
DEBUG.log("Fetch error: " + err);
|
||||
});
|
||||
f.once("end", function () {
|
||||
console.log(messageIDs)
|
||||
isDone = true;
|
||||
console.log("Done fetching all messages!");
|
||||
f.once("end", async function () {
|
||||
Promise.all(promises).then(async (res) => {
|
||||
DEBUG.log("Done fetching all messages!");
|
||||
for (let i = 0; i < mails.length; i++) {
|
||||
await registerMessageInApp(res[i], mails[i]);
|
||||
}
|
||||
});
|
||||
});
|
||||
// imap.end()
|
||||
// imap.end()
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const { getAddresseId } = require("../sql/mail");
|
||||
const { getAddresseId } = require("../db/mail");
|
||||
const { DEBUG } = require("../utils/debug");
|
||||
const { simpleParser } = require("mailparser");
|
||||
const moment = require("moment");
|
||||
@@ -11,95 +11,84 @@ const {
|
||||
registerBodypart,
|
||||
saveBodypart,
|
||||
saveSource,
|
||||
} = require("../sql/saveMessage");
|
||||
} = require("../db/saveMessage");
|
||||
|
||||
const {
|
||||
createRoom,
|
||||
registerMessageInRoom,
|
||||
createThread,
|
||||
registerMessageInThread,
|
||||
isRoomGroup,
|
||||
findSpacesFromMessage,
|
||||
hasSameMembersAsParent,
|
||||
} = require("../sql/saveMessageApp");
|
||||
const { getFieldId, findRoomByOwner } = require("../sql/mail");
|
||||
const { getFieldId } = require("../db/mail");
|
||||
|
||||
function saveMessage(attrs, mailboxId, imap) {
|
||||
const envelope = attrs.envelope;
|
||||
const ts = moment(new Date(envelope.date).getTime()).format(
|
||||
"YYYY-MM-DD HH:mm:ss"
|
||||
);
|
||||
const ts = moment(new Date(envelope.date).getTime()).format("YYYY-MM-DD HH:mm:ss");
|
||||
const rfc822size = attrs.size;
|
||||
const messageID = envelope.messageId;
|
||||
|
||||
registerMessage(ts, rfc822size, messageID)
|
||||
.then((messageId) => {
|
||||
const isSeen = attrs.flags.includes("Seen") ? 1 : 0; // todo verify
|
||||
const deleted = attrs.flags.includes("Deleted") ? 1 : 0; // todo verify
|
||||
return new Promise((resolve, reject) => {
|
||||
registerMessage(ts, rfc822size, messageID)
|
||||
.then((messageId) => {
|
||||
const isSeen = attrs.flags.includes("Seen") ? 1 : 0; // todo verify
|
||||
const deleted = attrs.flags.includes("Deleted") ? 1 : 0; // todo verify
|
||||
|
||||
registerMailbox_message(
|
||||
mailboxId,
|
||||
attrs.uid,
|
||||
messageId,
|
||||
attrs.modseq,
|
||||
isSeen,
|
||||
deleted
|
||||
);
|
||||
const f = imap.fetch(attrs.uid, { bodies: "" });
|
||||
let buffer = "";
|
||||
registerMailbox_message(mailboxId, attrs.uid, messageId, attrs.modseq, isSeen, deleted);
|
||||
const f = imap.fetch(attrs.uid, { bodies: "" });
|
||||
let buffer = "";
|
||||
|
||||
f.on("message", function (msg, seqno) {
|
||||
msg.on("body", function (stream, info) {
|
||||
stream.on("data", function (chunk) {
|
||||
buffer += chunk.toString("utf8");
|
||||
});
|
||||
f.on("message", function (msg, seqno) {
|
||||
msg.on("body", function (stream, info) {
|
||||
stream.on("data", function (chunk) {
|
||||
buffer += chunk.toString("utf8");
|
||||
});
|
||||
|
||||
stream.once("end", () => {
|
||||
// save raw data
|
||||
saveSource(messageId, buffer);
|
||||
stream.once("end", () => {
|
||||
// save raw data
|
||||
saveSource(messageId, buffer);
|
||||
|
||||
// parse data
|
||||
simpleParser(buffer, async (err, parsed) => {
|
||||
saveFromParsedData(parsed, messageId);
|
||||
// parse data
|
||||
simpleParser(buffer, async (err, parsed) => {
|
||||
saveFromParsedData(parsed, messageId)
|
||||
.then(() => {
|
||||
resolve(messageId);
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
f.once("error", function (err) {
|
||||
console.log("Fetch error: " + err);
|
||||
});
|
||||
f.once("end", function () {
|
||||
DEBUG.log("Done fetching data of "+messageID);
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
DEBUG.log("Unable to register message: " + err);
|
||||
reject(err);
|
||||
});
|
||||
f.once("error", function (err) {
|
||||
console.log("Fetch error: " + err);
|
||||
});
|
||||
f.once("end", function () {
|
||||
console.log("Done fetching all messages!");
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
DEBUG.log("Unable to register message: " + err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function saveFromParsedData(parsed, messageId) {
|
||||
fs.writeFileSync("./test.txt", JSON.stringify(parsed));
|
||||
async function saveFromParsedData(parsed, messageId) {
|
||||
const promises = [];
|
||||
Object.keys(parsed).forEach((key) => {
|
||||
if (["from", "to", "cc", "bcc", "reply-to"].includes(key)) {
|
||||
// save address field
|
||||
getFieldId(key).then((fieldId) => {
|
||||
parsed[key].value.forEach((addr, nb) => {
|
||||
getAddresseId(addr.address, addr.name).then((addressId) => {
|
||||
saveAddress_fields(messageId, fieldId, addressId, nb);
|
||||
promises.push(
|
||||
// save address field
|
||||
getFieldId(key).then((fieldId) => {
|
||||
parsed[key].value.forEach((addr, nb) => {
|
||||
getAddresseId(addr.address, addr.name).then(async (addressId) => {
|
||||
await saveAddress_fields(messageId, fieldId, addressId, nb);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}),
|
||||
);
|
||||
} else if (["subject", "inReplyTo"].includes(key)) {
|
||||
// todo : "references"
|
||||
getFieldId(key).then((fieldId) => {
|
||||
saveHeader_fields(
|
||||
messageId,
|
||||
fieldId,
|
||||
undefined,
|
||||
undefined,
|
||||
parsed[key]
|
||||
);
|
||||
});
|
||||
promises.push(
|
||||
getFieldId(key).then(async (fieldId) => {
|
||||
await saveHeader_fields(messageId, fieldId, undefined, undefined, parsed[key]);
|
||||
}),
|
||||
);
|
||||
} else if (["html", "text", "textAsHtml"].includes(key)) {
|
||||
const hash = "0";
|
||||
const size = "0";
|
||||
@@ -116,9 +105,7 @@ function saveFromParsedData(parsed, messageId) {
|
||||
// });
|
||||
} else if (key == "attachments") {
|
||||
// todo
|
||||
} else if (
|
||||
["date", "messageId", "headers", "headerLines"].includes(key)
|
||||
) {
|
||||
} else if (["date", "messageId", "headers", "headerLines"].includes(key)) {
|
||||
// messageId and date are already saved
|
||||
// other field are not improted and can be retrieved in source
|
||||
return;
|
||||
@@ -127,104 +114,10 @@ function saveFromParsedData(parsed, messageId) {
|
||||
return;
|
||||
}
|
||||
});
|
||||
return Promise.all(promises);
|
||||
// todo when transfered
|
||||
}
|
||||
|
||||
function haveSameReceivers() {
|
||||
// take cc and to
|
||||
}
|
||||
|
||||
/**
|
||||
* take object address and join mailbox and host to return mailbox@host
|
||||
*/
|
||||
function createAddress(elt) {
|
||||
return `${elt.mailbox}@${elt.host}`;
|
||||
}
|
||||
|
||||
function registerMessageInApp(envelope, messageId, isSeen) {
|
||||
getAddresseId(createAddress(envelope.sender[0])).then((ownerId) => {
|
||||
if (envelope.inReplyTo) {
|
||||
registerReplyMessage(envelope, messageId, isSeen);
|
||||
} else {
|
||||
findRoomByOwner(ownerId).then((res) => {
|
||||
if (res.length == 0) {
|
||||
createRoom(envelope.subject, ownerId).then((roomId) => {
|
||||
registerMessageInRoom(messageId, roomId, isSeen);
|
||||
});
|
||||
} else {
|
||||
registerMessageInRoom(messageId, res[0].room_id, isSeen);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function registerReplyMessage(envelope, messageId, isSeen) {
|
||||
const messageID = envelope.messageId;
|
||||
findSpacesFromMessage(messageId).then((spaces) => {
|
||||
// todo sub thread will not be in index 0 so look in all indexes
|
||||
if (spaces.length == 0) {
|
||||
// no space, so is a transfer
|
||||
// todo test if members of transferred message are included
|
||||
} else if (spaces[0].thread_id) {
|
||||
registerMessageInThread(messageId, spaces[0].thread_id, isSeen);
|
||||
// todo
|
||||
// if (hasSameMembersAsParent(messageID, envelope.inReplyTo)) {
|
||||
// // register new message in thread
|
||||
// // possibly convert to room only if parent is channel
|
||||
// } else {
|
||||
// // todo create sub thread
|
||||
// }
|
||||
} else if (spaces[0].room_id) {
|
||||
// message in room and not thread
|
||||
isRoomGroup(spaces[0].room_id).then((isGroup) => {
|
||||
if (isGroup) {
|
||||
if (hasSameMembersAsParent(messageID, envelope.inReplyTo)) {
|
||||
registerMessageInRoom(
|
||||
messageId,
|
||||
spaces[0].room_id,
|
||||
isSeen
|
||||
);
|
||||
} else {
|
||||
// group and not the same member as the reply
|
||||
// some recipient has been removed create a thread
|
||||
const isDm = 0; // todo
|
||||
createThread(
|
||||
space[0].room_id,
|
||||
envelope.subject,
|
||||
isSeen,
|
||||
isDm
|
||||
).then((threadId) => {
|
||||
registerMessageInThread(
|
||||
messageId,
|
||||
threadId,
|
||||
isSeen
|
||||
);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// reply from channel
|
||||
// todo
|
||||
// if (messageInRoom == 1) { // was new channel transform to group
|
||||
// // register new message in group
|
||||
// } else if (sender == owner) { // correction from the original sender
|
||||
// // leave in the same channel
|
||||
// } else { // user response to announcement
|
||||
// // create new thread
|
||||
// }
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// `SELECT app_room_messages.room, app_room_messages.thread FROM app_room_messages INNER JOIN messages WHERE messages.messageID = '${envelope.inReplyTo}' AND app_room_messages.message = messages.id`;
|
||||
}
|
||||
|
||||
function keyNormalizer(key) {
|
||||
// todo
|
||||
return key;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
saveMessage,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user