@soffits/dsh-notify-inbox 0.3.0 → 0.5.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/CHANGELOG.md +15 -0
- package/README.md +24 -4
- package/client.js +273 -203
- package/docs/ui.md +36 -0
- package/index.js +21 -2
- package/locale/en.json +15 -6
- package/locale/fr.json +16 -7
- package/locale/zh.json +14 -5
- package/package.json +24 -4
- package/src/actions.js +3 -2
- package/src/changes.js +75 -0
- package/src/domains.js +49 -0
- package/src/routes.js +69 -15
- package/src/store.js +103 -7
- package/src/view.js +75 -0
package/src/store.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* <dataDir>/<person>/inbox.jsonl one record per line; written by the router only
|
|
6
6
|
* <dataDir>/<person>/files/<id>/<name> attachments the router copied in
|
|
7
7
|
* <dataDir>/<person>/read.json read marks; written by this plugin only
|
|
8
|
+
* <dataDir>/<person>/summary.json the line shown above the items; written by the router's side
|
|
8
9
|
*
|
|
9
10
|
* Records are re-read incrementally: only the bytes appended since the last
|
|
10
11
|
* look are parsed, and the whole file again when it shrank or was replaced
|
|
@@ -18,16 +19,62 @@ export const PERSON_PATTERN = /^[a-z0-9][a-z0-9_-]{0,63}$/
|
|
|
18
19
|
export const RECORD_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9_.:-]{0,79}$/
|
|
19
20
|
const INBOX = 'inbox.jsonl'
|
|
20
21
|
const READ = 'read.json'
|
|
22
|
+
const SUMMARY = 'summary.json'
|
|
21
23
|
|
|
22
24
|
const text = value => (typeof value === 'string' ? value : '')
|
|
23
25
|
const list = value => (Array.isArray(value) ? value : [])
|
|
26
|
+
/** Text a person reads: a string, or a map from language tag to text (the page picks the language). */
|
|
27
|
+
const words = value => (typeof value === 'string' ? value : value !== null && typeof value === 'object' && !Array.isArray(value) && Object.values(value).every(v => typeof v === 'string') ? value : '')
|
|
28
|
+
|
|
29
|
+
export const GROUPS = ['needs-you', 'live', 'handled', 'info']
|
|
30
|
+
const ASKING = ['done', 'snooze']
|
|
31
|
+
const MAX_THREAD = 120
|
|
32
|
+
|
|
33
|
+
const isoTime = value => {
|
|
34
|
+
const at = Date.parse(text(value))
|
|
35
|
+
return Number.isNaN(at) ? undefined : new Date(at).toISOString()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** `live` as `{ until?, progress?: { from, to } }`, or null. */
|
|
39
|
+
function liveOf(raw) {
|
|
40
|
+
if (raw === true) return {}
|
|
41
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) return null
|
|
42
|
+
const out = {}
|
|
43
|
+
const until = isoTime(raw.until)
|
|
44
|
+
if (until !== undefined) out.until = until
|
|
45
|
+
const from = isoTime(raw.progress?.from)
|
|
46
|
+
const to = isoTime(raw.progress?.to)
|
|
47
|
+
if (from !== undefined && to !== undefined && from < to) out.progress = { from, to }
|
|
48
|
+
return out
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** `handled` as `{ text, at? }`, or null. */
|
|
52
|
+
function handledOf(raw) {
|
|
53
|
+
const value = typeof raw === 'string' ? { text: raw } : raw
|
|
54
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value) || text(value.text).trim() === '') return null
|
|
55
|
+
const at = isoTime(value.at)
|
|
56
|
+
return at === undefined ? { text: value.text.trim() } : { text: value.text.trim(), at }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The group a record belongs in. A writer usually says (`attention`); for a record that doesn't,
|
|
61
|
+
* the same default the router documents: handled, then live, then needs-you for an interrupt or a
|
|
62
|
+
* record with done or snooze buttons, else info.
|
|
63
|
+
*/
|
|
64
|
+
export function attentionOf(record) {
|
|
65
|
+
if (GROUPS.includes(record.attention)) return record.attention
|
|
66
|
+
if (record.handled !== null) return 'handled'
|
|
67
|
+
if (record.live !== null) return 'live'
|
|
68
|
+
if (record.lane === 'interrupt' || record.actions.some(action => ASKING.includes(action.kind))) return 'needs-you'
|
|
69
|
+
return 'info'
|
|
70
|
+
}
|
|
24
71
|
|
|
25
72
|
/** A record as the page gets it, or undefined when the line is not one. */
|
|
26
73
|
export function normalize(raw, person) {
|
|
27
74
|
if (typeof raw !== 'object' || raw === null || !RECORD_ID_PATTERN.test(text(raw.id))) return undefined
|
|
28
75
|
const time = Date.parse(text(raw.time))
|
|
29
76
|
if (Number.isNaN(time)) return undefined
|
|
30
|
-
|
|
77
|
+
const record = {
|
|
31
78
|
id: raw.id,
|
|
32
79
|
person,
|
|
33
80
|
time: new Date(time).toISOString(),
|
|
@@ -48,9 +95,13 @@ export function normalize(raw, person) {
|
|
|
48
95
|
actions: list(raw.actions).filter(action => typeof action?.kind === 'string' && typeof action?.id === 'string').map(action => ({
|
|
49
96
|
kind: action.kind,
|
|
50
97
|
id: action.id,
|
|
51
|
-
label:
|
|
98
|
+
label: words(action.label) || undefined,
|
|
52
99
|
payload: typeof action.payload === 'object' && action.payload !== null && !Array.isArray(action.payload) ? action.payload : {},
|
|
53
100
|
})),
|
|
101
|
+
attention: text(raw.attention),
|
|
102
|
+
thread: text(raw.thread).trim().slice(0, MAX_THREAD),
|
|
103
|
+
live: liveOf(raw.live),
|
|
104
|
+
handled: handledOf(raw.handled),
|
|
54
105
|
test: raw.test === true,
|
|
55
106
|
push: raw.push !== false,
|
|
56
107
|
routed: list(raw.routed).filter(row => typeof row?.destination === 'string').map(row => ({
|
|
@@ -60,6 +111,9 @@ export function normalize(raw, person) {
|
|
|
60
111
|
detail: text(row.detail) || undefined,
|
|
61
112
|
})),
|
|
62
113
|
}
|
|
114
|
+
record.attention = attentionOf(record)
|
|
115
|
+
if (record.live === null && record.attention === 'live') record.live = {}
|
|
116
|
+
return record
|
|
63
117
|
}
|
|
64
118
|
|
|
65
119
|
/** A "this press was handled" line the router appends after acting on a press, or undefined. */
|
|
@@ -112,6 +166,24 @@ export async function readFrom(path, offset) {
|
|
|
112
166
|
}
|
|
113
167
|
}
|
|
114
168
|
|
|
169
|
+
const HEAD_BYTES = 256
|
|
170
|
+
|
|
171
|
+
/** The first bytes of a file, as text (empty when it can't be read). */
|
|
172
|
+
async function readHead(path, length) {
|
|
173
|
+
try {
|
|
174
|
+
const handle = await open(path, 'r')
|
|
175
|
+
try {
|
|
176
|
+
const buffer = Buffer.alloc(length)
|
|
177
|
+
const { bytesRead } = await handle.read(buffer, 0, length, 0)
|
|
178
|
+
return buffer.subarray(0, bytesRead).toString('latin1')
|
|
179
|
+
} finally {
|
|
180
|
+
await handle.close()
|
|
181
|
+
}
|
|
182
|
+
} catch {
|
|
183
|
+
return ''
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
115
187
|
async function atomicWrite(path, value) {
|
|
116
188
|
const tmp = `${path}.${process.pid}.${Date.now()}.tmp`
|
|
117
189
|
await writeFile(tmp, JSON.stringify(value), { mode: 0o600 })
|
|
@@ -152,11 +224,14 @@ export function createStore(dataDir) {
|
|
|
152
224
|
return { records: [], handled: [], bad: 0 }
|
|
153
225
|
}
|
|
154
226
|
let entry = cache.get(person)
|
|
155
|
-
|
|
156
|
-
|
|
227
|
+
// A file rewritten in place (same inode, someone removed a line by hand) starts differently.
|
|
228
|
+
const replaced = entry !== undefined && entry.head !== '' && (await readHead(path, entry.head.length)) !== entry.head
|
|
229
|
+
if (entry === undefined || entry.ino !== info.ino || info.size < entry.offset || replaced) {
|
|
230
|
+
entry = { ino: info.ino, offset: 0, head: '', records: [], handled: [], bad: 0 }
|
|
157
231
|
cache.set(person, entry)
|
|
158
232
|
}
|
|
159
233
|
if (info.size > entry.offset) {
|
|
234
|
+
if (entry.offset === 0) entry.head = await readHead(path, HEAD_BYTES)
|
|
160
235
|
const parsed = parseLines(await readFrom(path, entry.offset), person)
|
|
161
236
|
entry.records.push(...parsed.records)
|
|
162
237
|
entry.handled.push(...parsed.handled)
|
|
@@ -212,7 +287,7 @@ export function createStore(dataDir) {
|
|
|
212
287
|
await saveMarks(person, { readBefore, read: current.read.filter(id => byId.has(id) && byId.get(id).time > readBefore), unread: [] })
|
|
213
288
|
}
|
|
214
289
|
|
|
215
|
-
/** The records of these people, newest first, each with `read
|
|
290
|
+
/** The records of these people, newest first, each with `read` and the `outcomes` of presses on it. */
|
|
216
291
|
async function view(people) {
|
|
217
292
|
const out = []
|
|
218
293
|
let bad = 0
|
|
@@ -221,12 +296,33 @@ export function createStore(dataDir) {
|
|
|
221
296
|
bad += skipped
|
|
222
297
|
const byRecord = new Map()
|
|
223
298
|
for (const status of handled) byRecord.set(status.record, [...(byRecord.get(status.record) ?? []), status])
|
|
224
|
-
for (const record of rows) out.push({ ...record, read: isRead(marked, record),
|
|
299
|
+
for (const record of rows) out.push({ ...record, read: isRead(marked, record), outcomes: byRecord.get(record.id) ?? [] })
|
|
225
300
|
}
|
|
226
301
|
out.sort((a, b) => (a.time === b.time ? 0 : a.time < b.time ? 1 : -1))
|
|
227
302
|
return { items: out, bad }
|
|
228
303
|
}
|
|
229
304
|
|
|
305
|
+
/**
|
|
306
|
+
* The newest summary line of these people (`<person>/summary.json`, `{ text, at }`, written by
|
|
307
|
+
* whatever keeps the inbox), or null when there is none or it is older than `maxHours`.
|
|
308
|
+
*/
|
|
309
|
+
async function summaryLine(people, maxHours, now = Date.now()) {
|
|
310
|
+
let best = null
|
|
311
|
+
for (const person of people) {
|
|
312
|
+
let value
|
|
313
|
+
try {
|
|
314
|
+
value = JSON.parse(await readFile(join(dataDir, person, SUMMARY), 'utf8'))
|
|
315
|
+
} catch {
|
|
316
|
+
continue
|
|
317
|
+
}
|
|
318
|
+
const said = words(value?.text)
|
|
319
|
+
const at = isoTime(value?.at)
|
|
320
|
+
if (said === '' || at === undefined || now - Date.parse(at) > maxHours * 3600 * 1000) continue
|
|
321
|
+
if (best === null || at > best.at) best = { text: said, at }
|
|
322
|
+
}
|
|
323
|
+
return best
|
|
324
|
+
}
|
|
325
|
+
|
|
230
326
|
/** Where a stored attachment lives, or undefined. */
|
|
231
327
|
async function file(people, id, name) {
|
|
232
328
|
if (!RECORD_ID_PATTERN.test(id) || typeof name !== 'string' || name === '' || name.includes('/') || name.includes('\\') || name.startsWith('.')) return undefined
|
|
@@ -238,5 +334,5 @@ export function createStore(dataDir) {
|
|
|
238
334
|
return undefined
|
|
239
335
|
}
|
|
240
336
|
|
|
241
|
-
return { persons, records, marks, setRead, markAllRead, view, file }
|
|
337
|
+
return { persons, records, marks, setRead, markAllRead, view, summaryLine, file }
|
|
242
338
|
}
|
package/src/view.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the page lists: items, not records. Records of one person with the same
|
|
3
|
+
* `thread` fold into one item, the newest on top and the older ones as its
|
|
4
|
+
* history; a live thread's newest record simply replaces the item. Each item
|
|
5
|
+
* gets the group it shows in now, which can differ from what its writer said:
|
|
6
|
+
* a needs-you item the person marked done is handled, a snoozed one waits
|
|
7
|
+
* under info until it comes back as a new record, and a live item whose time
|
|
8
|
+
* is over is info.
|
|
9
|
+
*/
|
|
10
|
+
import { createHash } from 'node:crypto'
|
|
11
|
+
import { GROUPS } from './store.js'
|
|
12
|
+
|
|
13
|
+
/** A live item without `until` stops being live this long after its last update. */
|
|
14
|
+
export const LIVE_IDLE_MS = 6 * 60 * 60 * 1000
|
|
15
|
+
const HISTORY_MAX = 20
|
|
16
|
+
|
|
17
|
+
/** A stable key for an item: the record id, or a hash of person and thread (safe in a query string). */
|
|
18
|
+
export function itemKey(record) {
|
|
19
|
+
if (record.thread === '') return record.id
|
|
20
|
+
return `t-${createHash('sha256').update(`${record.person}\n${record.thread}`).digest('hex').slice(0, 20)}`
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Whether a record's live state still runs at `now` (ms). */
|
|
24
|
+
export function liveNow(record, now) {
|
|
25
|
+
if (record.live === null) return false
|
|
26
|
+
const end = record.live.until !== undefined ? Date.parse(record.live.until) : Date.parse(record.time) + LIVE_IDLE_MS
|
|
27
|
+
return now < end
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The group an item's newest record shows in at `now`, with `moved` saying why when it isn't the
|
|
32
|
+
* writer's: 'done' (the person marked it done), 'snoozed', or 'ended' (a live item past its time).
|
|
33
|
+
*/
|
|
34
|
+
export function groupOf(record, now) {
|
|
35
|
+
if (record.attention === 'needs-you') {
|
|
36
|
+
const last = record.outcomes.findLast(row => row.result === 'done' || row.result === 'snoozed')
|
|
37
|
+
if (last?.result === 'done') return { group: 'handled', moved: 'done' }
|
|
38
|
+
if (last?.result === 'snoozed' && (last.until === undefined || Date.parse(last.until) > now)) return { group: 'info', moved: 'snoozed' }
|
|
39
|
+
}
|
|
40
|
+
if (record.attention === 'live' && !liveNow(record, now)) return { group: 'info', moved: 'ended' }
|
|
41
|
+
return { group: record.attention, moved: null }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Fold records (newest first, each with `read` and `outcomes`) into items, ordered by group
|
|
46
|
+
* (needs-you, live, handled, info) and newest first within a group.
|
|
47
|
+
*/
|
|
48
|
+
export function buildItems(records, now) {
|
|
49
|
+
const byKey = new Map()
|
|
50
|
+
for (const record of records) {
|
|
51
|
+
const key = itemKey(record)
|
|
52
|
+
const item = byKey.get(key)
|
|
53
|
+
if (item === undefined) {
|
|
54
|
+
byKey.set(key, { ...record, key, ids: [record.id], history: [] })
|
|
55
|
+
continue
|
|
56
|
+
}
|
|
57
|
+
item.ids.push(record.id)
|
|
58
|
+
item.read &&= record.read
|
|
59
|
+
if (item.history.length < HISTORY_MAX) item.history.push({ id: record.id, time: record.time, title: record.title, spoken: record.spoken })
|
|
60
|
+
}
|
|
61
|
+
const items = [...byKey.values()].map(item => ({ ...item, ...groupOf(item, now), liveNow: liveNow(item, now) }))
|
|
62
|
+
const rank = group => GROUPS.indexOf(group)
|
|
63
|
+
return items.sort((a, b) => rank(a.group) - rank(b.group) || (a.time === b.time ? 0 : a.time < b.time ? 1 : -1))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** How many items each group has, and how many are unread. */
|
|
67
|
+
export function countItems(items) {
|
|
68
|
+
const groups = Object.fromEntries(GROUPS.map(group => [group, 0]))
|
|
69
|
+
let unread = 0
|
|
70
|
+
for (const item of items) {
|
|
71
|
+
groups[item.group] += 1
|
|
72
|
+
if (!item.read) unread += 1
|
|
73
|
+
}
|
|
74
|
+
return { groups, unread }
|
|
75
|
+
}
|