inboxer 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +376 -2
- package/dist/app.d.ts +6 -0
- package/dist/app.js +56 -0
- package/dist/auth.d.ts +33 -0
- package/dist/auth.js +111 -0
- package/dist/config.d.ts +23 -0
- package/dist/config.js +90 -0
- package/dist/core/address.d.ts +6 -0
- package/dist/core/address.js +16 -0
- package/dist/core/attachments.d.ts +9 -0
- package/dist/core/attachments.js +30 -0
- package/dist/core/context.d.ts +10 -0
- package/dist/core/context.js +21 -0
- package/dist/core/mail.d.ts +170 -0
- package/dist/core/mail.js +656 -0
- package/dist/core/sync.d.ts +16 -0
- package/dist/core/sync.js +44 -0
- package/dist/core/text.d.ts +8 -0
- package/dist/core/text.js +45 -0
- package/dist/core/threads.d.ts +21 -0
- package/dist/core/threads.js +81 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +51 -0
- package/dist/mcp.d.ts +5 -0
- package/dist/mcp.js +198 -0
- package/dist/provider/index.d.ts +3 -0
- package/dist/provider/index.js +11 -0
- package/dist/provider/resend.d.ts +8 -0
- package/dist/provider/resend.js +184 -0
- package/dist/provider/types.d.ts +77 -0
- package/dist/provider/types.js +6 -0
- package/dist/store.d.ts +54 -0
- package/dist/store.js +148 -0
- package/dist/types.d.ts +138 -0
- package/dist/types.js +1 -0
- package/dist/ui/assets.d.ts +8 -0
- package/dist/ui/assets.js +13 -0
- package/dist/ui/routes.d.ts +11 -0
- package/dist/ui/routes.js +232 -0
- package/dist/ui/static/app.js +27 -0
- package/dist/ui/static/htmx.min.js +1 -0
- package/dist/ui/static/style.css +179 -0
- package/dist/ui/strings.d.ts +67 -0
- package/dist/ui/strings.js +67 -0
- package/dist/ui/views.d.ts +45 -0
- package/dist/ui/views.js +275 -0
- package/package.json +81 -4
- package/skills/inboxer/SKILL.md +118 -0
- package/src/app.ts +56 -0
- package/src/auth.ts +119 -0
- package/src/config.ts +92 -0
- package/src/core/address.ts +19 -0
- package/src/core/attachments.ts +33 -0
- package/src/core/context.ts +28 -0
- package/src/core/mail.ts +737 -0
- package/src/core/sync.ts +51 -0
- package/src/core/text.ts +52 -0
- package/src/core/threads.ts +102 -0
- package/src/index.ts +93 -0
- package/src/mcp.ts +233 -0
- package/src/provider/index.ts +14 -0
- package/src/provider/resend.ts +235 -0
- package/src/provider/types.ts +82 -0
- package/src/store.ts +187 -0
- package/src/types.ts +140 -0
- package/src/ui/assets.ts +21 -0
- package/src/ui/routes.ts +252 -0
- package/src/ui/static/app.js +27 -0
- package/src/ui/static/htmx.min.js +1 -0
- package/src/ui/static/style.css +179 -0
- package/src/ui/strings.ts +73 -0
- package/src/ui/views.ts +315 -0
package/src/core/mail.ts
ADDED
|
@@ -0,0 +1,737 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type { DeliveryStatus, MailProvider, ProviderEvent } from "../provider/types.ts";
|
|
3
|
+
import { reaches } from "../config.ts";
|
|
4
|
+
import { NotFoundError, nid, type Store } from "../store.ts";
|
|
5
|
+
import { CATCHALL, type Actor, type Contact, type Context, type AttachmentMeta, type InboxerConfig, type MailboxConfig, type Message, type MetaValue, type PendingAction, type Scope, type Thread } from "../types.ts";
|
|
6
|
+
import { AttachmentFiles } from "./attachments.ts";
|
|
7
|
+
import { bareAddress, domainOf, formatAddress } from "./address.ts";
|
|
8
|
+
import { mergeContext, metaMatches, type ContextPatch } from "./context.ts";
|
|
9
|
+
import { htmlToText, replySubject, textToHtml, tokenize } from "./text.ts";
|
|
10
|
+
import { messageTime, participantsOf, refreshThread, resolveThreadId, threadMessages } from "./threads.ts";
|
|
11
|
+
|
|
12
|
+
const STATUS_RANK: Record<DeliveryStatus, number> = { sent: 1, delivered: 2, bounced: 3, failed: 3 };
|
|
13
|
+
|
|
14
|
+
export class ConflictError extends Error {
|
|
15
|
+
readonly current: Message;
|
|
16
|
+
constructor(current: Message) {
|
|
17
|
+
super(`Draft ${current.id} changed (current version ${current.version})`);
|
|
18
|
+
this.name = "ConflictError";
|
|
19
|
+
this.current = current;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class InvalidRequestError extends Error {
|
|
24
|
+
constructor(message: string) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = "InvalidRequestError";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type ThreadStatus = "unanswered" | "all" | "archived";
|
|
31
|
+
|
|
32
|
+
export interface ListThreadsInput {
|
|
33
|
+
mailbox?: string;
|
|
34
|
+
status?: ThreadStatus;
|
|
35
|
+
label?: string;
|
|
36
|
+
since?: string;
|
|
37
|
+
/** Threads whose context, or a participant's contact, has every pair. */
|
|
38
|
+
meta?: Record<string, MetaValue>;
|
|
39
|
+
limit?: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface SearchInput {
|
|
43
|
+
query?: string;
|
|
44
|
+
meta?: Record<string, MetaValue>;
|
|
45
|
+
mailbox?: string;
|
|
46
|
+
from?: string;
|
|
47
|
+
since?: string;
|
|
48
|
+
until?: string;
|
|
49
|
+
limit?: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface CreateDraftInput {
|
|
53
|
+
mailbox?: string;
|
|
54
|
+
replyToId?: string;
|
|
55
|
+
replyAll?: boolean;
|
|
56
|
+
to?: string[];
|
|
57
|
+
cc?: string[];
|
|
58
|
+
bcc?: string[];
|
|
59
|
+
subject?: string;
|
|
60
|
+
text: string;
|
|
61
|
+
html?: string;
|
|
62
|
+
note?: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface DraftPatch {
|
|
66
|
+
to?: string[];
|
|
67
|
+
cc?: string[];
|
|
68
|
+
bcc?: string[];
|
|
69
|
+
subject?: string;
|
|
70
|
+
text?: string;
|
|
71
|
+
html?: string | null;
|
|
72
|
+
note?: string | null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface MessageView {
|
|
76
|
+
id: string;
|
|
77
|
+
thread_id: string;
|
|
78
|
+
mailbox: string;
|
|
79
|
+
direction: Message["direction"];
|
|
80
|
+
status: Message["status"];
|
|
81
|
+
from: string;
|
|
82
|
+
to: string[];
|
|
83
|
+
cc: string[];
|
|
84
|
+
subject: string;
|
|
85
|
+
date: string;
|
|
86
|
+
text: string;
|
|
87
|
+
labels: string[];
|
|
88
|
+
read: boolean;
|
|
89
|
+
attachments: Message["attachments"];
|
|
90
|
+
note: string | null;
|
|
91
|
+
version: number;
|
|
92
|
+
edited_by_human: boolean;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The mail domain: everything the MCP tools, the UI and the webhook do goes through here. */
|
|
96
|
+
export class Mail {
|
|
97
|
+
readonly store: Store;
|
|
98
|
+
readonly provider: MailProvider;
|
|
99
|
+
readonly cfg: InboxerConfig;
|
|
100
|
+
private readonly ownAddresses: Set<string>;
|
|
101
|
+
private readonly timers = new Map<string, ReturnType<typeof setTimeout>>();
|
|
102
|
+
private readonly files: AttachmentFiles;
|
|
103
|
+
|
|
104
|
+
constructor(store: Store, provider: MailProvider, cfg: InboxerConfig) {
|
|
105
|
+
this.store = store;
|
|
106
|
+
this.provider = provider;
|
|
107
|
+
this.cfg = cfg;
|
|
108
|
+
this.ownAddresses = new Set(Object.values(cfg.mailboxes).map(m => bareAddress(m.address)));
|
|
109
|
+
this.files = new AttachmentFiles(cfg.dataDir);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
listMailboxes(scope: Scope): { id: string; address: string; name: string }[] {
|
|
113
|
+
return [
|
|
114
|
+
...Object.entries(this.cfg.mailboxes).map(([id, m]) => ({ id, address: m.address, name: m.name })),
|
|
115
|
+
{ id: CATCHALL, address: "*", name: "Catch-all" },
|
|
116
|
+
].filter(m => reaches(scope, m.id));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Out of scope reads as missing, so a scoped token cannot probe other mailboxes.
|
|
120
|
+
|
|
121
|
+
async requireMessage(id: string, scope: Scope): Promise<Message> {
|
|
122
|
+
const message = await this.store.requireMessage(id);
|
|
123
|
+
if (!reaches(scope, message.mailbox)) throw new NotFoundError(`Message ${id} not found`);
|
|
124
|
+
return message;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async requireThread(id: string, scope: Scope): Promise<Thread> {
|
|
128
|
+
const thread = await this.store.requireThread(id);
|
|
129
|
+
if (!reaches(scope, thread.mailbox)) throw new NotFoundError(`Thread ${id} not found`);
|
|
130
|
+
return thread;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Scoped actors only reach contacts that take part in one of their threads. */
|
|
134
|
+
private async requireContactAccess(address: string, scope: Scope): Promise<void> {
|
|
135
|
+
if (scope === null) return;
|
|
136
|
+
const threads = await this.store.allThreads();
|
|
137
|
+
if (!threads.some(t => reaches(scope, t.mailbox) && t.participants.includes(address))) {
|
|
138
|
+
throw new NotFoundError(`Contact ${address} not found`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private mailboxFor(addresses: string[]): string {
|
|
143
|
+
const bare = addresses.map(bareAddress);
|
|
144
|
+
for (const [id, mailbox] of Object.entries(this.cfg.mailboxes)) {
|
|
145
|
+
if (bare.includes(bareAddress(mailbox.address))) return id;
|
|
146
|
+
}
|
|
147
|
+
return CATCHALL;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private sendingMailbox(id: string): MailboxConfig {
|
|
151
|
+
const mailbox = this.cfg.mailboxes[id];
|
|
152
|
+
if (!mailbox) throw new InvalidRequestError(`Mailbox "${id}" cannot send (configured: ${Object.keys(this.cfg.mailboxes).join(", ")})`);
|
|
153
|
+
return mailbox;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Inbound
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Stores an inbound message once per provider id and returns its id. Attachments are copied to
|
|
160
|
+
* disk right away; one that fails to download is fetched again the first time it is opened.
|
|
161
|
+
*/
|
|
162
|
+
async ingest(providerId: string): Promise<string> {
|
|
163
|
+
const known = await this.store.lookupProviderId(this.provider.name, providerId);
|
|
164
|
+
if (known) return known;
|
|
165
|
+
const inbound = await this.provider.getInbound(providerId);
|
|
166
|
+
const id = await this.storeInbound(providerId, inbound);
|
|
167
|
+
const message = await this.store.requireMessage(id);
|
|
168
|
+
for (const meta of message.attachments) {
|
|
169
|
+
await this.attachmentBody(message, meta).catch(err =>
|
|
170
|
+
console.error(`inboxer: attachment ${meta.id} of message ${id} not stored`, err));
|
|
171
|
+
}
|
|
172
|
+
return id;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private storeInbound(providerId: string, inbound: Awaited<ReturnType<MailProvider["getInbound"]>>): Promise<string> {
|
|
176
|
+
return this.store.serialize(async () => {
|
|
177
|
+
const existing = await this.store.lookupProviderId(this.provider.name, providerId);
|
|
178
|
+
if (existing) return existing;
|
|
179
|
+
|
|
180
|
+
const mailbox = this.mailboxFor([...inbound.deliveredTo, ...inbound.to, ...inbound.cc]);
|
|
181
|
+
const participants = participantsOf(inbound);
|
|
182
|
+
const threadId = await resolveThreadId(
|
|
183
|
+
this.store,
|
|
184
|
+
{
|
|
185
|
+
mailbox,
|
|
186
|
+
inReplyTo: inbound.inReplyTo,
|
|
187
|
+
references: inbound.references,
|
|
188
|
+
subject: inbound.subject,
|
|
189
|
+
participants,
|
|
190
|
+
at: inbound.receivedAt,
|
|
191
|
+
},
|
|
192
|
+
this.ownAddresses,
|
|
193
|
+
this.cfg.threadWindowDays,
|
|
194
|
+
);
|
|
195
|
+
const message: Message = {
|
|
196
|
+
id: nid(),
|
|
197
|
+
provider: this.provider.name,
|
|
198
|
+
provider_id: inbound.providerId,
|
|
199
|
+
mailbox,
|
|
200
|
+
direction: "in",
|
|
201
|
+
status: "received",
|
|
202
|
+
message_id: inbound.messageId,
|
|
203
|
+
in_reply_to: inbound.inReplyTo,
|
|
204
|
+
references: inbound.references,
|
|
205
|
+
thread_id: threadId,
|
|
206
|
+
from: inbound.from,
|
|
207
|
+
to: inbound.to,
|
|
208
|
+
cc: inbound.cc,
|
|
209
|
+
bcc: inbound.bcc,
|
|
210
|
+
reply_to: inbound.replyTo,
|
|
211
|
+
subject: inbound.subject,
|
|
212
|
+
text: inbound.text ?? (inbound.html ? htmlToText(inbound.html) : ""),
|
|
213
|
+
html: inbound.html,
|
|
214
|
+
headers: inbound.headers,
|
|
215
|
+
attachments: inbound.attachments,
|
|
216
|
+
labels: {},
|
|
217
|
+
note: null,
|
|
218
|
+
created_by: null,
|
|
219
|
+
read: false,
|
|
220
|
+
archived: false,
|
|
221
|
+
created_at: inbound.receivedAt,
|
|
222
|
+
sent_at: null,
|
|
223
|
+
version: 1,
|
|
224
|
+
edited_by_human: false,
|
|
225
|
+
pending: null,
|
|
226
|
+
};
|
|
227
|
+
await this.store.putMessage(message);
|
|
228
|
+
await this.store.indexProviderId(this.provider.name, inbound.providerId, message.id);
|
|
229
|
+
if (message.message_id) await this.store.indexMessageId(message.message_id, message.id);
|
|
230
|
+
await refreshThread(this.store, threadId, message.id);
|
|
231
|
+
return message.id;
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async applyEvent(event: ProviderEvent): Promise<void> {
|
|
236
|
+
if (event.type === "received") {
|
|
237
|
+
await this.ingest(event.providerId);
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
await this.applyStatus(event.providerId, event.status, event.messageId);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Status updates never move backwards: a late `sent` does not overwrite `delivered`. */
|
|
244
|
+
applyStatus(providerId: string, status: DeliveryStatus, messageId: string | null): Promise<void> {
|
|
245
|
+
return this.store.serialize(async () => {
|
|
246
|
+
const id = await this.store.lookupProviderId(this.provider.name, providerId);
|
|
247
|
+
if (!id) return;
|
|
248
|
+
const message = await this.store.requireMessage(id);
|
|
249
|
+
if (message.direction !== "out") return;
|
|
250
|
+
const current = STATUS_RANK[message.status as DeliveryStatus] ?? 0;
|
|
251
|
+
if (STATUS_RANK[status] > current) message.status = status;
|
|
252
|
+
if (messageId && messageId !== message.message_id) {
|
|
253
|
+
message.message_id = messageId;
|
|
254
|
+
await this.store.indexMessageId(messageId, id);
|
|
255
|
+
}
|
|
256
|
+
await this.store.putMessage(message);
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Queries
|
|
261
|
+
|
|
262
|
+
/** Matches a thread by its own context or by any participant's contact. */
|
|
263
|
+
private async metaFilter(wanted: Record<string, MetaValue> | undefined) {
|
|
264
|
+
if (!wanted || !Object.keys(wanted).length) return null;
|
|
265
|
+
const threadContexts = await this.store.allThreadContexts();
|
|
266
|
+
const contacts = new Map((await this.store.allContacts()).map(c => [c.address, c]));
|
|
267
|
+
return (threadId: string, participants: string[]) =>
|
|
268
|
+
metaMatches(threadContexts[threadId]?.meta, wanted) ||
|
|
269
|
+
participants.some(p => metaMatches(contacts.get(p)?.meta, wanted));
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
async listThreads(input: ListThreadsInput, scope: Scope): Promise<Thread[]> {
|
|
273
|
+
const status = input.status ?? "unanswered";
|
|
274
|
+
const byMeta = await this.metaFilter(input.meta);
|
|
275
|
+
const threads = (await this.store.allThreads()).filter(t => {
|
|
276
|
+
if (!reaches(scope, t.mailbox)) return false;
|
|
277
|
+
if (byMeta && !byMeta(t.id, t.participants)) return false;
|
|
278
|
+
if (input.mailbox && t.mailbox !== input.mailbox) return false;
|
|
279
|
+
if (input.label && !t.labels[input.label]) return false;
|
|
280
|
+
if (input.since && t.last_at < input.since) return false;
|
|
281
|
+
if (status === "archived") return t.archived;
|
|
282
|
+
if (t.archived) return false;
|
|
283
|
+
return status === "all" || (t.last_direction === "in" && !t.has_draft);
|
|
284
|
+
});
|
|
285
|
+
threads.sort((a, b) => b.last_at.localeCompare(a.last_at));
|
|
286
|
+
return threads.slice(0, input.limit ?? 50);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
async search(input: SearchInput, scope: Scope): Promise<MessageView[]> {
|
|
290
|
+
const terms = tokenize(input.query ?? "");
|
|
291
|
+
const byMeta = await this.metaFilter(input.meta);
|
|
292
|
+
if (!terms.length && !byMeta) throw new InvalidRequestError("pass a query with searchable terms, meta, or both");
|
|
293
|
+
const from = input.from?.toLowerCase();
|
|
294
|
+
const hits = (await this.store.allMessages()).filter(m => {
|
|
295
|
+
if (m.status === "discarded" || !reaches(scope, m.mailbox)) return false;
|
|
296
|
+
if (byMeta && !byMeta(m.thread_id, participantsOf(m))) return false;
|
|
297
|
+
if (input.mailbox && m.mailbox !== input.mailbox) return false;
|
|
298
|
+
if (from && !bareAddress(m.from).includes(from)) return false;
|
|
299
|
+
const at = messageTime(m);
|
|
300
|
+
if (input.since && at < input.since) return false;
|
|
301
|
+
if (input.until && at > input.until) return false;
|
|
302
|
+
const haystack = tokenize([m.subject, m.text, m.from, ...m.to].join(" ")).join(" ");
|
|
303
|
+
return terms.every(term => haystack.includes(term));
|
|
304
|
+
});
|
|
305
|
+
hits.sort((a, b) => messageTime(b).localeCompare(messageTime(a)));
|
|
306
|
+
return hits.slice(0, input.limit ?? 50).map(toView);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
async getThread(threadId: string, scope: Scope): Promise<{ thread: Thread; context: Context | null; contacts: Contact[]; messages: MessageView[] }> {
|
|
310
|
+
const thread = await this.requireThread(threadId, scope);
|
|
311
|
+
return { thread, ...(await this.contextOf(thread)), messages: (await threadMessages(this.store, thread)).map(toView) };
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** The thread's own context and the contacts of its external participants that have one. */
|
|
315
|
+
async contextOf(thread: Thread): Promise<{ context: Context | null; contacts: Contact[] }> {
|
|
316
|
+
const external = thread.participants.filter(p => !this.ownAddresses.has(p));
|
|
317
|
+
const contacts = (await Promise.all(external.map(p => this.store.getContact(p)))).filter((c): c is Contact => !!c);
|
|
318
|
+
return { context: await this.store.getThreadContext(thread.id), contacts };
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Context
|
|
322
|
+
|
|
323
|
+
async getContact(address: string, scope: Scope): Promise<Contact | null> {
|
|
324
|
+
const bare = bareAddress(address);
|
|
325
|
+
await this.requireContactAccess(bare, scope);
|
|
326
|
+
return this.store.getContact(bare);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
async setContact(address: string, patch: ContextPatch, actor: Actor): Promise<Contact> {
|
|
330
|
+
const bare = bareAddress(address);
|
|
331
|
+
if (!bare.includes("@")) throw new InvalidRequestError(`"${address}" is not an email address`);
|
|
332
|
+
await this.requireContactAccess(bare, actor.scope);
|
|
333
|
+
return this.store.serialize(async () => {
|
|
334
|
+
const contact: Contact = { address: bare, ...this.merge(await this.store.getContact(bare), patch, actor) };
|
|
335
|
+
await this.store.putContact(contact);
|
|
336
|
+
await this.store.audit("contact.update", actor, bare, { ...patch });
|
|
337
|
+
return contact;
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
setThreadContext(threadId: string, patch: ContextPatch, actor: Actor): Promise<Context> {
|
|
342
|
+
return this.store.serialize(async () => {
|
|
343
|
+
await this.requireThread(threadId, actor.scope);
|
|
344
|
+
const context = this.merge(await this.store.getThreadContext(threadId), patch, actor);
|
|
345
|
+
await this.store.putThreadContext(threadId, context);
|
|
346
|
+
await this.store.audit("thread.context", actor, threadId, { ...patch });
|
|
347
|
+
return context;
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
private merge(existing: Context | null, patch: ContextPatch, actor: Actor): Context {
|
|
352
|
+
if (Object.keys(patch.meta ?? {}).some(key => !key.trim())) throw new InvalidRequestError("meta keys must be non-empty");
|
|
353
|
+
return mergeContext(existing, patch, actor);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
async listDrafts(mailbox: string | undefined, scope: Scope): Promise<Message[]> {
|
|
357
|
+
const drafts = await Promise.all((await this.store.draftIds()).map(id => this.store.requireMessage(id)));
|
|
358
|
+
return drafts
|
|
359
|
+
.filter(d => reaches(scope, d.mailbox) && (!mailbox || d.mailbox === mailbox))
|
|
360
|
+
.sort((a, b) => b.created_at.localeCompare(a.created_at));
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// Organizing
|
|
364
|
+
|
|
365
|
+
/** Message ids, or thread ids to apply the change to every message in the thread. */
|
|
366
|
+
private async resolveMessageIds(ids: string[], scope: Scope): Promise<string[]> {
|
|
367
|
+
const out: string[] = [];
|
|
368
|
+
for (const id of ids) {
|
|
369
|
+
const thread = await this.store.getThread(id);
|
|
370
|
+
if (thread && reaches(scope, thread.mailbox)) out.push(...thread.message_ids);
|
|
371
|
+
else out.push((await this.requireMessage(id, scope)).id);
|
|
372
|
+
}
|
|
373
|
+
return [...new Set(out)];
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
private updateMessages(ids: string[], actor: Actor, action: string, change: (m: Message) => void): Promise<number> {
|
|
377
|
+
return this.store.serialize(async () => {
|
|
378
|
+
const messageIds = await this.resolveMessageIds(ids, actor.scope);
|
|
379
|
+
const threads = new Set<string>();
|
|
380
|
+
for (const id of messageIds) {
|
|
381
|
+
const message = await this.store.requireMessage(id);
|
|
382
|
+
change(message);
|
|
383
|
+
await this.store.putMessage(message);
|
|
384
|
+
threads.add(message.thread_id);
|
|
385
|
+
}
|
|
386
|
+
for (const threadId of threads) await refreshThread(this.store, threadId);
|
|
387
|
+
await this.store.audit(action, actor, ids.join(","));
|
|
388
|
+
return messageIds.length;
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
label(ids: string[], add: string[], remove: string[], actor: Actor): Promise<number> {
|
|
393
|
+
return this.updateMessages(ids, actor, "label", m => {
|
|
394
|
+
for (const label of add) m.labels[label] = true;
|
|
395
|
+
for (const label of remove) delete m.labels[label];
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
mark(ids: string[], flags: { read?: boolean; archived?: boolean }, actor: Actor): Promise<number> {
|
|
400
|
+
return this.updateMessages(ids, actor, "mark", m => {
|
|
401
|
+
if (flags.read !== undefined) m.read = flags.read;
|
|
402
|
+
if (flags.archived !== undefined) m.archived = flags.archived;
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// Drafts and sending
|
|
407
|
+
|
|
408
|
+
createDraft(input: CreateDraftInput, actor: Actor): Promise<Message> {
|
|
409
|
+
return this.store.serialize(() => this.createDraftUnlocked(input, actor));
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
private async createDraftUnlocked(input: CreateDraftInput, actor: Actor): Promise<Message> {
|
|
413
|
+
const now = new Date().toISOString();
|
|
414
|
+
let mailbox: string;
|
|
415
|
+
let to: string[];
|
|
416
|
+
let cc = input.cc ?? [];
|
|
417
|
+
let subject: string;
|
|
418
|
+
let inReplyTo: string | null = null;
|
|
419
|
+
let references: string[] = [];
|
|
420
|
+
let threadId: string;
|
|
421
|
+
|
|
422
|
+
if (input.replyToId) {
|
|
423
|
+
const original = await this.requireMessage(input.replyToId, actor.scope);
|
|
424
|
+
mailbox = input.mailbox ?? original.mailbox;
|
|
425
|
+
const own = bareAddress(this.sendingMailbox(mailbox).address);
|
|
426
|
+
to = input.to ?? (original.direction === "in" ? (original.reply_to.length ? original.reply_to : [original.from]) : original.to);
|
|
427
|
+
if (input.replyAll && !input.cc) {
|
|
428
|
+
const already = new Set([own, ...to.map(bareAddress)]);
|
|
429
|
+
cc = [...original.to, ...original.cc].filter(a => !already.has(bareAddress(a)));
|
|
430
|
+
}
|
|
431
|
+
subject = input.subject ?? replySubject(original.subject);
|
|
432
|
+
inReplyTo = original.message_id;
|
|
433
|
+
references = original.message_id ? [...original.references, original.message_id] : original.references;
|
|
434
|
+
threadId = original.thread_id;
|
|
435
|
+
} else {
|
|
436
|
+
if (!input.mailbox) throw new InvalidRequestError("mailbox is required when not replying");
|
|
437
|
+
if (!input.to?.length) throw new InvalidRequestError("to is required when not replying");
|
|
438
|
+
if (input.subject === undefined) throw new InvalidRequestError("subject is required when not replying");
|
|
439
|
+
mailbox = input.mailbox;
|
|
440
|
+
to = input.to;
|
|
441
|
+
subject = input.subject;
|
|
442
|
+
threadId = nid();
|
|
443
|
+
}
|
|
444
|
+
const sender = this.sendingMailbox(mailbox);
|
|
445
|
+
if (!reaches(actor.scope, mailbox)) throw new InvalidRequestError(`Mailbox "${mailbox}" is not available to ${actor.name}`);
|
|
446
|
+
|
|
447
|
+
const draft: Message = {
|
|
448
|
+
id: nid(),
|
|
449
|
+
provider: this.provider.name,
|
|
450
|
+
provider_id: null,
|
|
451
|
+
mailbox,
|
|
452
|
+
direction: "out",
|
|
453
|
+
status: "draft",
|
|
454
|
+
message_id: null,
|
|
455
|
+
in_reply_to: inReplyTo,
|
|
456
|
+
references,
|
|
457
|
+
thread_id: threadId,
|
|
458
|
+
from: formatAddress(sender.name, sender.address),
|
|
459
|
+
to,
|
|
460
|
+
cc,
|
|
461
|
+
bcc: input.bcc ?? [],
|
|
462
|
+
reply_to: [],
|
|
463
|
+
subject,
|
|
464
|
+
text: input.text,
|
|
465
|
+
html: input.html ?? null,
|
|
466
|
+
headers: {},
|
|
467
|
+
attachments: [],
|
|
468
|
+
labels: {},
|
|
469
|
+
note: input.note ?? null,
|
|
470
|
+
created_by: actor.kind === "ui" ? "ui" : "agent",
|
|
471
|
+
read: true,
|
|
472
|
+
archived: false,
|
|
473
|
+
created_at: now,
|
|
474
|
+
sent_at: null,
|
|
475
|
+
version: 1,
|
|
476
|
+
edited_by_human: actor.kind === "ui",
|
|
477
|
+
pending: null,
|
|
478
|
+
};
|
|
479
|
+
await this.store.putMessage(draft);
|
|
480
|
+
await this.store.setDraft(draft.id, true);
|
|
481
|
+
await refreshThread(this.store, threadId, draft.id);
|
|
482
|
+
await this.store.audit("draft.create", actor, draft.id);
|
|
483
|
+
return draft;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
private async requireDraft(id: string, scope: Scope): Promise<Message> {
|
|
487
|
+
const draft = await this.requireMessage(id, scope);
|
|
488
|
+
if (draft.status !== "draft") throw new InvalidRequestError(`Message ${id} is not a pending draft (status: ${draft.status})`);
|
|
489
|
+
return draft;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Optimistic concurrency: `version` must be the current one. An agent cannot overwrite a draft a
|
|
494
|
+
* human edited unless it passes `force`. A UI edit regenerates the HTML from the text at send time.
|
|
495
|
+
*/
|
|
496
|
+
updateDraft(id: string, version: number, patch: DraftPatch, actor: Actor, force = false): Promise<Message> {
|
|
497
|
+
return this.store.serialize(async () => {
|
|
498
|
+
const draft = await this.requireDraft(id, actor.scope);
|
|
499
|
+
if (draft.version !== version) throw new ConflictError(draft);
|
|
500
|
+
if (draft.pending) throw new InvalidRequestError(`Draft ${id} is about to ${draft.pending.action}; undo it first`);
|
|
501
|
+
if (actor.kind !== "ui" && draft.edited_by_human && !force) {
|
|
502
|
+
throw new InvalidRequestError(`Draft ${id} was edited by a human; pass force to overwrite it`);
|
|
503
|
+
}
|
|
504
|
+
const changed = Object.keys(patch).filter(k => patch[k as keyof DraftPatch] !== undefined);
|
|
505
|
+
Object.assign(draft, Object.fromEntries(changed.map(k => [k, patch[k as keyof DraftPatch]])));
|
|
506
|
+
if (actor.kind === "ui") {
|
|
507
|
+
draft.edited_by_human = true;
|
|
508
|
+
if (patch.text !== undefined && patch.html === undefined) draft.html = null;
|
|
509
|
+
}
|
|
510
|
+
draft.version += 1;
|
|
511
|
+
await this.store.putMessage(draft);
|
|
512
|
+
await refreshThread(this.store, draft.thread_id);
|
|
513
|
+
await this.store.audit("draft.update", actor, id, { fields: changed, version: draft.version });
|
|
514
|
+
return draft;
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
discardDraft(id: string, actor: Actor): Promise<void> {
|
|
519
|
+
return this.store.serialize(() => this.discardDraftUnlocked(id, actor));
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
private async discardDraftUnlocked(id: string, actor: Actor): Promise<void> {
|
|
523
|
+
const draft = await this.requireDraft(id, actor.scope);
|
|
524
|
+
draft.status = "discarded";
|
|
525
|
+
draft.pending = null;
|
|
526
|
+
await this.store.putMessage(draft);
|
|
527
|
+
await this.store.setDraft(id, false);
|
|
528
|
+
await refreshThread(this.store, draft.thread_id);
|
|
529
|
+
await this.store.audit("draft.discard", actor, id);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/** Attaching does not change `version`: it never conflicts with text edits in flight. */
|
|
533
|
+
attach(id: string, file: { filename: string; contentType: string; body: Uint8Array }, actor: Actor): Promise<Message> {
|
|
534
|
+
return this.store.serialize(async () => {
|
|
535
|
+
const draft = await this.requireDraft(id, actor.scope);
|
|
536
|
+
if (draft.pending) throw new InvalidRequestError(`Draft ${id} is about to ${draft.pending.action}; undo it first`);
|
|
537
|
+
const meta: AttachmentMeta = {
|
|
538
|
+
id: nid(),
|
|
539
|
+
filename: file.filename,
|
|
540
|
+
content_type: file.contentType || "application/octet-stream",
|
|
541
|
+
size: file.body.byteLength,
|
|
542
|
+
inline: false,
|
|
543
|
+
content_id: null,
|
|
544
|
+
};
|
|
545
|
+
await this.files.write(draft.id, meta.id, file.body);
|
|
546
|
+
draft.attachments.push(meta);
|
|
547
|
+
await this.store.putMessage(draft);
|
|
548
|
+
await this.store.audit("draft.attach", actor, id, { filename: meta.filename, size: meta.size });
|
|
549
|
+
return draft;
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
detach(id: string, attachmentId: string, actor: Actor): Promise<Message> {
|
|
554
|
+
return this.store.serialize(async () => {
|
|
555
|
+
const draft = await this.requireDraft(id, actor.scope);
|
|
556
|
+
if (draft.pending) throw new InvalidRequestError(`Draft ${id} is about to ${draft.pending.action}; undo it first`);
|
|
557
|
+
const index = draft.attachments.findIndex(a => a.id === attachmentId);
|
|
558
|
+
if (index < 0) throw new NotFoundError(`Attachment ${attachmentId} not found in draft ${id}`);
|
|
559
|
+
const [removed] = draft.attachments.splice(index, 1);
|
|
560
|
+
await this.store.putMessage(draft);
|
|
561
|
+
await this.store.audit("draft.detach", actor, id, { filename: removed!.filename });
|
|
562
|
+
return draft;
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
// Undo window: a human's send or discard waits `ui.undoSec` and survives a restart.
|
|
567
|
+
|
|
568
|
+
/** Starts the undo window for a send or discard. Calling it again keeps the first one. */
|
|
569
|
+
schedule(id: string, action: PendingAction["action"], actor: Actor): Promise<Message> {
|
|
570
|
+
return this.store.serialize(async () => {
|
|
571
|
+
const draft = await this.requireDraft(id, actor.scope);
|
|
572
|
+
if (!draft.pending) {
|
|
573
|
+
draft.pending = { action, at: new Date(Date.now() + this.cfg.ui.undoSec * 1000).toISOString(), by: actor.name };
|
|
574
|
+
await this.store.putMessage(draft);
|
|
575
|
+
await this.store.audit("draft.schedule", actor, id, { action });
|
|
576
|
+
}
|
|
577
|
+
this.arm(draft);
|
|
578
|
+
return draft;
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
undo(id: string, actor: Actor): Promise<Message> {
|
|
583
|
+
return this.store.serialize(async () => {
|
|
584
|
+
const draft = await this.requireDraft(id, actor.scope);
|
|
585
|
+
clearTimeout(this.timers.get(id));
|
|
586
|
+
this.timers.delete(id);
|
|
587
|
+
if (draft.pending) {
|
|
588
|
+
const { action } = draft.pending;
|
|
589
|
+
draft.pending = null;
|
|
590
|
+
await this.store.putMessage(draft);
|
|
591
|
+
await this.store.audit("draft.undo", actor, id, { action });
|
|
592
|
+
}
|
|
593
|
+
return draft;
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/** Re-arms undo windows left by a previous process; overdue ones run now. */
|
|
598
|
+
async resumePending(): Promise<void> {
|
|
599
|
+
for (const id of await this.store.draftIds()) {
|
|
600
|
+
const draft = await this.store.requireMessage(id);
|
|
601
|
+
if (draft.pending) this.arm(draft);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
stopPending(): void {
|
|
606
|
+
for (const timer of this.timers.values()) clearTimeout(timer);
|
|
607
|
+
this.timers.clear();
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
private arm(draft: Message): void {
|
|
611
|
+
clearTimeout(this.timers.get(draft.id));
|
|
612
|
+
const delay = Math.max(0, Date.parse(draft.pending!.at) - Date.now());
|
|
613
|
+
this.timers.set(draft.id, setTimeout(() => {
|
|
614
|
+
this.runPending(draft.id).catch(err => console.error(`inboxer: pending action on draft ${draft.id} failed`, err));
|
|
615
|
+
}, delay));
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/** A failed send leaves a plain pending draft, so the reviewer sees it was not sent. */
|
|
619
|
+
private runPending(id: string): Promise<void> {
|
|
620
|
+
this.timers.delete(id);
|
|
621
|
+
return this.store.serialize(async () => {
|
|
622
|
+
const draft = await this.store.getMessage(id);
|
|
623
|
+
if (!draft || draft.status !== "draft" || !draft.pending) return;
|
|
624
|
+
const { action, by } = draft.pending;
|
|
625
|
+
const actor: Actor = { kind: "ui", name: by, scope: null };
|
|
626
|
+
draft.pending = null;
|
|
627
|
+
await this.store.putMessage(draft);
|
|
628
|
+
if (action === "send") await this.sendDraftUnlocked(id, actor);
|
|
629
|
+
else await this.discardDraftUnlocked(id, actor);
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
sendDraft(id: string, actor: Actor): Promise<Message> {
|
|
634
|
+
return this.store.serialize(() => this.sendDraftUnlocked(id, actor));
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* The draft id is the idempotency key and the Message-ID is stored before the provider call,
|
|
639
|
+
* so retrying a failed send repeats the same request instead of sending twice.
|
|
640
|
+
*/
|
|
641
|
+
private async sendDraftUnlocked(id: string, actor: Actor): Promise<Message> {
|
|
642
|
+
const draft = await this.requireDraft(id, actor.scope);
|
|
643
|
+
const sender = this.sendingMailbox(draft.mailbox);
|
|
644
|
+
if (!draft.message_id) {
|
|
645
|
+
draft.message_id = `<${randomUUID()}@${domainOf(sender.address)}>`;
|
|
646
|
+
await this.store.putMessage(draft);
|
|
647
|
+
}
|
|
648
|
+
const headers: Record<string, string> = { "Message-ID": draft.message_id };
|
|
649
|
+
if (draft.in_reply_to) headers["In-Reply-To"] = draft.in_reply_to;
|
|
650
|
+
if (draft.references.length) headers["References"] = draft.references.join(" ");
|
|
651
|
+
|
|
652
|
+
const attachments = await Promise.all(draft.attachments.map(async a => ({
|
|
653
|
+
filename: a.filename,
|
|
654
|
+
contentType: a.content_type,
|
|
655
|
+
content: await this.attachmentBody(draft, a),
|
|
656
|
+
})));
|
|
657
|
+
const { providerId } = await this.provider.send(
|
|
658
|
+
{
|
|
659
|
+
from: draft.from,
|
|
660
|
+
to: draft.to,
|
|
661
|
+
cc: draft.cc,
|
|
662
|
+
bcc: draft.bcc,
|
|
663
|
+
replyTo: draft.reply_to,
|
|
664
|
+
subject: draft.subject,
|
|
665
|
+
text: sender.signatureText ? `${draft.text}\n\n${sender.signatureText}` : draft.text,
|
|
666
|
+
html: (draft.html ?? textToHtml(draft.text)) + (sender.signatureHtml ? `\n${sender.signatureHtml}` : ""),
|
|
667
|
+
headers,
|
|
668
|
+
attachments,
|
|
669
|
+
},
|
|
670
|
+
draft.id,
|
|
671
|
+
);
|
|
672
|
+
|
|
673
|
+
draft.status = "sent";
|
|
674
|
+
draft.pending = null;
|
|
675
|
+
draft.provider_id = providerId;
|
|
676
|
+
draft.sent_at = new Date().toISOString();
|
|
677
|
+
draft.headers = headers;
|
|
678
|
+
await this.store.putMessage(draft);
|
|
679
|
+
await this.store.indexProviderId(this.provider.name, providerId, draft.id);
|
|
680
|
+
await this.store.indexMessageId(draft.message_id, draft.id);
|
|
681
|
+
await this.store.setDraft(draft.id, false);
|
|
682
|
+
await refreshThread(this.store, draft.thread_id);
|
|
683
|
+
await this.store.audit("send", actor, draft.id, { provider_id: providerId });
|
|
684
|
+
return draft;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
/** Direct send. If the provider fails, the message stays as a pending draft. */
|
|
688
|
+
send(input: CreateDraftInput, actor: Actor): Promise<Message> {
|
|
689
|
+
return this.store.serialize(async () => {
|
|
690
|
+
const draft = await this.createDraftUnlocked(input, actor);
|
|
691
|
+
return this.sendDraftUnlocked(draft.id, actor);
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
reply(messageId: string, text: string, replyAll: boolean, actor: Actor): Promise<Message> {
|
|
696
|
+
return this.send({ replyToId: messageId, replyAll, text }, actor);
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/** An inbound attachment's metadata and bytes, from disk or, the first time, from the provider. */
|
|
700
|
+
async getAttachment(messageId: string, attachmentId: string, scope: Scope): Promise<{ meta: AttachmentMeta; body: Uint8Array }> {
|
|
701
|
+
const message = await this.requireMessage(messageId, scope);
|
|
702
|
+
const meta = message.attachments.find(a => a.id === attachmentId);
|
|
703
|
+
if (!meta) throw new NotFoundError(`Attachment ${attachmentId} not found in message ${messageId}`);
|
|
704
|
+
return { meta, body: await this.attachmentBody(message, meta) };
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
private async attachmentBody(message: Message, meta: AttachmentMeta): Promise<Uint8Array> {
|
|
708
|
+
const stored = await this.files.read(message.id, meta.id);
|
|
709
|
+
if (stored) return stored;
|
|
710
|
+
if (!message.provider_id || message.direction !== "in") throw new NotFoundError(`Attachment ${meta.id} not found`);
|
|
711
|
+
const body = await this.provider.getAttachment(message.provider_id, meta.id);
|
|
712
|
+
await this.files.write(message.id, meta.id, body);
|
|
713
|
+
return body;
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
export function toView(m: Message): MessageView {
|
|
718
|
+
return {
|
|
719
|
+
id: m.id,
|
|
720
|
+
thread_id: m.thread_id,
|
|
721
|
+
mailbox: m.mailbox,
|
|
722
|
+
direction: m.direction,
|
|
723
|
+
status: m.status,
|
|
724
|
+
from: m.from,
|
|
725
|
+
to: m.to,
|
|
726
|
+
cc: m.cc,
|
|
727
|
+
subject: m.subject,
|
|
728
|
+
date: messageTime(m),
|
|
729
|
+
text: m.text || (m.html ? htmlToText(m.html) : ""),
|
|
730
|
+
labels: Object.keys(m.labels),
|
|
731
|
+
read: m.read,
|
|
732
|
+
attachments: m.attachments,
|
|
733
|
+
note: m.note,
|
|
734
|
+
version: m.version,
|
|
735
|
+
edited_by_human: m.edited_by_human,
|
|
736
|
+
};
|
|
737
|
+
}
|