improve syncing and storing

This commit is contained in:
grimhilt
2023-03-29 16:23:24 +02:00
parent 185f051a63
commit 91898e25a5
15 changed files with 245 additions and 347 deletions

View File

@@ -4,10 +4,10 @@ const { registerMessageInApp } = require("../saveMessage");
const { saveMessage } = require("../storeMessage");
class Box {
constructor(_imap, boxId, _boxName) {
constructor(_imap, _boxId, _boxName) {
this.imap = _imap;
this.boxName = _boxName;
this.id = boxId;
this.id = _boxId;
this.box;
this.init();
}
@@ -25,6 +25,7 @@ class Box {
sync(savedUid, currentUid) {
const promises = [];
const mails = [];
logger.log(`Syncing from ${savedUid} to ${currentUid} uid`);
const f = this.imap.seq.fetch(`${savedUid}:${currentUid}`, {
size: true,
envelope: true,
@@ -42,12 +43,29 @@ class Box {
});
f.once("end", async () => {
await Promise.all(promises).then(async (res) => {
for (let i = 0; i < mails.length; i++) {
console.log(i, mails[i].uid)
await registerMessageInApp(res[i], mails[i]);
let step = 20;
for (let i = 0; i < promises.length; i += step) {
for (let j = i; j < (i + step && promises.length); j++) {
console.log(j, promises.length, promises[j])
await new Promise((resolve, reject) => {
promises[j]
.then(async (res) => {
await registerMessageInApp(res, mails[j], this.id);
resolve();
})
.catch((err) => {
reject(err);
});
});
}
});
logger.log(`Saved messages ${i + step > promises.length ? promises.length : i + step}/${mails.length}`);
updateMailbox(this.id, mails[i].uid);
}
// await Promise.all(promises).then(async (res) => {
// for (let i = 0; i < mails.length; i++) {
// logger.log(`Saved message ${i}/${mails.length}`);
// }
// });
updateMailbox(this.id, currentUid);
});
}

View File

@@ -13,7 +13,6 @@ class ImapInstance {
port: account.port,
tls: account.tls,
});
this.account = account;
this.boxes = [];
@@ -25,11 +24,11 @@ class ImapInstance {
this.imapReady();
});
this.imap.once("error", function (err) {
this.imap.once("error", (err) => {
logger.error("Imap error for " + this.account.user + ": " + err);
});
this.imap.once("end", function () {
this.imap.once("end", () => {
logger.log("Connection ended for " + this.account.user);
});
@@ -54,10 +53,11 @@ class ImapInstance {
getAllBox(boxes) {
// ideally we should get the all box to get all messages
let allBox;
let allBox = '';
Object.keys(boxes).forEach((key) => {
if (key === "INBOX") return;
if (allBox.includes("/")) return;
if (allBox.includes("/")) return; // already found
if (!boxes[key].children) return; // no children
allBox = key;
Object.keys(boxes[key].children).forEach((childBoxes) => {
if (boxes[key].children[childBoxes].attribs.includes("\\All")) {