refactor: clean project

This commit is contained in:
2024-05-11 00:18:20 +03:00
parent 49f93c7b8a
commit db6f3e95fd
2 changed files with 6 additions and 58 deletions

View File

@@ -1,6 +1,5 @@
const {ImapFlow} = require('imapflow');
const simpleParser = require('mailparser').simpleParser
const dotenv = require('dotenv')
require('dotenv').config()
@@ -19,76 +18,25 @@ const client = new ImapFlow({
const markString = 'test'
const concat_RS = (stream: any) => new Promise((res, rej) => {
let buffers: any = [];
stream.on("data", (data: any) => {
buffers.push(data);
});
stream.on("end", () => {
res(Buffer.concat(buffers));
});
});
const main = async () => {
await client.connect(); // Wait until client connects and authorizes
await client.mailboxOpen('INBOX'); // Open mailbox
//let lock = await client.getMailboxLock('INBOX'); // Select and lock a mailbox. Throws if mailbox does not exist
let mailbox = await client.mailboxOpen('INBOX');
try {
// // fetch latest message source
// // client.mailbox includes information about currently selected mailbox
// // "exists" value is also the largest sequence number available in the mailbox
// let message = await client.fetchOne(client.mailbox.exists, { source: true });
// console.log(message.source.toString());
//
// // list subjects for all messages
// // uid value is always included in FETCH response, envelope strings are in unicode.
// for await (let message of client.fetch('1:*', { envelope: true })) {
// console.log(`${message.uid}: ${message.envelope.subject}`);
// }
// await client.messageFlagsRemove({seen: false}, ["\\Seen"], {uid:true}); // mark all seen messages as unseen by removing Seen flag
// await client.messageFlagsSet({seen: false}, ["\\Seen"]); // mark all unseen messages as seen (and remove other flags)
//let list = await client.search({seen: false}); // find all unseen messages
//let lastMsg = await client.fetchOne('*', {uid: true}) // fetch one message
let listSeenMsgId = await client.search({seen: false});
let listSeenMsgId = await client.search({seen: false}); // search only unseen messages
console.log('📌', 'LIST:', listSeenMsgId)
// for await (let msg of client.fetch(listSeenMsgId, {uid: false})){
// console.log(msg.envelope.subject);
// }
for await (let message of client.fetch(listSeenMsgId,
{
envelope: true,
//bodyParts: true,
bodyParts: true,
bodyStructure: true
})) {
if (message.envelope.subject.includes(markString)) {
console.log('📌', message.envelope.subject, message.seq, message.bodyStructure);
let bs;
if (message.bodyStructure.childNodes) {
let nodeHTML = message.bodyStructure?.childNodes?.find((node: any) => node.type?.toLowerCase() === 'text/html');
let nodePlainText = message.bodyStructure?.childNodes?.find((node: any) => node.type?.toLowerCase() === 'text/plain');
bs = nodePlainText ? nodePlainText : nodeHTML;
} else {
if (message.bodyStructure.type === 'text/plain') {
bs = {
part: "1",
type: "text/plain"
}
}
}
}
}
} finally {
//lock.release(); // Make sure lock is released, otherwise next `getMailboxLock()` never returns
} catch (e) {
console.log(e)
}
await client.logout(); // log out and close connection