started some app structure

This commit is contained in:
grimhilt
2023-03-13 00:13:17 +01:00
parent 749127ac19
commit c81042a223
8 changed files with 459 additions and 111 deletions

View File

@@ -22,7 +22,7 @@ imap.once("ready", function () {
console.log(results[results.length - 1]);
});
const f = imap.fetch(970, { bodies: ['TEXT'], size: true, struct: true, envelope: true });
const f = imap.fetch(970, { size: true, struct: true, envelope: true });
// var f = imap.seq.fetch('1:3', {
// bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
// struct: true
@@ -30,23 +30,23 @@ imap.once("ready", function () {
f.on("message", function (msg, seqno) {
// console.log("Message #%d", seqno);
// var prefix = "(#" + seqno + ") ";
msg.on("body", function (stream, info) {
simpleParser(stream, async (err, parsed) => {
// find box id;
console.log(parsed)
const boxId = 1;
// saveMessage(parsed, boxId);
console.log(parsed.subject);
fs.writeFileSync("./test.txt", JSON.stringify(parsed));
});
// console.log(prefix + 'Body');
// stream.pipe(fs.createWriteStream('msg-' + seqno + '-body.txt'));
});
// msg.on("body", function (stream, info) {
// simpleParser(stream, async (err, parsed) => {
// // find box id;
// // console.log(parsed)
// const boxId = 1;
// saveMessage(parsed, boxId);
// // console.log(parsed.subject);
// // fs.writeFileSync("./test.txt", JSON.stringify(parsed));
// });
// // console.log(prefix + 'Body');
// // stream.pipe(fs.createWriteStream('msg-' + seqno + '-body.txt'));
// });
msg.once('attributes', attrs => {
// todo find boxId
const boxId = 1;
console.log(attrs)
// saveMessage(attrs, boxId, imap);
// console.log(attrs)
saveMessage(attrs, boxId, imap);
});
});
@@ -58,7 +58,7 @@ imap.once("ready", function () {
});
f.once("end", function () {
console.log("Done fetching all messages!");
imap.end();
// imap.end();
});
// });
return;
@@ -135,66 +135,6 @@ imap.once("end", function () {
imap.connect();
// const getEmails = () => {
// imap.once('ready', () => {
// imap.openBox('INBOX', false, () => {
// imap.search(['UNSEEN'], (err, results) => {
// const f = imap.fetch(results, {bodies: ''});
// f.on('message', msg => {
// msg.on('body', stream => {
// simpleParser(stream, async (err, parsed) => {
// // const {from, subject, textAsHtml, text} = parsed;
// // console.log(parsed.from.value);
// // console.log(parsed.subject);
// // console.log(parsed.date)
// // console.log(parsed.replyTo.value);
// // console.log(parsed.messageId);
// // console.log(parsed.html);
// // console.log(parsed.text);
// // console.log(parsed.textAsHtml);
// // 'x-emsg-mtaselection' => 'prod_5_emailing.carrefour.fr',
// // 'message-id' => '<emsg.6584.7d09.15dd1ec64c1@ukmme02.em.unica.net>',
// // 'feedback-id' => 'emailing.carrefour.fr:10070-6584:emsg-x',
// // 'list' => { unsubscribe: [Object] },
// // 'mime-version' => '1.0',
// // 'content-type' => { value: 'text/html', params: [Object] },
// // 'content-transfer-encoding' => 'quoted-printable'
// });
// });
// msg.once('attributes', attrs => {
// const {uid} = attrs;
// console.log(uid)
// // imap.addFlags(uid, ['\\Seen'], () => {
// // // Mark the email as read after reading it
// // console.log('Marked as read!');
// // });
// });
// });
// f.once('error', ex => {
// return Promise.reject(ex);
// });
// f.once('end', () => {
// console.log('Done fetching all messages!');
// imap.end();
// });
// });
// });
// });
// imap.once('error', err => {
// console.log(err);
// });
// imap.once('end', () => {
// console.log('Connection ended');
// });
// imap.connect();
// };
// getEmails();
function isValidEmail(email) {
// todo

View File

@@ -8,9 +8,17 @@ const {
saveHeader_fields,
saveAddress_fields,
registerBodypart,
saveBodypart
saveBodypart,
} = require("../sql/saveMessage");
const { getFieldId } = require("../sql/mail");
const {
createRoom,
registerMessageInRoom,
createThread,
registerMessageInThread,
isRoomGroup
} = require("../sql/saveMessageApp");
const { getFieldId, findRoomByOwner } = require("../sql/mail");
function saveMessage(attrs, mailboxId, imap) {
const envelope = attrs.envelope;
@@ -95,34 +103,77 @@ function saveMessage(attrs, mailboxId, imap) {
}
});
// todo check for different provider
if (envelope?.inReplyTo) {
`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`;
// parent message is in a thread, so register this message only in a thread
if (thread) {
`INSERT IGNORE INTO app_room_messages (thread, message) VALUES('${thread}', '${messageId}')`;
// increment not read counter and lastUpdate is not read (both)
} else if (room) {
if (!isGroup) {
// check if create thread
} else {
// check if create new group and delete thread
}
} else {
}
}
/**
* if reply to then add to same group as previous and update type if necessary
* else add to sender group
*
*/
// todo check for different provider name of inreplyto
registerMessageInApp()
}
);
}
function haveSameReceivers() {
// take cc and to
}
function registerMessageInApp() {
if (envelope.inReplyTo) {
registerReplyMessage();
} else {
findRoomByOwner(ownerId).then((res) => {
if (res.length == 0) {
registerMessageInRoom(messageId, res[0].id);
} else {
createRoom(envelope.subject, ownerId, envelope.flags.includes["seen"]).then((roomId) => {
registerMessageInRoom(messageId, roomId);
});
}
});
}
}
function registerReplyMessage() {
findSpaceFromMessage(messageId).then((spaces) => {
if(spaces.length == 0) { // no space, so is a transfer
// todo test if members of transferred message are included
} else if (spaces[0].room) { // message in room
isRoomGroup(spaces[0].room_id).then((isGroup) => {
if (isGroup) {
if (hasSameMembersAsParent(messageId)) {
registerMessageInRoom(messageId, spaces[0].room_id);
} else {
// group and not the same member as the reply
// some recipient has been removed create a thread
const notSeen = 0; // todo
const isDm = 0; // todo
createThread(space[0].room_id, envelope.subject, notSeen, isDm).then((threadId) => {
registerMessageInThread(messageId, threadId);
});
}
} 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
// }
}
});
} else if (spaces[0].thread) { // message in thread
// todo
// if (hasSameMembersAsParent(messageId)) {
// // register new message in thread
// // possibly convert to room only if parent is channel
// } else {
// // create sub 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 findTextPart(struct) {
for (var i = 0, len = struct.length, r; i < len; ++i) {
if (Array.isArray(struct[i])) {