started to convert to typescript

This commit is contained in:
grimhilt
2023-04-01 16:32:29 +02:00
parent aced3b8914
commit 90dd16ee0d
32 changed files with 1045 additions and 3897 deletions

View File

@@ -1,9 +1,15 @@
const { getMailbox, updateMailbox } = require("../../db/imap/imap");
const { logger } = require("../../system/Logger");
const { registerMessageInApp } = require("../saveMessage");
const { saveMessage } = require("../storeMessage");
import { ImapMessageAttributes } from "imap";
import { getMailbox, updateMailbox } from "../../db/imap/imap";
import logger from "../../system/Logger";
import RegisterMessageInApp from "../saveMessage";
import { saveMessage } from "../storeMessage";
export default class Box {
imap: Imap;
boxName: string;
id: number;
box: Object;
class Box {
constructor(_imap, _boxId, _boxName) {
this.imap = _imap;
this.boxName = _boxName;
@@ -23,16 +29,18 @@ class Box {
}
sync(savedUid, currentUid) {
const promises = [];
const mails = [];
const promises: Promise<unknown>[] = [];
const mails: ImapMessageAttributes[] = [];
logger.log(`Syncing from ${savedUid} to ${currentUid} uid`);
const f = this.imap.seq.fetch(`${savedUid}:${currentUid}`, {
// const f = this.imap.seq.fetch(`${savedUid}:${currentUid}`, {
size: true,
envelope: true,
});
f.on("message", (msg, seqno) => {
msg.once("attributes", (attrs) => {
msg.once("attributes", (attrs: Attrs) => {
console.log(attrs.envelope)
mails.push(attrs);
promises.push(saveMessage(attrs, this.id, this.imap));
});
@@ -44,14 +52,15 @@ class Box {
f.once("end", async () => {
let step = 20;
logger.log(promises.length)
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();
const register = new RegisterMessageInApp(res, mails[j], this.id);
await register.save();
resolve("");
})
.catch((err) => {
reject(err);
@@ -61,16 +70,7 @@ class Box {
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);
});
}
}
module.exports = {
Box,
};
}

View File

@@ -1,9 +1,15 @@
const Imap = require("imap");
const { getAllMailboxes, registerMailbox } = require("../../db/imap/imap");
const { logger } = require("../../system/Logger");
const { Box } = require("./Box");
import { Account } from "./ImapSync";
import Imap from "imap";
import { getAllMailboxes, registerMailbox } from "../../db/imap/imap";
import logger from "../../system/Logger";
import Box from "./Box";
export class ImapInstance {
imap: Imap;
account: Account;
boxes: Box[];
class ImapInstance {
constructor(account) {
this.imap = new Imap({
user: account.user,
@@ -68,8 +74,4 @@ class ImapInstance {
if (!allBox.includes("/")) logger.warn("Did not find 'All' mailbox");
return allBox;
}
}
module.exports = {
ImapInstance,
};
}

View File

@@ -1,28 +0,0 @@
const { getAllAccounts } = require("../../db/imap/imap");
const { logger } = require("../../system/Logger");
const { ImapInstance } = require("./ImapInstance");
class ImapSync {
constructor() {
this.instances = [];
}
init() {
getAllAccounts().then((accounts) => {
for (let i = 0; i < accounts.length; i++) {
accounts[i].password = accounts[i]?.password.toString().replace(/[\u{0080}-\u{FFFF}]/gu,"");
this.addInstance(accounts[i]);
}
}).catch((err) => {
logger.error(err);
});
}
addInstance(config) {
this.instances.push(new ImapInstance(config));
}
}
module.exports = {
ImapSync
}

View File

@@ -0,0 +1,33 @@
import { getAllAccounts } from "../../db/imap/imap";
import logger from "../../system/Logger";
import { ImapInstance } from "./ImapInstance";
export interface Account {
id: number;
user: string
password?: string
}
export default class ImapSync {
instances: ImapInstance[]
constructor() {
this.instances = [];
}
init() {
getAllAccounts().then((accounts: Account[]) => {
for (let i = 0; i < accounts.length; i++) {
accounts[i].password = accounts[i]?.password?.toString().replace(/[\u{0080}-\u{FFFF}]/gu,"");
if (accounts[i].id == 2) continue; //debug_todo
this.addInstance(accounts[i]);
}
}).catch((err) => {
logger.error(err);
});
}
addInstance(config) {
this.instances.push(new ImapInstance(config));
}
}