Initial implementation for generating thread JSON files
This commit is contained in:
@@ -10,12 +10,12 @@ import {AsyncCollection} from "../bones/collection/AsyncCollection.ts";
|
||||
import {withClient} from "./client.ts";
|
||||
import {buildThreadAddressMatcher} from "../threads/id.ts";
|
||||
|
||||
export async function getMailboxesToSearch(thread?: string): Promise<Collection<string>> {
|
||||
export async function getMailboxesToSearch(thread?: string, client?: ImapFlow): Promise<Collection<string>> {
|
||||
// There are 2 possibilities for where mail might end up.
|
||||
// Either the mail-server is configured to forward the + extensions
|
||||
// to their own folders automatically (e.g. the "c.1234" folder), or
|
||||
// aliased mail just winds up in INBOX.
|
||||
return collect(await withClient(c => c.list()))
|
||||
return collect(await (client?.list() || withClient(c => c.list())))
|
||||
.filter(box =>
|
||||
box.name === 'INBOX' || box.specialUse === '\\\\Inbox'
|
||||
|| (!thread && box.name.startsWith(config.mail.threads.idPrefix))
|
||||
@@ -31,6 +31,7 @@ export async function withMailbox<T>(mailbox: string, cb: (c: AsyncCollection<Me
|
||||
export class MailboxIterable extends Iterable<Message> {
|
||||
private addressMatcher: RegExp
|
||||
private client?: Maybe<ImapFlow>
|
||||
private borrowedClient: boolean = false
|
||||
private query: FetchQueryObject = {
|
||||
envelope: true,
|
||||
source: true,
|
||||
@@ -53,9 +54,14 @@ export class MailboxIterable extends Iterable<Message> {
|
||||
|
||||
constructor(
|
||||
public readonly mailbox: string,
|
||||
client?: ImapFlow,
|
||||
) {
|
||||
super()
|
||||
this.addressMatcher = buildThreadAddressMatcher()
|
||||
if ( client ) {
|
||||
this.client = client
|
||||
this.borrowedClient = true
|
||||
}
|
||||
}
|
||||
|
||||
async at(i: number): Promise<Maybe<Message>> {
|
||||
@@ -104,6 +110,8 @@ export class MailboxIterable extends Iterable<Message> {
|
||||
date: message.envelope.date,
|
||||
from: message.envelope.from[0],
|
||||
subject: message.envelope.subject,
|
||||
mailbox: this.mailbox,
|
||||
modseq: message.modseq,
|
||||
recipients,
|
||||
content,
|
||||
thread,
|
||||
@@ -111,7 +119,7 @@ export class MailboxIterable extends Iterable<Message> {
|
||||
}
|
||||
|
||||
public async release(): Promise<void> {
|
||||
if ( this.client ) {
|
||||
if ( this.client && !this.borrowedClient ) {
|
||||
await this.client.logout()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user