@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.
Files changed (131) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +676 -2
  3. package/_dist/addr.d.ts +40 -0
  4. package/_dist/addr.d.ts.map +1 -0
  5. package/_dist/answers.d.ts +18 -0
  6. package/_dist/answers.d.ts.map +1 -0
  7. package/_dist/arrive.d.ts +64 -0
  8. package/_dist/arrive.d.ts.map +1 -0
  9. package/_dist/capture.d.ts +22 -0
  10. package/_dist/capture.d.ts.map +1 -0
  11. package/_dist/comp.d.ts +27 -0
  12. package/_dist/comp.d.ts.map +1 -0
  13. package/_dist/effects.d.ts +9 -0
  14. package/_dist/effects.d.ts.map +1 -0
  15. package/_dist/gmail.d.ts +16 -0
  16. package/_dist/gmail.d.ts.map +1 -0
  17. package/_dist/graph.d.ts +9 -0
  18. package/_dist/graph.d.ts.map +1 -0
  19. package/_dist/inbound.d.ts +105 -0
  20. package/_dist/inbound.d.ts.map +1 -0
  21. package/_dist/invite.d.ts +69 -0
  22. package/_dist/invite.d.ts.map +1 -0
  23. package/_dist/md.d.ts +34 -0
  24. package/_dist/md.d.ts.map +1 -0
  25. package/_dist/mod.d.ts +109 -0
  26. package/_dist/mod.d.ts.map +1 -0
  27. package/_dist/options.d.ts +38 -0
  28. package/_dist/options.d.ts.map +1 -0
  29. package/_dist/plugin.d.ts +38 -0
  30. package/_dist/plugin.d.ts.map +1 -0
  31. package/_dist/pull.d.ts +99 -0
  32. package/_dist/pull.d.ts.map +1 -0
  33. package/_dist/receipts.d.ts +8 -0
  34. package/_dist/receipts.d.ts.map +1 -0
  35. package/_dist/recipient.d.ts +24 -0
  36. package/_dist/recipient.d.ts.map +1 -0
  37. package/_dist/routes.d.ts +19 -0
  38. package/_dist/routes.d.ts.map +1 -0
  39. package/_dist/send.d.ts +80 -0
  40. package/_dist/send.d.ts.map +1 -0
  41. package/_dist/service.d.ts +14 -0
  42. package/_dist/service.d.ts.map +1 -0
  43. package/_dist/stash.d.ts +23 -0
  44. package/_dist/stash.d.ts.map +1 -0
  45. package/_dist/tools.d.ts +27 -0
  46. package/_dist/tools.d.ts.map +1 -0
  47. package/_dist/transport.d.ts +6 -0
  48. package/_dist/transport.d.ts.map +1 -0
  49. package/_dist/vocab.d.ts +7 -0
  50. package/_dist/vocab.d.ts.map +1 -0
  51. package/addr.js +92 -0
  52. package/addr.js.map +1 -0
  53. package/addr.ts +106 -0
  54. package/answers.js +71 -0
  55. package/answers.js.map +1 -0
  56. package/answers.ts +95 -0
  57. package/arrive.js +122 -0
  58. package/arrive.js.map +1 -0
  59. package/arrive.ts +198 -0
  60. package/capture.js +21 -0
  61. package/capture.js.map +1 -0
  62. package/capture.ts +43 -0
  63. package/comp.js +76 -0
  64. package/comp.js.map +1 -0
  65. package/comp.ts +91 -0
  66. package/deno.json +1 -0
  67. package/effects.js +90 -0
  68. package/effects.js.map +1 -0
  69. package/effects.ts +90 -0
  70. package/gmail.js +262 -0
  71. package/gmail.js.map +1 -0
  72. package/gmail.ts +314 -0
  73. package/graph.js +26 -0
  74. package/graph.js.map +1 -0
  75. package/graph.ts +30 -0
  76. package/inbound.js +206 -0
  77. package/inbound.js.map +1 -0
  78. package/inbound.ts +238 -0
  79. package/invite.js +102 -0
  80. package/invite.js.map +1 -0
  81. package/invite.ts +143 -0
  82. package/md.js +188 -0
  83. package/md.js.map +1 -0
  84. package/md.ts +245 -0
  85. package/mod.js +109 -0
  86. package/mod.js.map +1 -0
  87. package/mod.ts +111 -0
  88. package/options.js +24 -0
  89. package/options.js.map +1 -0
  90. package/options.ts +83 -0
  91. package/package.json +66 -4
  92. package/plugin.js +97 -0
  93. package/plugin.js.map +1 -0
  94. package/plugin.ts +88 -0
  95. package/pull.js +344 -0
  96. package/pull.js.map +1 -0
  97. package/pull.ts +443 -0
  98. package/readings.js +24 -0
  99. package/readings.js.map +1 -0
  100. package/readings.ts +34 -0
  101. package/receipts.js +45 -0
  102. package/receipts.js.map +1 -0
  103. package/receipts.ts +45 -0
  104. package/recipient.js +92 -0
  105. package/recipient.js.map +1 -0
  106. package/recipient.ts +89 -0
  107. package/routes.js +103 -0
  108. package/routes.js.map +1 -0
  109. package/routes.ts +125 -0
  110. package/send.js +284 -0
  111. package/send.js.map +1 -0
  112. package/send.ts +325 -0
  113. package/service.js +64 -0
  114. package/service.js.map +1 -0
  115. package/service.ts +75 -0
  116. package/stash.js +29 -0
  117. package/stash.js.map +1 -0
  118. package/stash.ts +49 -0
  119. package/tools.js +343 -0
  120. package/tools.js.map +1 -0
  121. package/tools.ts +416 -0
  122. package/transport.js +23 -0
  123. package/transport.js.map +1 -0
  124. package/transport.ts +24 -0
  125. package/vocab.js +14 -0
  126. package/vocab.js.map +1 -0
  127. package/vocab.json +415 -0
  128. package/vocab.ts +16 -0
  129. package/work.js +121 -0
  130. package/work.js.map +1 -0
  131. package/work.ts +134 -0
package/work.ts ADDED
@@ -0,0 +1,134 @@
1
+ // A letter that reaches a project is work for it, the way a bug the tracker
2
+ // opens is (@yaks/heal files a task for each). Hearing it is not enough: a
3
+ // session of the project hears an arrival once on its bus (@yaks/session) and
4
+ // marks it `notified`, and a session that heard it and went on with other work
5
+ // left nothing behind that says a person is still waiting. So the arrival
6
+ // files a task in the project, which stays open on its board until an answer
7
+ // goes out or an agent closes it.
8
+ //
9
+ // mail_task an arrival to a project, or to an address filed under one
10
+ // (hello@yaks.app under yaks.app), files `task{}` +
11
+ // `doc{title, body}` + `filed{project, priority}`, linked to
12
+ // the letter by an `about` edge
13
+ // mail_answered a letter this graph delivered about it (./answers.ts
14
+ // `answering`: its reply, or one sent about it) completes
15
+ // the task the letter filed
16
+ // mail_dismissed the letter's task cancelled says the letter needs no
17
+ // answer, so the letter is archived (`archived{at, by, via}`
18
+ // copied from the cancel), which takes it off the unanswered
19
+ // list (`yak admin errors`)
20
+ //
21
+ // All three are declared under the `local` option: a graph whose addresses are
22
+ // entities that read it (an agent, a project) is one where a letter is work
23
+ // for whoever works here; where a person reads the mail in a mail client, the
24
+ // mailbox is the destination and files nothing.
25
+ //
26
+ // The task's eid derives from the letter, so a run interrupted and run again
27
+ // finds the task it wrote, and the reply finds the task to complete without a
28
+ // search. Every word is the vocabulary's to declare: a graph without tasks,
29
+ // projects or edges files nothing, or files without the link.
30
+
31
+ import { line } from '@yaks/query'
32
+ import type { Bundle, Comp, Eid, ReadTx } from 'jsr:@yaks/graph@0.2.36'
33
+ import type { Handlers } from 'jsr:@yaks/effects@0.2.36'
34
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
35
+ import { identityEid } from '@yaks/graph/identity'
36
+ import { link } from '@yaks/edge'
37
+ import { DELIVER, DELIVERED, EMAIL, MAIL } from './comp.js'
38
+ import { answering } from './answers.js'
39
+
40
+ /** The task a letter filed: derived from the letter, so it is found again. */
41
+ export let taskEid = (letter: Eid): Eid => identityEid('mail_task', [letter])
42
+
43
+ let comp = (b: Bundle | undefined, name: string): Comp =>
44
+ (b?.[name] ?? {}) as Comp
45
+ let str = (v: unknown): string => typeof v == 'string' ? v : ''
46
+ let one = async (tx: ReadTx, eid: string): Promise<Bundle | undefined> =>
47
+ eid ? (await tx.get([eid]))[0] : undefined
48
+
49
+ /** The project a letter reached: its target when that is a project, or the
50
+ * project the target's address is filed under. */
51
+ let projectOf = async (tx: ReadTx, target: string): Promise<string> => {
52
+ let to = await one(tx, target)
53
+ if (!to) return ''
54
+ if (to.project) return target
55
+ return to[EMAIL] ? str(comp(to, 'filed').project) : ''
56
+ }
57
+
58
+ /** The task a letter files, for an agent to answer it. */
59
+ let filing = (letter: Bundle, project: Eid): Bundle[] => {
60
+ let eid = letter.entity.eid
61
+ let mail = comp(letter, MAIL)
62
+ let from = str(mail.reply_address) || str(mail.from)
63
+ let subject = str(comp(letter, 'doc').title).split('\n')[0] || '(no subject)'
64
+ return [{
65
+ entity: { eid: taskEid(eid) },
66
+ task: {},
67
+ doc: {
68
+ title: `Letter from ${from || 'someone'}: ${
69
+ subject.length > 160 ? `${subject.slice(0, 160)}…` : subject
70
+ }`,
71
+ body: `A letter${from ? ` from ${from}` : ''} reached this project. ` +
72
+ `Read it with \`yak mail show ${eid}\`. A reply (\`yak mail ` +
73
+ 'reply`) or a letter sent `--about` it completes this task. An ' +
74
+ 'answer given where the letter asks for one instead, such as a ' +
75
+ 'support thread it names, does not, so complete the task once it is ' +
76
+ 'given. When the letter needs no answer, cancel this task: that ' +
77
+ 'archives the letter.',
78
+ },
79
+ filed: { project, priority: 1 },
80
+ }]
81
+ }
82
+
83
+ /** `mail_task`, `mail_answered` and `mail_dismissed`, for a graph whose vocabulary has tasks
84
+ * filed under projects; under any other, neither does anything. */
85
+ export let work = (vocab?: Vocab): Handlers => ({
86
+ mail_task: async (event, tx, write) => {
87
+ if (!vocab?.comp('task') || !vocab.prop('filed', 'project')) return
88
+ let letter = await one(tx, event.entity.eid)
89
+ if (!letter?.[MAIL] || letter[DELIVER]) return
90
+ let project = await projectOf(tx, str(comp(letter, MAIL).target))
91
+ if (!project || await one(tx, taskEid(letter.entity.eid))) return
92
+ let linked = vocab.comp('edge') && vocab.comp('about')
93
+ await write([
94
+ ...filing(letter, project),
95
+ ...linked
96
+ ? [link(taskEid(letter.entity.eid), 'about', event.entity.eid)]
97
+ : [],
98
+ ])
99
+ },
100
+ mail_answered: async (event, tx, write) => {
101
+ let reply = await one(tx, event.entity.eid)
102
+ if (!reply?.[DELIVER] || !reply[DELIVERED]) return
103
+ let tasks = await Promise.all(
104
+ answering(reply).map((letter) => one(tx, taskEid(letter))),
105
+ )
106
+ let open = tasks.filter((t) => t?.task && !t.completed && !t.cancelled)
107
+ if (open.length) {
108
+ await write(open.map((t) => ({ entity: t!.entity, completed: {} })))
109
+ }
110
+ },
111
+ mail_dismissed: async (event, tx, write) => {
112
+ if (!vocab?.comp('archived') || !vocab.comp('about')) return
113
+ let task = await one(tx, event.entity.eid)
114
+ let cancelled = task?.cancelled as Comp | undefined
115
+ if (!cancelled) return
116
+ let links = await tx.read(
117
+ line`.about&.edge.from=${task!.entity.eid}`,
118
+ )
119
+ let letters = await tx.get(
120
+ links.map((l) => str(comp(l, 'edge').to))
121
+ .filter((to) => taskEid(to) == task!.entity.eid),
122
+ )
123
+ let waiting = letters.filter((l) => l?.[MAIL] && !l.archived)
124
+ if (waiting.length) {
125
+ let { at, by, via } = cancelled
126
+ await write(waiting.map((l) => ({
127
+ entity: l!.entity,
128
+ archived: Object.fromEntries(
129
+ Object.entries({ at, by, via }).filter(([, v]) => v != undefined),
130
+ ),
131
+ })))
132
+ }
133
+ },
134
+ })