Initial implementation for generating thread JSON files
This commit is contained in:
69
src/threads/refresh.ts
Normal file
69
src/threads/refresh.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import {getMailboxesToSearch, MailboxIterable} from "../mail/read.ts";
|
||||
import {withClient} from "../mail/client.ts";
|
||||
import type {Message, ThreadData} from "../types.ts";
|
||||
import {AsyncCollection} from "../bones/collection/AsyncCollection.ts";
|
||||
import {sha256} from "../bones/crypto.ts";
|
||||
import {config} from "../config.ts";
|
||||
|
||||
export async function refreshThreadsEntirely(): Promise<void> {
|
||||
await withClient(async client => {
|
||||
const messagesByThread: {[thread: string]: Message[]} = {}
|
||||
|
||||
const boxes = await getMailboxesToSearch(undefined, client)
|
||||
const now = new Date()
|
||||
|
||||
for ( const box of boxes.all() ) {
|
||||
const iter = new MailboxIterable(box, client)
|
||||
const messages = new AsyncCollection(iter)
|
||||
|
||||
await messages.each(message => {
|
||||
if ( !message.thread || !message.from.address ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( !messagesByThread[message.thread] ) {
|
||||
messagesByThread[message.thread] = []
|
||||
}
|
||||
|
||||
messagesByThread[message.thread].push(message)
|
||||
})
|
||||
}
|
||||
|
||||
for ( const threadId of Object.keys(messagesByThread) ) {
|
||||
const threadData: ThreadData = {
|
||||
thread: threadId,
|
||||
refresh: {
|
||||
date: now,
|
||||
markers: {},
|
||||
},
|
||||
comments: [],
|
||||
}
|
||||
|
||||
const messages = messagesByThread[threadId]
|
||||
for ( const message of messages ) {
|
||||
if (
|
||||
!threadData.refresh.markers[message.mailbox]
|
||||
|| threadData.refresh.markers[message.mailbox] < message.modseq
|
||||
) {
|
||||
threadData.refresh.markers[message.mailbox] = message.modseq
|
||||
}
|
||||
|
||||
threadData.comments.push({
|
||||
user: {
|
||||
name: message.from.name || '(anonymous)',
|
||||
mailId: sha256(message.from.address!),
|
||||
domainId: sha256(message.from.address!.split('@').reverse()[0]),
|
||||
},
|
||||
date: message.date,
|
||||
subject: message.subject,
|
||||
text: message.content,
|
||||
})
|
||||
}
|
||||
|
||||
const json = JSON.stringify(
|
||||
threadData,
|
||||
(_, v) => typeof v === 'bigint' ? `${v}` : v)
|
||||
await Bun.write(`${config.dirs.data}/threads/${threadId}.json`, json)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user