Skip to content

Commit

Permalink
WIP: add new email ckeck
Browse files Browse the repository at this point in the history
  • Loading branch information
Shxuuer committed Jan 30, 2025
1 parent 770d246 commit f729fa0
Show file tree
Hide file tree
Showing 9 changed files with 337 additions and 138 deletions.
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { app } from "electron";
// import { createWindow, createTray } from "./main/ui";
import { createWindow, createTray } from "./main/ui";
import { handleIPC } from "./main/ipc";
import { AccountManager } from "./mail/AccountManager";

Expand All @@ -10,8 +10,8 @@ if (require("electron-squirrel-startup")) {
}

app.whenReady().then(() => {
// createTray();
// createWindow();
createTray();
createWindow();
const accountManager: AccountManager = new AccountManager();
handleIPC(accountManager);
});
Expand Down
38 changes: 22 additions & 16 deletions src/interface.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export interface mail {
addNewMail: (mail: {}) => boolean;
delMail: (mail: string) => boolean;
onMailsUpdate: (callback: (mails: []) => void) => void;
addAccount: (account: Account) => boolean | string;
delAccount: (accountAddr: string) => void;
getHtmlByUid: (accountAddr: string, boxPath: string, uid: string) => string;
onMailsUpdate: (callback: (accountBox: AccountBox[]) => void) => void;
openHowToAdd: () => void;
}

Expand All @@ -12,26 +13,31 @@ declare global {
}

declare global {
interface Message {
subject: string;
date: Date;
from: { name: string; address: string };
to: { name: string; address: string };
source: string;
}
interface Box {
boxName: string;
messages: Message[];
}
interface Account {
type Account = {
imap: string;
imapPort: string;
smtp: string;
smtpPort: string;
mailAddr: string;
password: string;
accessToken?: string;
}
};
type AccountBox = {
accountAddr: string;
boxes: Box[];
};
type Box = {
boxPath: string;
messages: Message[];
};
type Message = {
subject?: string;
date?: Date;
from?: { name: string; address: string };
to?: { name: string; address: string };
source?: string;
uid: string;
};
}

declare global {
Expand Down
53 changes: 46 additions & 7 deletions src/mail/AccountManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ export class AccountManager {
if (!fs.existsSync(this.configPath))
fs.mkdirSync(this.configPath, { recursive: true });
// get all accounts from database and regist them
this.getAccountsListFromDatabase().forEach((accountAddr) => {
this.getAccountsListFromDatabase().forEach(async (accountAddr) => {
const account: Account = this.getAccountFromDatabase(accountAddr);
this.registAccount(account).then((service: MailService) => {});
const service: MailService = await this.registAccount(account);
});
// start checking new mails
this.checkNewMails();
}

/**
Expand All @@ -29,6 +31,15 @@ export class AccountManager {
return this.accounts;
}

public getBoxList(): AccountBox[] {
return this.accounts.map((account) => {
return {
accountAddr: account.mailAddr,
boxes: account.getCache(),
};
});
}

/**
* check if the account is valid
* @param {Account} account account information
Expand All @@ -42,6 +53,7 @@ export class AccountManager {
resolve(true);
})
.catch((err) => {
console.log(err);
reject(err);
});
});
Expand All @@ -55,15 +67,15 @@ export class AccountManager {
public async registAccount(account: Account): Promise<MailService> {
return new Promise((resolve, reject) => {
const index = this.accounts.findIndex(
(acc) => acc.mailAddr === account.mailAddr,
(acc) => acc.mailAddr === account.mailAddr
);
if (index !== -1) return;
const client = new MailService(account);
client
.connect()
.then(() => {
this.accounts.push(client);
updateMailsToRenderer();
updateMailsToRenderer(this);
resolve(client);
})
.catch((err) => {
Expand All @@ -79,12 +91,12 @@ export class AccountManager {
*/
public unregistAccount(accountAddr: string): void {
const index = this.accounts.findIndex(
(account) => account.mailAddr === accountAddr,
(account) => account.mailAddr === accountAddr
);
if (index === -1) return;
this.accounts.splice(index, 1);
this.accounts[index].close();
updateMailsToRenderer();
updateMailsToRenderer(this);
}

/**
Expand Down Expand Up @@ -123,5 +135,32 @@ export class AccountManager {
fs.unlinkSync(path.join(this.configPath, accountAddr));
}

public checkNewMails(): void {}
/**
* check new mails
*/
public async checkNewMails(): Promise<void> {
setInterval(async () => {
console.log(this.accounts.length);
this.accounts.forEach(async (service) => {
const mails = await service.checkNewMails(["INBOX"]);
if (mails.length > 0) updateMailsToRenderer(this);
});
}, 5000);
}

/**
* get mail's html source code
* @param mailAddr mail's mail address
* @param boxPath mail's box path
* @param uid mail's uid
* @returns mail's html source code
*/
public async getMailHTML(
mailAddr: string,
boxPath: string,
uid: string
): Promise<string> {
const account = this.accounts.find((acc) => acc.mailAddr === mailAddr);
return await account.getMailHTML(boxPath, uid);
}
}
Loading

0 comments on commit f729fa0

Please sign in to comment.