@yaks/mail 0.0.0-stage → 0.2.36
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 +202 -0
- package/README.md +676 -2
- package/_dist/addr.d.ts +40 -0
- package/_dist/addr.d.ts.map +1 -0
- package/_dist/answers.d.ts +18 -0
- package/_dist/answers.d.ts.map +1 -0
- package/_dist/arrive.d.ts +64 -0
- package/_dist/arrive.d.ts.map +1 -0
- package/_dist/capture.d.ts +22 -0
- package/_dist/capture.d.ts.map +1 -0
- package/_dist/comp.d.ts +27 -0
- package/_dist/comp.d.ts.map +1 -0
- package/_dist/effects.d.ts +9 -0
- package/_dist/effects.d.ts.map +1 -0
- package/_dist/gmail.d.ts +16 -0
- package/_dist/gmail.d.ts.map +1 -0
- package/_dist/graph.d.ts +9 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/inbound.d.ts +105 -0
- package/_dist/inbound.d.ts.map +1 -0
- package/_dist/invite.d.ts +69 -0
- package/_dist/invite.d.ts.map +1 -0
- package/_dist/md.d.ts +34 -0
- package/_dist/md.d.ts.map +1 -0
- package/_dist/mod.d.ts +109 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/options.d.ts +38 -0
- package/_dist/options.d.ts.map +1 -0
- package/_dist/plugin.d.ts +38 -0
- package/_dist/plugin.d.ts.map +1 -0
- package/_dist/pull.d.ts +99 -0
- package/_dist/pull.d.ts.map +1 -0
- package/_dist/receipts.d.ts +8 -0
- package/_dist/receipts.d.ts.map +1 -0
- package/_dist/recipient.d.ts +24 -0
- package/_dist/recipient.d.ts.map +1 -0
- package/_dist/routes.d.ts +19 -0
- package/_dist/routes.d.ts.map +1 -0
- package/_dist/send.d.ts +80 -0
- package/_dist/send.d.ts.map +1 -0
- package/_dist/service.d.ts +14 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/stash.d.ts +23 -0
- package/_dist/stash.d.ts.map +1 -0
- package/_dist/tools.d.ts +27 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/transport.d.ts +6 -0
- package/_dist/transport.d.ts.map +1 -0
- package/_dist/vocab.d.ts +7 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/addr.js +92 -0
- package/addr.js.map +1 -0
- package/addr.ts +106 -0
- package/answers.js +71 -0
- package/answers.js.map +1 -0
- package/answers.ts +95 -0
- package/arrive.js +122 -0
- package/arrive.js.map +1 -0
- package/arrive.ts +198 -0
- package/capture.js +21 -0
- package/capture.js.map +1 -0
- package/capture.ts +43 -0
- package/comp.js +76 -0
- package/comp.js.map +1 -0
- package/comp.ts +91 -0
- package/deno.json +1 -0
- package/effects.js +90 -0
- package/effects.js.map +1 -0
- package/effects.ts +90 -0
- package/gmail.js +262 -0
- package/gmail.js.map +1 -0
- package/gmail.ts +314 -0
- package/graph.js +26 -0
- package/graph.js.map +1 -0
- package/graph.ts +30 -0
- package/inbound.js +206 -0
- package/inbound.js.map +1 -0
- package/inbound.ts +238 -0
- package/invite.js +102 -0
- package/invite.js.map +1 -0
- package/invite.ts +143 -0
- package/md.js +188 -0
- package/md.js.map +1 -0
- package/md.ts +245 -0
- package/mod.js +109 -0
- package/mod.js.map +1 -0
- package/mod.ts +111 -0
- package/options.js +24 -0
- package/options.js.map +1 -0
- package/options.ts +83 -0
- package/package.json +66 -4
- package/plugin.js +97 -0
- package/plugin.js.map +1 -0
- package/plugin.ts +88 -0
- package/pull.js +344 -0
- package/pull.js.map +1 -0
- package/pull.ts +443 -0
- package/readings.js +24 -0
- package/readings.js.map +1 -0
- package/readings.ts +34 -0
- package/receipts.js +45 -0
- package/receipts.js.map +1 -0
- package/receipts.ts +45 -0
- package/recipient.js +92 -0
- package/recipient.js.map +1 -0
- package/recipient.ts +89 -0
- package/routes.js +103 -0
- package/routes.js.map +1 -0
- package/routes.ts +125 -0
- package/send.js +284 -0
- package/send.js.map +1 -0
- package/send.ts +325 -0
- package/service.js +64 -0
- package/service.js.map +1 -0
- package/service.ts +75 -0
- package/stash.js +29 -0
- package/stash.js.map +1 -0
- package/stash.ts +49 -0
- package/tools.js +343 -0
- package/tools.js.map +1 -0
- package/tools.ts +416 -0
- package/transport.js +23 -0
- package/transport.js.map +1 -0
- package/transport.ts +24 -0
- package/vocab.js +14 -0
- package/vocab.js.map +1 -0
- package/vocab.json +415 -0
- package/vocab.ts +16 -0
- package/work.js +121 -0
- package/work.js.map +1 -0
- package/work.ts +134 -0
package/tools.ts
ADDED
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
// What an agent may ask for here: the `@yaks/mail/tools` entry point — the
|
|
2
|
+
// implementations behind the `tool: true` declarations in ./vocab.json. Three
|
|
3
|
+
// commands a person runs all day, and one check.
|
|
4
|
+
//
|
|
5
|
+
// Reading and answering belong to the caller's attention. The shared
|
|
6
|
+
// subscription row keeps the thread's `opened` and `archived` marks apart from
|
|
7
|
+
// the letter's own state, and the inbox uses the same thread identity.
|
|
8
|
+
// Marks are refreshed atomically, so repeated actions receive a fresh time.
|
|
9
|
+
// A graph using these tools composes @yaks/notify and @yaks/kernel vocabulary.
|
|
10
|
+
//
|
|
11
|
+
// The sender is not this file's business. `mail send` and `mail reply` create
|
|
12
|
+
// a letter that asks to be sent (`deliver`), and ./effects.ts — built from the
|
|
13
|
+
// `sender` the config names beside this plugin — is what hands it to a
|
|
14
|
+
// transport and writes `delivered` or `bounced` back onto it. So a tool that
|
|
15
|
+
// writes mail never talks to a mail server, and a test replaces the whole of
|
|
16
|
+
// it with `{"via": "stash"}`.
|
|
17
|
+
//
|
|
18
|
+
// What is not checked in `mail check`, deliberately: whether each address this
|
|
19
|
+
// graph can create is deliverable at the MTA — the fleet's doctor read
|
|
20
|
+
// Cloudflare Email Routing's live rule set to determine that. That is a
|
|
21
|
+
// question about a zone somebody deployed, answered using a credential the
|
|
22
|
+
// server holds, and its answer is the same whatever graph is running; a
|
|
23
|
+
// package's check reads the graph its own components describe. A deployment
|
|
24
|
+
// that wants that check asks its provider, in its own deployment's check, and
|
|
25
|
+
// this package's `bounced{reason}` is what records the answer arriving the
|
|
26
|
+
// hard way.
|
|
27
|
+
|
|
28
|
+
import type { Bundle, Comp, Eid, Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
29
|
+
import { addressed, argsOf, who } from '@yaks/graph/call'
|
|
30
|
+
import { BODY, DOC, TITLE } from '@yaks/doc'
|
|
31
|
+
import { human } from '@yaks/id'
|
|
32
|
+
import { threadOf as attentionTarget } from '@yaks/notify'
|
|
33
|
+
import { ancestry, markAttention } from '@yaks/notify/attention'
|
|
34
|
+
import { absent, and, eq, every, list, present } from '@yaks/query'
|
|
35
|
+
import type { Finding } from 'jsr:@yaks/tools@0.2.36'
|
|
36
|
+
import { CallError } from '@yaks/tools/errors'
|
|
37
|
+
import { checked } from '@yaks/tools/check'
|
|
38
|
+
import { said } from '@yaks/tools/result'
|
|
39
|
+
import type { Runs } from 'jsr:@yaks/graph@0.2.36/tools'
|
|
40
|
+
import { wearer } from './arrive.js'
|
|
41
|
+
import { post } from './effects.js'
|
|
42
|
+
import { DELIVER, EMAIL, MAIL } from './comp.js'
|
|
43
|
+
import type { Options } from './options.ts'
|
|
44
|
+
import type { Host } from 'jsr:@yaks/host@0.2.36'
|
|
45
|
+
|
|
46
|
+
let str = (v: unknown): string => v == null ? '' : String(v)
|
|
47
|
+
|
|
48
|
+
let comp = (b: Bundle | undefined, name: string): Comp | undefined =>
|
|
49
|
+
b?.[name] as Comp | undefined
|
|
50
|
+
|
|
51
|
+
let prop = (c: Comp | undefined, k: string): string => str(c?.[k])
|
|
52
|
+
|
|
53
|
+
// One entity whole, by eid. Everything here works from the letter as it
|
|
54
|
+
// stands rather than from a patch, the way ./send.ts does.
|
|
55
|
+
let one = async (graph: Graph, eid: Eid): Promise<Bundle | undefined> =>
|
|
56
|
+
(await graph.get([eid]))[0]
|
|
57
|
+
|
|
58
|
+
// The eid a recipient argument names, whatever form a person typed it in:
|
|
59
|
+
// `to` takes an address or an id, so it is not a declared reference the
|
|
60
|
+
// runner resolves the way it resolves `who`, `item`, `letter` and `about`
|
|
61
|
+
// (@yaks/tools).
|
|
62
|
+
let at = async (graph: Graph, said: unknown): Promise<Eid> =>
|
|
63
|
+
(await addressed(graph, [str(said)]))[0]
|
|
64
|
+
|
|
65
|
+
// The letter an argument names, rejected unless it carries `mail`.
|
|
66
|
+
let letterIn = async (graph: Graph, said: unknown): Promise<Bundle> => {
|
|
67
|
+
let found = await one(graph, str(said))
|
|
68
|
+
if (!comp(found, MAIL)) {
|
|
69
|
+
throw new CallError('letter', `not a letter: ${str(said)}`)
|
|
70
|
+
}
|
|
71
|
+
return found!
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** `Re:` derivation — shed however many `Re:`/`Fwd:` layers already piled up,
|
|
75
|
+
* so a long thread does not grow a subject line. */
|
|
76
|
+
export let reSubject = (said: string): string =>
|
|
77
|
+
`Re: ${said.replace(/^(\s*(re|fwd?):\s*)+/i, '').trim()}`
|
|
78
|
+
|
|
79
|
+
/** A letter's two sides as a reply to it sees them. An arrival — a letter
|
|
80
|
+
* with a Message-ID that never asked to be sent; one of ours carries
|
|
81
|
+
* `deliver`, and has a Message-ID too once the transport gave it one — is
|
|
82
|
+
* answered at whom it says to answer, its Reply-To, else its author, and from
|
|
83
|
+
* the address it was delivered to. Our own letter is answered to its
|
|
84
|
+
* recipient, from the address it went out from. */
|
|
85
|
+
export let sides = (letter: Bundle): {
|
|
86
|
+
/** whether it arrived here */
|
|
87
|
+
arrived: boolean
|
|
88
|
+
/** the far side: an arrival's address, or our letter's recipient's eid */
|
|
89
|
+
far: string
|
|
90
|
+
/** the address a reply goes from, this side of the thread */
|
|
91
|
+
from: string
|
|
92
|
+
} => {
|
|
93
|
+
let mail = comp(letter, MAIL)
|
|
94
|
+
let deliver = comp(letter, DELIVER)
|
|
95
|
+
let arrived = !!prop(mail, 'message_id') && !deliver
|
|
96
|
+
return arrived
|
|
97
|
+
? {
|
|
98
|
+
arrived,
|
|
99
|
+
far: prop(mail, 'reply_address') || prop(mail, 'from'),
|
|
100
|
+
from: prop(mail, 'to'),
|
|
101
|
+
}
|
|
102
|
+
: { arrived, far: prop(deliver, 'to'), from: prop(mail, 'from') }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// The far side's address as an entity, since `deliver.to` names a recipient
|
|
106
|
+
// rather than a string: whoever already has that address, else a new `email`
|
|
107
|
+
// row for it. That is what makes the address book grow by use instead of by
|
|
108
|
+
// bookkeeping — and why writing to a stranger needs no step before it. An
|
|
109
|
+
// empty address resolves to nobody, never to a blank row: an `email` entity
|
|
110
|
+
// with no address is a recipient a letter can be aimed at and never reach.
|
|
111
|
+
let recipientOf = async (
|
|
112
|
+
graph: Graph,
|
|
113
|
+
address: string,
|
|
114
|
+
domain?: string,
|
|
115
|
+
): Promise<{ to: Eid; made: Bundle[] }> => {
|
|
116
|
+
if (!address) return { to: '', made: [] }
|
|
117
|
+
let known = await wearer(graph, address, domain)
|
|
118
|
+
return known
|
|
119
|
+
? { to: known, made: [] }
|
|
120
|
+
: { to: '$to', made: [{ entity: { eid: '$to' }, [EMAIL]: { address } }] }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Whom an argument means: a string with an `@` is an address, anything else is
|
|
124
|
+
// an id.
|
|
125
|
+
let aimedAt = async (
|
|
126
|
+
graph: Graph,
|
|
127
|
+
said: string,
|
|
128
|
+
domain?: string,
|
|
129
|
+
): Promise<{ to: Eid; made: Bundle[] }> =>
|
|
130
|
+
said.includes('@')
|
|
131
|
+
? await recipientOf(graph, said, domain)
|
|
132
|
+
: { to: await at(graph, said), made: [] }
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* A letter's thread: up its one-parent `reply_to` chain, then down over
|
|
136
|
+
* whatever answers anything already found. Each read is keyed by the letters
|
|
137
|
+
* in hand, so a thread costs its own rows rather than every letter ever sent.
|
|
138
|
+
*/
|
|
139
|
+
export let threadOf = async (
|
|
140
|
+
graph: Graph,
|
|
141
|
+
letter: Bundle,
|
|
142
|
+
): Promise<Bundle[]> => {
|
|
143
|
+
let found = await ancestry(graph, letter)
|
|
144
|
+
let seen = new Set(found.map((b) => b.entity.eid))
|
|
145
|
+
let refs = (rows: Bundle[]) => [
|
|
146
|
+
...new Set(
|
|
147
|
+
rows.flatMap((b) => [
|
|
148
|
+
b.entity.eid,
|
|
149
|
+
prop(comp(b, MAIL), 'message_id'),
|
|
150
|
+
prop(comp(b, 'notified'), 'message_id'),
|
|
151
|
+
]).filter(Boolean),
|
|
152
|
+
),
|
|
153
|
+
]
|
|
154
|
+
for (let front = refs(found); front.length;) {
|
|
155
|
+
let down = (await graph.read(
|
|
156
|
+
and(eq(`${MAIL}.reply_to`, list(...front)), every()),
|
|
157
|
+
{ native: true },
|
|
158
|
+
))
|
|
159
|
+
.filter((b) => !seen.has(b.entity.eid))
|
|
160
|
+
for (let b of down) seen.add(b.entity.eid), found.push(b)
|
|
161
|
+
front = refs(down)
|
|
162
|
+
}
|
|
163
|
+
// Chronological where the letters record a time, insertion order where they
|
|
164
|
+
// do not — a sort that is stable either way rather than one that invents a
|
|
165
|
+
// time.
|
|
166
|
+
return found.sort((a, b) =>
|
|
167
|
+
prop(comp(a, MAIL), 'at').localeCompare(prop(comp(b, MAIL), 'at'))
|
|
168
|
+
)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Resolve the caller and thread through the shared policy before touching
|
|
172
|
+
// attention. A mail target aimed at the caller is an address, not a thread.
|
|
173
|
+
let attentionOf = async (
|
|
174
|
+
graph: Graph,
|
|
175
|
+
call: Bundle,
|
|
176
|
+
thread: Bundle[],
|
|
177
|
+
letter: Bundle,
|
|
178
|
+
) => {
|
|
179
|
+
let actor = str(who(call)?.by)
|
|
180
|
+
if (!actor) {
|
|
181
|
+
throw new CallError(
|
|
182
|
+
'who',
|
|
183
|
+
'nobody is asking — mail attention needs a caller',
|
|
184
|
+
)
|
|
185
|
+
}
|
|
186
|
+
let [person] = await graph.get([actor])
|
|
187
|
+
let rows = thread.map((b) => ({
|
|
188
|
+
eid: b.entity.eid,
|
|
189
|
+
comps: b as Record<string, Comp>,
|
|
190
|
+
}))
|
|
191
|
+
let byId = new Map(rows.map((r) => [r.eid, r]))
|
|
192
|
+
for (let r of rows) {
|
|
193
|
+
for (let id of [r.comps.mail?.message_id, r.comps.notified?.message_id]) {
|
|
194
|
+
if (id) byId.set(str(id), r)
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
let target = attentionTarget({
|
|
198
|
+
eid: letter.entity.eid,
|
|
199
|
+
comps: letter as Record<string, Comp>,
|
|
200
|
+
}, {
|
|
201
|
+
actor,
|
|
202
|
+
scope: person?.project ? actor : undefined,
|
|
203
|
+
session: person?.session ? actor : undefined,
|
|
204
|
+
}, byId)
|
|
205
|
+
let [subscription] = await graph.read(and(
|
|
206
|
+
eq('subscription.actor', actor),
|
|
207
|
+
eq('subscription.target', target),
|
|
208
|
+
every(),
|
|
209
|
+
))
|
|
210
|
+
return { actor, target, subscription }
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// One letter as a line: who it is from, what it is about, and whether it has
|
|
214
|
+
// been read — `●` unread, `·` read, `×` archived, the way a mail client does.
|
|
215
|
+
let line =
|
|
216
|
+
(id: (b: Bundle) => string, attention?: Bundle) => (b: Bundle): string => {
|
|
217
|
+
let mail = comp(b, MAIL)
|
|
218
|
+
let at = prop(mail, 'at') || prop(comp(b, 'created'), 'at')
|
|
219
|
+
let marked = (name: string) => {
|
|
220
|
+
let mark = comp(attention, name)
|
|
221
|
+
return !!mark && (!mark.at || str(mark.at) >= at)
|
|
222
|
+
}
|
|
223
|
+
let dot = marked('archived') ? '×' : marked('opened') ? '·' : '●'
|
|
224
|
+
let who = prop(mail, 'from')
|
|
225
|
+
return `${dot} ${id(b)}${who ? ` ${who}` : ''} — ${
|
|
226
|
+
prop(comp(b, DOC), TITLE) || '(no subject)'
|
|
227
|
+
}`
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// One letter whole: the envelope a person reads, then the words, then the rest
|
|
231
|
+
// of the thread with this one marked.
|
|
232
|
+
let page = (
|
|
233
|
+
id: (b: Bundle) => string,
|
|
234
|
+
letter: Bundle,
|
|
235
|
+
thread: Bundle[],
|
|
236
|
+
attention?: Bundle,
|
|
237
|
+
) => {
|
|
238
|
+
let mail = comp(letter, MAIL)
|
|
239
|
+
let doc = comp(letter, DOC)
|
|
240
|
+
let said = [
|
|
241
|
+
`# ${prop(doc, TITLE) || '(no subject)'}`,
|
|
242
|
+
[
|
|
243
|
+
prop(mail, 'from') && `from ${prop(mail, 'from')}`,
|
|
244
|
+
prop(mail, 'to') && `to ${prop(mail, 'to')}`,
|
|
245
|
+
prop(mail, 'at'),
|
|
246
|
+
mail?.verified === false && 'UNSIGNED',
|
|
247
|
+
].filter(Boolean).join(' · '),
|
|
248
|
+
'',
|
|
249
|
+
prop(doc, BODY),
|
|
250
|
+
]
|
|
251
|
+
if (thread.length > 1) {
|
|
252
|
+
said.push(
|
|
253
|
+
'',
|
|
254
|
+
'## Thread',
|
|
255
|
+
...thread.map((t) =>
|
|
256
|
+
`${t.entity.eid == letter.entity.eid ? '▶' : ' '} ${
|
|
257
|
+
line(id, attention)(t)
|
|
258
|
+
}`
|
|
259
|
+
),
|
|
260
|
+
)
|
|
261
|
+
}
|
|
262
|
+
return said.join('\n')
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** The implementations of the tools ./vocab.json declares. A factory, like
|
|
266
|
+
* every other entry point in these packages: the domain is what an address a
|
|
267
|
+
* person typed is canonicalized against before the address book is queried,
|
|
268
|
+
* into the same form the canonicalizer stored it in (./plugin.ts). Everything
|
|
269
|
+
* else a handler needs arrives on the call it is handed. */
|
|
270
|
+
export let runs = (
|
|
271
|
+
host?: Pick<Host, 'senders'> | null,
|
|
272
|
+
options: Options = {},
|
|
273
|
+
): Runs => ({
|
|
274
|
+
// Reading is the mark, so this writes. The prose is the result; the `opened`
|
|
275
|
+
// patch is what keeps a second call from reporting it as unread.
|
|
276
|
+
mail_show: async (call, graph): Promise<Bundle[]> => {
|
|
277
|
+
let args = argsOf(call)
|
|
278
|
+
let letter = await letterIn(graph, args.letter)
|
|
279
|
+
let thread = await threadOf(graph, letter)
|
|
280
|
+
let attention = await attentionOf(graph, call, thread, letter)
|
|
281
|
+
return [
|
|
282
|
+
...await markAttention(
|
|
283
|
+
graph,
|
|
284
|
+
attention.actor,
|
|
285
|
+
attention.target,
|
|
286
|
+
'opened',
|
|
287
|
+
),
|
|
288
|
+
said(
|
|
289
|
+
call,
|
|
290
|
+
page(human(graph.vocab), letter, thread, attention.subscription),
|
|
291
|
+
),
|
|
292
|
+
]
|
|
293
|
+
},
|
|
294
|
+
|
|
295
|
+
// The reply goes to the far side ({@link sides}) and never to a fallback
|
|
296
|
+
// between an arrival's and ours: the wrong choice here is this graph's own
|
|
297
|
+
// address, so a reply that quietly went there would look sent without
|
|
298
|
+
// being sent.
|
|
299
|
+
mail_reply: async (call, graph): Promise<Bundle[]> => {
|
|
300
|
+
let args = argsOf(call)
|
|
301
|
+
let letter = await letterIn(graph, args.letter)
|
|
302
|
+
let { arrived, far: answered, from } = sides(letter)
|
|
303
|
+
let far = arrived
|
|
304
|
+
? await recipientOf(graph, answered, options.domain)
|
|
305
|
+
: { to: answered, made: [] as Bundle[] }
|
|
306
|
+
if (!far.to) {
|
|
307
|
+
throw new CallError(
|
|
308
|
+
'reply',
|
|
309
|
+
'cannot reply: that letter names nobody to answer — send a fresh mail',
|
|
310
|
+
)
|
|
311
|
+
}
|
|
312
|
+
let attention = arrived
|
|
313
|
+
? await attentionOf(graph, call, await threadOf(graph, letter), letter)
|
|
314
|
+
: undefined
|
|
315
|
+
return [
|
|
316
|
+
...far.made,
|
|
317
|
+
{
|
|
318
|
+
entity: { eid: '$reply' },
|
|
319
|
+
[DOC]: {
|
|
320
|
+
[TITLE]: reSubject(prop(comp(letter, DOC), TITLE)),
|
|
321
|
+
[BODY]: str(args.body),
|
|
322
|
+
},
|
|
323
|
+
// No `target`: on an arrival that property holds whom the letter was
|
|
324
|
+
// routed to (./arrive.ts), which is this side of the thread — carrying
|
|
325
|
+
// it forward would file our own answer as one more letter to us.
|
|
326
|
+
// `reply_to` is what threads a reply, and it is enough.
|
|
327
|
+
[MAIL]: { ...(from ? { from } : {}), reply_to: letter.entity.eid },
|
|
328
|
+
[DELIVER]: { to: far.to },
|
|
329
|
+
},
|
|
330
|
+
// Answering an arrival puts its thread away: a thread you have replied
|
|
331
|
+
// to is not one waiting on you. Only an arrival — following up on your
|
|
332
|
+
// own letter left nothing waiting, so there is nothing to put away.
|
|
333
|
+
...(attention
|
|
334
|
+
? await markAttention(
|
|
335
|
+
graph,
|
|
336
|
+
attention.actor,
|
|
337
|
+
attention.target,
|
|
338
|
+
'archived',
|
|
339
|
+
)
|
|
340
|
+
: []),
|
|
341
|
+
]
|
|
342
|
+
},
|
|
343
|
+
|
|
344
|
+
// A letter goes out from the address of what it is about, so the answer
|
|
345
|
+
// comes back to that: a project's letter from the project's address. Where
|
|
346
|
+
// that has none, from whoever is asking.
|
|
347
|
+
mail_send: async (call, graph): Promise<Bundle[]> => {
|
|
348
|
+
let args = argsOf(call)
|
|
349
|
+
let far = await aimedAt(graph, str(args.to), options.domain)
|
|
350
|
+
if (!far.to) {
|
|
351
|
+
throw new CallError('to', 'a letter needs somebody to go to — say --to')
|
|
352
|
+
}
|
|
353
|
+
let target = str(args.about)
|
|
354
|
+
let address = async (eid: string) =>
|
|
355
|
+
eid ? prop(comp(await one(graph, eid), EMAIL), 'address') : ''
|
|
356
|
+
let from = args.from != null
|
|
357
|
+
? str(args.from)
|
|
358
|
+
: options.from?.() || await address(target) ||
|
|
359
|
+
await address(str(who(call)?.by))
|
|
360
|
+
if (!from) {
|
|
361
|
+
throw new CallError(
|
|
362
|
+
'from',
|
|
363
|
+
'a letter needs a from address — say --from, or send it about ' +
|
|
364
|
+
'something with an `email.address`, or give whoever is asking one',
|
|
365
|
+
)
|
|
366
|
+
}
|
|
367
|
+
return [
|
|
368
|
+
...far.made,
|
|
369
|
+
{
|
|
370
|
+
entity: { eid: '$letter' },
|
|
371
|
+
[DOC]: {
|
|
372
|
+
[TITLE]: str(args.subject),
|
|
373
|
+
[BODY]: str(args.body),
|
|
374
|
+
},
|
|
375
|
+
[MAIL]: { from, ...(target ? { target } : {}) },
|
|
376
|
+
[DELIVER]: { to: far.to },
|
|
377
|
+
},
|
|
378
|
+
]
|
|
379
|
+
},
|
|
380
|
+
|
|
381
|
+
mail_check: async (call, graph) => {
|
|
382
|
+
// An arrival is a letter with a Message-ID that never asked to be sent
|
|
383
|
+
// (see `sides`), and one with no sender has nobody to answer.
|
|
384
|
+
let orphans = await graph.read(
|
|
385
|
+
and(
|
|
386
|
+
present(`${MAIL}.message_id`),
|
|
387
|
+
absent(DELIVER),
|
|
388
|
+
absent(`${MAIL}.from`),
|
|
389
|
+
every(),
|
|
390
|
+
),
|
|
391
|
+
)
|
|
392
|
+
let id = human(graph.vocab)
|
|
393
|
+
let found = orphans.map((b): Finding => ({
|
|
394
|
+
level: 'fail',
|
|
395
|
+
text: `${id(b)} arrived with no sender — a reply has nowhere to go`,
|
|
396
|
+
}))
|
|
397
|
+
// And the other direction: a transport this server could not build leaves
|
|
398
|
+
// outbound letters sitting in the graph. Missing config never stops the
|
|
399
|
+
// server from starting (./effects.ts), so this is where it is reported.
|
|
400
|
+
let { waiting } = options.sender
|
|
401
|
+
? post(options.sender, host)
|
|
402
|
+
: { waiting: undefined }
|
|
403
|
+
if (waiting) {
|
|
404
|
+
found.push({
|
|
405
|
+
level: 'warn',
|
|
406
|
+
text: `nothing is being sent — ${waiting}. What is written meanwhile ` +
|
|
407
|
+
`waits in the graph`,
|
|
408
|
+
})
|
|
409
|
+
}
|
|
410
|
+
return checked(
|
|
411
|
+
call.entity.eid,
|
|
412
|
+
'every letter that arrived carries a sender, and this server can send',
|
|
413
|
+
found,
|
|
414
|
+
)
|
|
415
|
+
},
|
|
416
|
+
})
|
package/transport.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// One name selects one sender. The host lends its senders by name, a provider
|
|
2
|
+
// among them by naming its plugin in the config (@yaks/resend,
|
|
3
|
+
// @yaks/cloudflare-mail); mail owns the selection and leaves a letter waiting
|
|
4
|
+
// while its sender cannot send.
|
|
5
|
+
import { stash } from "./stash.js";
|
|
6
|
+
/** The sender a name selects from the host's, or why it cannot send yet.
|
|
7
|
+
* `stash` is the built-in one that keeps letters in memory. */ export let post = (said, host)=>{
|
|
8
|
+
if (host?.senders && Object.hasOwn(host.senders, said.via)) {
|
|
9
|
+
let sender = host.senders[said.via];
|
|
10
|
+
let waiting = sender.waiting?.();
|
|
11
|
+
return waiting ? {
|
|
12
|
+
waiting
|
|
13
|
+
} : {
|
|
14
|
+
sender
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
return said.via == 'stash' ? {
|
|
18
|
+
sender: stash()
|
|
19
|
+
} : {
|
|
20
|
+
waiting: `no sender called ${JSON.stringify(said.via)}`
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=transport.js.map
|
package/transport.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.js","sources":["./transport.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,+DAA+D;AAC/D,8EAA8E;AAC9E,gCAAgC;AAIhC,SAAS,KAAK,qBAAoB;AAElC;6DAC6D,GAC7D,OAAO,IAAI,OAAO,CAChB,MACA;EAEA,IAAI,MAAM,WAAW,OAAO,MAAM,CAAC,KAAK,OAAO,EAAE,KAAK,GAAG,GAAG;IAC1D,IAAI,SAAS,KAAK,OAAO,CAAC,KAAK,GAAG,CAAC;IACnC,IAAI,UAAU,OAAO,OAAO;IAC5B,OAAO,UAAU;MAAE;IAAQ,IAAI;MAAE;IAAO;EAC1C;EACA,OAAO,KAAK,GAAG,IAAI,UACf;IAAE,QAAQ;EAAQ,IAClB;IAAE,SAAS,CAAC,iBAAiB,EAAE,KAAK,SAAS,CAAC,KAAK,GAAG,GAAG;EAAC;AAChE,EAAC"}
|
package/transport.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// One name selects one sender. The host lends its senders by name, a provider
|
|
2
|
+
// among them by naming its plugin in the config (@yaks/resend,
|
|
3
|
+
// @yaks/cloudflare-mail); mail owns the selection and leaves a letter waiting
|
|
4
|
+
// while its sender cannot send.
|
|
5
|
+
import type { Host } from 'jsr:@yaks/host@0.2.36'
|
|
6
|
+
import type { Built } from 'jsr:@yaks/host@0.2.36/senders'
|
|
7
|
+
import type { Transport } from './options.ts'
|
|
8
|
+
import { stash } from './stash.js'
|
|
9
|
+
|
|
10
|
+
/** The sender a name selects from the host's, or why it cannot send yet.
|
|
11
|
+
* `stash` is the built-in one that keeps letters in memory. */
|
|
12
|
+
export let post = (
|
|
13
|
+
said: Transport,
|
|
14
|
+
host?: Pick<Host, 'senders'> | null,
|
|
15
|
+
): Built => {
|
|
16
|
+
if (host?.senders && Object.hasOwn(host.senders, said.via)) {
|
|
17
|
+
let sender = host.senders[said.via]
|
|
18
|
+
let waiting = sender.waiting?.()
|
|
19
|
+
return waiting ? { waiting } : { sender }
|
|
20
|
+
}
|
|
21
|
+
return said.via == 'stash'
|
|
22
|
+
? { sender: stash() }
|
|
23
|
+
: { waiting: `no sender called ${JSON.stringify(said.via)}` }
|
|
24
|
+
}
|
package/vocab.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// The mail components, and only those: the `@yaks/mail/vocab` entry point. It
|
|
2
|
+
// imports no storage, no SQL and no runtime, so a browser tab that loads this
|
|
3
|
+
// vocabulary loads nothing else.
|
|
4
|
+
import { mailDoc } from "./comp.js";
|
|
5
|
+
import manifest from './package.json' with {
|
|
6
|
+
type: 'json'
|
|
7
|
+
};
|
|
8
|
+
export { mailDoc };
|
|
9
|
+
/** What this package is: its manifest's description, the one place it is
|
|
10
|
+
* written, which a host stamps on each document here. */ export let description = manifest.description;
|
|
11
|
+
/** Every document this plugin declares. */ export let docs = [
|
|
12
|
+
mailDoc
|
|
13
|
+
];
|
|
14
|
+
//# sourceMappingURL=vocab.js.map
|
package/vocab.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vocab.js","sources":["./vocab.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,8EAA8E;AAC9E,iCAAiC;AAGjC,SAAS,OAAO,oBAAmB;AACnC,OAAO,cAAc,sBAAsB;EAAE,MAAM;AAAO,EAAC;AAE3D,SAAS,OAAO,GAAE;AAElB;uDACuD,GACvD,OAAO,IAAI,cAAsB,SAAS,WAAW,CAAA;AAErD,yCAAyC,GACzC,OAAO,IAAI,OAAmB;EAAC;CAAQ,CAAA"}
|