代码拉取完成,页面将自动刷新
import { RegexHelper } from './Helpers/RegexHelper';
import { ResponseParser } from './Helpers/ResponseParser';
import { Message } from './Message';
import { Response } from './Response';
import { Store } from './Store';
export class Folder {
private store: Store;
private name: string;
private openStatus: boolean = false;
public constructor(store: Store, name: string) {
this.store = store;
this.name = name;
}
private toJSON() {
return {
name: this.name,
};
}
// 获取文件夹名称
public getName() {
return this.name;
}
// 获取文件夹内邮件数量(全部/未读/最近)
public getMessageCount() {
return new Promise<{
messages: number;
unseen: number;
recent: number;
}>(async (resolve, reject) => {
if (!this.openStatus) {
await this.open();
}
const response = await this.store.getClient().sendCommand('STATUS INBOX (MESSAGES UNSEEN RECENT)');
if (!response.ok) {
response.msg = 'getMessageCount error';
reject(response);
} else {
const { payload } = response;
const matchRes = RegexHelper.matchAll(payload, /(MESSAGES|UNSEEN|RECENT)\s([0-9]*)/gi);
const countMap = {
messages: 0,
unseen: 0,
recent: 0,
};
matchRes.forEach((match) => {
const type = match[1].toLowerCase() as keyof typeof countMap;
const count = Number(match[2].toLowerCase());
if (Object.keys(countMap).includes(type)) {
countMap[type] = count;
}
});
resolve(countMap);
}
});
}
// 关闭文件夹
public close() {
this.openStatus = false;
}
// 打开文件夹,返回总邮件数
public open() {
return new Promise<number>(async (resolve, reject) => {
const client = this.store.getClient();
const response = await client.sendCommand(`SELECT ${this.name}`);
// 失败
if (!response.ok) {
response.msg = 'folder:open error';
reject(response);
} else {
// 成功
const { payload } = response;
const matchCount = payload.matchAll(/\*\s([0-9]*)\sEXISTS/gi);
const messageCount = matchCount.next().value?.[1] ?? 0;
// 关闭其他的Folder
this.store['folders'].forEach((folder) => {
folder !== this && folder.close();
});
// 自身标记为打开状态
this.openStatus = true;
resolve(messageCount);
}
});
}
// 当前文件夹的打开状态
public isOpen() {
return this.openStatus;
}
// 获取 Message 对象,无范围则返回所有
public getMessages(range?: [number, number]) {
return new Promise<Message[]>(async (resolve, reject) => {
if (range) {
if (range[0] <= 0) range[0] = 1;
if (range[1] < range[0]) range[1] = range[0];
}
const client = this.store.getClient();
const fetchRange = range ? `${range[0]}:${range[1]}` : '1:*';
const response = await client.sendCommand(`UID SEARCH CHARSET UTF-8 ${fetchRange}`);
if (!response.ok) {
response.msg = 'folder:getMessage error';
reject(response.error);
} else {
const regex = /^\*\sSEARCH\s(.*)[\r\n]/gim;
const matches = [...response.payload.matchAll(regex)];
const messages: Message[] = [];
matches.forEach((match) => {
match[1].split(' ').forEach((uid) => {
const message = new Message(this, uid);
messages.push(message);
});
});
resolve(messages);
}
});
}
// uid获取指定的message
public getMessageByUID(uid: string) {
return new Message(this, uid);
}
// index获取指定的message
public getMessageByIndex(index: number) {
return new Promise<Message>(async (resolve, reject) => {
const messages = await this.getMessages([index, index]);
if (messages.length) {
resolve(messages[0]);
} else {
reject(Response(207, 'folder: getMessageByIndex error'));
}
});
}
// 批量更新 messages headers
public updateHeaders(messages: Message[]) {
return new Promise<void>(async (resolve, reject) => {
const client = this.store.getClient();
const messageMap: Record<string, Message> = {};
const uidStr = messages
.map((msg) => {
messageMap[msg.uid] = msg;
return msg.uid;
})
.join(',');
const response = await client.sendCommand(
`UID FETCH ${uidStr} (RFC822.SIZE BODY.PEEK[HEADER.FIELDS (Subject From Date Cc Bcc To Content-Type)])`
);
// 失败
if (!response.ok) {
response.msg = 'Folder: updateHeaders error';
reject(response.error);
} else {
// 成功
const headersArr = ResponseParser.parseFetchHeader(response.payload);
headersArr.forEach((headers) => {
const uid = headers.uid;
if (uid) {
const msg = messageMap[uid];
msg['headers'] = headers;
}
});
resolve();
}
});
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。