@soffits/dsh-card-board 0.10.0 → 0.11.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 +10 -0
- package/README.md +16 -3
- package/client.js +510 -123
- package/examples/boards/home/cards/countdown.json +9 -0
- package/index.js +2 -0
- package/locale/en.json +16 -1
- package/locale/fr.json +16 -1
- package/locale/zh.json +16 -1
- package/package.json +1 -1
- package/src/arrange-store.js +54 -0
- package/src/arrange.js +119 -0
- package/src/person-store.js +54 -0
- package/src/routes.js +31 -3
- package/src/schema.js +16 -1
- package/src/seen.js +7 -37
|
@@ -53,5 +53,14 @@
|
|
|
53
53
|
}
|
|
54
54
|
],
|
|
55
55
|
"expiresAt": "2026-03-04T10:50:00+01:00"
|
|
56
|
+
},
|
|
57
|
+
"raise": {
|
|
58
|
+
"from": "2026-03-03T07:40:00+01:00",
|
|
59
|
+
"until": "2026-03-04T10:50:00+01:00",
|
|
60
|
+
"label": {
|
|
61
|
+
"en": "Leaves tomorrow",
|
|
62
|
+
"fr": "Départ demain",
|
|
63
|
+
"zh": "明天出发"
|
|
64
|
+
}
|
|
56
65
|
}
|
|
57
66
|
}
|
package/index.js
CHANGED
|
@@ -12,6 +12,7 @@ import { isAbsolute, join, resolve } from 'node:path'
|
|
|
12
12
|
import { fileURLToPath } from 'node:url'
|
|
13
13
|
import z from '@deepseek-ai/schemastery'
|
|
14
14
|
import { createPeople, expandHome } from './src/access.js'
|
|
15
|
+
import { createArrangeStore } from './src/arrange-store.js'
|
|
15
16
|
import { createMessages } from './src/messages.js'
|
|
16
17
|
import { createPresses } from './src/presses.js'
|
|
17
18
|
import { createRoutes } from './src/routes.js'
|
|
@@ -76,6 +77,7 @@ export function apply(ctx, config) {
|
|
|
76
77
|
pollSeconds: config.pollSeconds ?? 30,
|
|
77
78
|
presses,
|
|
78
79
|
seen: createSeenStore(join(dataDir, '.seen')),
|
|
80
|
+
arrange: createArrangeStore(join(dataDir, '.arrange')),
|
|
79
81
|
watches,
|
|
80
82
|
messages: createMessages(fileURLToPath(new URL('./locale', import.meta.url))),
|
|
81
83
|
version,
|
package/locale/en.json
CHANGED
|
@@ -47,6 +47,21 @@
|
|
|
47
47
|
"toast.undone": "Undone.",
|
|
48
48
|
"toast.undoFailed": "Couldn’t undo that.",
|
|
49
49
|
"undo": "Undo",
|
|
50
|
-
"markNotDone": "Mark not done"
|
|
50
|
+
"markNotDone": "Mark not done",
|
|
51
|
+
"arrange.menu": "Arrange this card (drag to move)",
|
|
52
|
+
"arrange.pin": "Pin to top",
|
|
53
|
+
"arrange.unpin": "Unpin",
|
|
54
|
+
"arrange.up": "Move up",
|
|
55
|
+
"arrange.down": "Move down",
|
|
56
|
+
"arrange.reset": "Reset order",
|
|
57
|
+
"arrange.resetHint": "Back to the order this board comes with",
|
|
58
|
+
"arrange.raised": "Up for now",
|
|
59
|
+
"arrange.raisedWhy": "Time matters for this card right now, so it sits above the rest",
|
|
60
|
+
"arrange.moved": "{title} is now number {n} of {total}",
|
|
61
|
+
"arrange.pinnedDone": "{title} pinned to the top",
|
|
62
|
+
"arrange.unpinnedDone": "{title} unpinned",
|
|
63
|
+
"arrange.resetDone": "Order reset to the board’s own",
|
|
64
|
+
"arrange.failed": "Couldn’t save your order.",
|
|
65
|
+
"arrange.pinned": "Pinned"
|
|
51
66
|
}
|
|
52
67
|
}
|
package/locale/fr.json
CHANGED
|
@@ -47,6 +47,21 @@
|
|
|
47
47
|
"toast.undone": "Annulé.",
|
|
48
48
|
"toast.undoFailed": "Impossible d’annuler.",
|
|
49
49
|
"undo": "Annuler",
|
|
50
|
-
"markNotDone": "Marquer comme non fait"
|
|
50
|
+
"markNotDone": "Marquer comme non fait",
|
|
51
|
+
"arrange.menu": "Organiser cette carte (glisser pour déplacer)",
|
|
52
|
+
"arrange.pin": "Épingler en haut",
|
|
53
|
+
"arrange.unpin": "Désépingler",
|
|
54
|
+
"arrange.up": "Monter",
|
|
55
|
+
"arrange.down": "Descendre",
|
|
56
|
+
"arrange.reset": "Rétablir l’ordre",
|
|
57
|
+
"arrange.resetHint": "Revenir à l’ordre d’origine de ce tableau",
|
|
58
|
+
"arrange.raised": "En haut pour l’instant",
|
|
59
|
+
"arrange.raisedWhy": "L’heure compte pour cette carte en ce moment, elle passe donc devant les autres",
|
|
60
|
+
"arrange.moved": "{title} est maintenant en position {n} sur {total}",
|
|
61
|
+
"arrange.pinnedDone": "{title} épinglée en haut",
|
|
62
|
+
"arrange.unpinnedDone": "{title} désépinglée",
|
|
63
|
+
"arrange.resetDone": "Ordre d’origine rétabli",
|
|
64
|
+
"arrange.failed": "Impossible d’enregistrer votre ordre.",
|
|
65
|
+
"arrange.pinned": "Épinglée"
|
|
51
66
|
}
|
|
52
67
|
}
|
package/locale/zh.json
CHANGED
|
@@ -47,6 +47,21 @@
|
|
|
47
47
|
"toast.undone": "已撤销。",
|
|
48
48
|
"toast.undoFailed": "没能撤销。",
|
|
49
49
|
"undo": "撤销",
|
|
50
|
-
"markNotDone": "标为未完成"
|
|
50
|
+
"markNotDone": "标为未完成",
|
|
51
|
+
"arrange.menu": "调整这张卡片(可拖动)",
|
|
52
|
+
"arrange.pin": "置顶",
|
|
53
|
+
"arrange.unpin": "取消置顶",
|
|
54
|
+
"arrange.up": "上移",
|
|
55
|
+
"arrange.down": "下移",
|
|
56
|
+
"arrange.reset": "恢复默认顺序",
|
|
57
|
+
"arrange.resetHint": "回到这个看板自带的顺序",
|
|
58
|
+
"arrange.raised": "暂时靠前",
|
|
59
|
+
"arrange.raisedWhy": "这张卡片现在和时间有关,所以排在其他卡片之前",
|
|
60
|
+
"arrange.moved": "{title} 现在排第 {n}(共 {total})",
|
|
61
|
+
"arrange.pinnedDone": "已置顶:{title}",
|
|
62
|
+
"arrange.unpinnedDone": "已取消置顶:{title}",
|
|
63
|
+
"arrange.resetDone": "已恢复默认顺序",
|
|
64
|
+
"arrange.failed": "没能保存你的顺序。",
|
|
65
|
+
"arrange.pinned": "已置顶"
|
|
51
66
|
}
|
|
52
67
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soffits/dsh-card-board",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "A data-driven card board for DeepSeek Harness: scripts write small JSON cards, the Web UI shows each person the boards they may see.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How each person has arranged each board, kept by the plugin per person so it follows them to every device. It is the
|
|
3
|
+
* only thing the plugin stores about cards; the cards themselves stay where their producers write them.
|
|
4
|
+
*
|
|
5
|
+
* <dataDir>/.arrange/<hash of the person>.json
|
|
6
|
+
* { "boards": { "<board>": { "pinned": ["<card>"], "order": ["<card>"] } } }
|
|
7
|
+
*
|
|
8
|
+
* A person's change reaches their other open pages through `subscribe`.
|
|
9
|
+
*/
|
|
10
|
+
import { parseArrangement } from './arrange.js'
|
|
11
|
+
import { createPersonStore } from './person-store.js'
|
|
12
|
+
import { ID_PATTERN } from './schema.js'
|
|
13
|
+
|
|
14
|
+
function clean(value) {
|
|
15
|
+
if (typeof value?.boards !== 'object' || value.boards === null) return undefined
|
|
16
|
+
const boards = {}
|
|
17
|
+
for (const [board, arrangement] of Object.entries(value.boards)) {
|
|
18
|
+
const parsed = ID_PATTERN.test(board) ? parseArrangement(arrangement) : undefined
|
|
19
|
+
if (parsed) boards[board] = parsed
|
|
20
|
+
}
|
|
21
|
+
return { boards }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createArrangeStore(dir) {
|
|
25
|
+
const store = createPersonStore(dir, () => ({ boards: {} }), clean)
|
|
26
|
+
const listeners = new Set()
|
|
27
|
+
const change = async (person, board, edit) => {
|
|
28
|
+
await store.update(person, value => {
|
|
29
|
+
const boards = { ...value.boards }
|
|
30
|
+
edit(boards)
|
|
31
|
+
return { boards }
|
|
32
|
+
})
|
|
33
|
+
for (const listener of [...listeners]) listener(person, board)
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
/** A person's arrangement of a board, or undefined when they never arranged it. */
|
|
37
|
+
async get(person, board) {
|
|
38
|
+
return (await store.read(person)).boards[board]
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
tag: store.tag,
|
|
42
|
+
|
|
43
|
+
set: (person, board, arrangement) => change(person, board, boards => { boards[board] = arrangement }),
|
|
44
|
+
|
|
45
|
+
/** Back to the board's own order. */
|
|
46
|
+
reset: (person, board) => change(person, board, boards => { delete boards[board] }),
|
|
47
|
+
|
|
48
|
+
/** Call `listener(person, board)` whenever an arrangement changes; returns the way to stop. */
|
|
49
|
+
subscribe(listener) {
|
|
50
|
+
listeners.add(listener)
|
|
51
|
+
return () => { listeners.delete(listener) }
|
|
52
|
+
},
|
|
53
|
+
}
|
|
54
|
+
}
|
package/src/arrange.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How one person arranges a board, and the order a viewer sees. Pure: the server checks and stores an arrangement,
|
|
3
|
+
* the page computes the order from it, and both use this file.
|
|
4
|
+
*
|
|
5
|
+
* The board's own `order` is the default. A person's arrangement is `{ pinned, order }`: the cards they pinned to the
|
|
6
|
+
* top (in the order they pinned them) and the sequence of the rest. Cards the arrangement doesn't know (a card that
|
|
7
|
+
* appeared since) take the place the default order gives them, so a new card is never lost at the bottom of an old
|
|
8
|
+
* arrangement. What the viewer finally sees puts the cards a producer has raised for now above all of it (`raised`).
|
|
9
|
+
*/
|
|
10
|
+
import { ID_PATTERN } from './schema.js'
|
|
11
|
+
|
|
12
|
+
/** Most cards a board holds; an arrangement lists no more. */
|
|
13
|
+
export const MAX_ARRANGED = 200
|
|
14
|
+
|
|
15
|
+
/** An arrangement from a request body or a stored file, or undefined when it is malformed. */
|
|
16
|
+
export function parseArrangement(value) {
|
|
17
|
+
if (typeof value !== 'object' || value === null) return undefined
|
|
18
|
+
const lists = []
|
|
19
|
+
for (const key of ['pinned', 'order']) {
|
|
20
|
+
const list = value[key]
|
|
21
|
+
if (!Array.isArray(list) || list.length > MAX_ARRANGED || !list.every(id => typeof id === 'string' && ID_PATTERN.test(id))) return undefined
|
|
22
|
+
lists.push(list)
|
|
23
|
+
}
|
|
24
|
+
const [pinned, order] = lists
|
|
25
|
+
if (new Set([...pinned, ...order]).size !== pinned.length + order.length) return undefined
|
|
26
|
+
return { pinned, order }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The full arrangement for the cards that exist now, in default order `ids`: what the person pinned and placed that is
|
|
31
|
+
* still there, and every other card slotted in after the card the default order puts before it.
|
|
32
|
+
* @param {string[]} ids - the cards present, in the board's default order
|
|
33
|
+
* @param {{ pinned: string[], order: string[] } | undefined} arrangement
|
|
34
|
+
* @returns {{ pinned: string[], order: string[] }}
|
|
35
|
+
*/
|
|
36
|
+
export function arranged(ids, arrangement) {
|
|
37
|
+
if (arrangement === undefined) return { pinned: [], order: [...ids] }
|
|
38
|
+
const present = new Set(ids)
|
|
39
|
+
const pinned = arrangement.pinned.filter(id => present.has(id))
|
|
40
|
+
const taken = new Set(pinned)
|
|
41
|
+
const order = arrangement.order.filter(id => present.has(id) && !taken.has(id))
|
|
42
|
+
const placed = new Set(order)
|
|
43
|
+
ids.forEach((id, i) => {
|
|
44
|
+
if (taken.has(id) || placed.has(id)) return
|
|
45
|
+
let after = -1
|
|
46
|
+
for (let before = i - 1; before >= 0 && after < 0; before--) after = order.indexOf(ids[before])
|
|
47
|
+
order.splice(after + 1, 0, id)
|
|
48
|
+
})
|
|
49
|
+
return { pinned, order }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Every card in the order the person arranged it: pinned first. */
|
|
53
|
+
export const sequenceOf = ({ pinned, order }) => [...pinned, ...order]
|
|
54
|
+
|
|
55
|
+
/** The order on the page: cards raised now first, then the rest, each keeping the person's order. */
|
|
56
|
+
export function visibleOrder(state, raised) {
|
|
57
|
+
const all = sequenceOf(state)
|
|
58
|
+
return [...all.filter(id => raised.has(id)), ...all.filter(id => !raised.has(id))]
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Pin a card: it joins the end of the pins. */
|
|
62
|
+
export function pinCard({ pinned, order }, id) {
|
|
63
|
+
return { pinned: [...pinned.filter(p => p !== id), id], order: order.filter(o => o !== id) }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Unpin a card: it stays where it is on the page, the first of the others. */
|
|
67
|
+
export function unpinCard({ pinned, order }, id) {
|
|
68
|
+
return { pinned: pinned.filter(p => p !== id), order: [id, ...order.filter(o => o !== id)] }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Move a card one place up or down among its own kind (pinned among the pins, the others among the others). */
|
|
72
|
+
export function shiftCard({ pinned, order }, id, delta) {
|
|
73
|
+
const shift = list => {
|
|
74
|
+
const from = list.indexOf(id)
|
|
75
|
+
const to = from + delta
|
|
76
|
+
if (from < 0 || to < 0 || to >= list.length) return list
|
|
77
|
+
const next = [...list]
|
|
78
|
+
next.splice(from, 1)
|
|
79
|
+
next.splice(to, 0, id)
|
|
80
|
+
return next
|
|
81
|
+
}
|
|
82
|
+
return { pinned: shift(pinned), order: shift(order) }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Whether a card can move one place up (delta -1) or down (+1) among its own kind. */
|
|
86
|
+
export function canShift({ pinned, order }, id, delta) {
|
|
87
|
+
const list = pinned.includes(id) ? pinned : order
|
|
88
|
+
const to = list.indexOf(id) + delta
|
|
89
|
+
return list.includes(id) && to >= 0 && to < list.length
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Drop a card before or after another one. Dropped among the pins it becomes a pin, dropped after them it is not one;
|
|
94
|
+
* right at the edge between the two it stays what it was.
|
|
95
|
+
*/
|
|
96
|
+
export function dropCard(state, id, target, after) {
|
|
97
|
+
if (id === target) return state
|
|
98
|
+
const wasPinned = state.pinned.includes(id)
|
|
99
|
+
const rest = sequenceOf(state).filter(card => card !== id)
|
|
100
|
+
const pinsLeft = state.pinned.filter(card => card !== id).length
|
|
101
|
+
const at = rest.indexOf(target) + (after ? 1 : 0)
|
|
102
|
+
if (at < 0 || rest.indexOf(target) < 0) return state
|
|
103
|
+
const next = [...rest.slice(0, at), id, ...rest.slice(at)]
|
|
104
|
+
const pinCount = at < pinsLeft ? pinsLeft + 1 : at > pinsLeft ? pinsLeft : pinsLeft + (wasPinned ? 1 : 0)
|
|
105
|
+
return { pinned: next.slice(0, pinCount), order: next.slice(pinCount) }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const at = (time, fallback) => (time === undefined ? fallback : Date.parse(time))
|
|
109
|
+
|
|
110
|
+
/** Whether a card's `raise` window covers `now` (ms). An open end counts as never ending, or always having begun. */
|
|
111
|
+
export function isRaised(raise, now) {
|
|
112
|
+
return raise !== undefined && at(raise.from, -Infinity) <= now && now < at(raise.until, Infinity)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** The next moment (ms) after `now` when any of these `raise` windows opens or closes, or undefined when none will. */
|
|
116
|
+
export function nextRaiseChange(raises, now) {
|
|
117
|
+
const times = raises.flatMap(raise => [at(raise?.from, NaN), at(raise?.until, NaN)]).filter(time => time > now)
|
|
118
|
+
return times.length > 0 ? Math.min(...times) : undefined
|
|
119
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the plugin keeps for each person, one small JSON file each: what they have seen (`.seen/`) and how they have
|
|
3
|
+
* arranged their boards (`.arrange/`). Writes to a person's file go one at a time and atomically.
|
|
4
|
+
*/
|
|
5
|
+
import { createHash } from 'node:crypto'
|
|
6
|
+
import { mkdir, readFile, rename, stat, writeFile } from 'node:fs/promises'
|
|
7
|
+
import { join } from 'node:path'
|
|
8
|
+
|
|
9
|
+
export function hash(value) {
|
|
10
|
+
return createHash('sha256').update(JSON.stringify(value)).digest('base64url').slice(0, 12)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {string} dir
|
|
15
|
+
* @param {() => object} empty - a person's file when they have none
|
|
16
|
+
* @param {(value: unknown) => object | undefined} clean - the file's content when it is well formed, else undefined
|
|
17
|
+
*/
|
|
18
|
+
export function createPersonStore(dir, empty, clean) {
|
|
19
|
+
const fileOf = person => join(dir, `${hash(String(person).toLowerCase())}.json`)
|
|
20
|
+
const queues = new Map()
|
|
21
|
+
|
|
22
|
+
async function read(person) {
|
|
23
|
+
try {
|
|
24
|
+
return clean(JSON.parse(await readFile(fileOf(person), 'utf8'))) ?? empty()
|
|
25
|
+
} catch {
|
|
26
|
+
return empty()
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
read,
|
|
32
|
+
|
|
33
|
+
/** A tag that changes whenever this person's file changes, for ETags. */
|
|
34
|
+
async tag(person) {
|
|
35
|
+
const info = await stat(fileOf(person)).catch(() => undefined)
|
|
36
|
+
return info ? `${info.size}:${info.mtimeMs}` : '-'
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
/** Change a person's file: `change` gets the current content and returns the new one. */
|
|
40
|
+
update(person, change) {
|
|
41
|
+
const path = fileOf(person)
|
|
42
|
+
const previous = queues.get(path) ?? Promise.resolve()
|
|
43
|
+
const next = previous.catch(() => {}).then(async () => {
|
|
44
|
+
const value = change(await read(person))
|
|
45
|
+
await mkdir(dir, { recursive: true, mode: 0o700 })
|
|
46
|
+
const tmp = `${path}.${process.pid}.tmp`
|
|
47
|
+
await writeFile(tmp, JSON.stringify(value), { mode: 0o600 })
|
|
48
|
+
await rename(tmp, path)
|
|
49
|
+
})
|
|
50
|
+
queues.set(path, next)
|
|
51
|
+
return next.finally(() => { if (queues.get(path) === next) queues.delete(path) })
|
|
52
|
+
},
|
|
53
|
+
}
|
|
54
|
+
}
|
package/src/routes.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
* GET /api/card-board/card?board=<id>&id=<card> one card (ETag, 304)
|
|
10
10
|
* GET /api/card-board/events?board=<id> Server-Sent Events: which card or board.json changed
|
|
11
11
|
* POST /api/card-board/seen the card versions the viewer has seen
|
|
12
|
+
* POST /api/card-board/arrange the viewer's own pins and order for a board, or `reset` back to the board's
|
|
12
13
|
* GET /api/card-board/locale?tag=fr the page's own words for a language (from locale/*.json)
|
|
13
14
|
* POST /api/card-board/action press a step, done, snooze or event action, or take a press back (`undo`)
|
|
14
15
|
*
|
|
@@ -17,6 +18,7 @@
|
|
|
17
18
|
*/
|
|
18
19
|
import { join } from 'node:path'
|
|
19
20
|
import { maySee, viewerOf } from './access.js'
|
|
21
|
+
import { parseArrangement } from './arrange.js'
|
|
20
22
|
import { LANGUAGE_ID } from './messages.js'
|
|
21
23
|
import { ID_PATTERN, PRESS_KINDS, findAction } from './schema.js'
|
|
22
24
|
import { cardVersions, freshness, parseSeen } from './seen.js'
|
|
@@ -27,6 +29,7 @@ export const BOARD_PATH = '/api/card-board/board'
|
|
|
27
29
|
export const CARD_PATH = '/api/card-board/card'
|
|
28
30
|
export const EVENTS_PATH = '/api/card-board/events'
|
|
29
31
|
export const SEEN_PATH = '/api/card-board/seen'
|
|
32
|
+
export const ARRANGE_PATH = '/api/card-board/arrange'
|
|
30
33
|
export const ACTION_PATH = '/api/card-board/action'
|
|
31
34
|
export const LOCALE_PATH = '/api/card-board/locale'
|
|
32
35
|
const DATE_TIME = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:\d{2})$/
|
|
@@ -53,12 +56,13 @@ const NO_PROVIDER = 'This board needs a sign-in plugin that provides requestIden
|
|
|
53
56
|
* @param {number} options.pollSeconds
|
|
54
57
|
* @param {ReturnType<import('./presses.js').createPresses>} options.presses
|
|
55
58
|
* @param {ReturnType<import('./seen.js').createSeenStore>} options.seen
|
|
59
|
+
* @param {ReturnType<import('./arrange-store.js').createArrangeStore>} options.arrange
|
|
56
60
|
* @param {ReturnType<import('./watch.js').createWatches>} options.watches
|
|
57
61
|
* @param {(tag: string) => Promise<object>} options.messages - the page's words for a language
|
|
58
62
|
* @param {string} [options.version] - mixed into ETags, so a plugin update refreshes open pages
|
|
59
63
|
* @param {number} [options.heartbeatMs]
|
|
60
64
|
*/
|
|
61
|
-
export function createRoutes({ dataDir, people, identity, pollSeconds, presses, seen, watches, messages, version = '', heartbeatMs = HEARTBEAT_MS }) {
|
|
65
|
+
export function createRoutes({ dataDir, people, identity, pollSeconds, presses, seen, arrange, watches, messages, version = '', heartbeatMs = HEARTBEAT_MS }) {
|
|
62
66
|
/** The viewer and their board, or the Response that refuses them. */
|
|
63
67
|
async function access(board) {
|
|
64
68
|
const viewer = await viewerOf(people, identity())
|
|
@@ -106,11 +110,12 @@ export function createRoutes({ dataDir, people, identity, pollSeconds, presses,
|
|
|
106
110
|
if (found.refused) return found.refused
|
|
107
111
|
const { viewer, root, entry, person } = found
|
|
108
112
|
const states = await pressStates(root, id)
|
|
109
|
-
const tag = await boardTag(root, id, `${version}\0${viewer.mode}\0${person ?? ''}\0${await seen.tag(person)}\0${JSON.stringify(states)}`)
|
|
113
|
+
const tag = await boardTag(root, id, `${version}\0${viewer.mode}\0${person ?? ''}\0${await seen.tag(person)}\0${await arrange.tag(person)}\0${JSON.stringify(states)}`)
|
|
110
114
|
if (request.headers.get('if-none-match') === tag) return new Response(null, { status: 304, headers: { ...HEADERS, etag: tag } })
|
|
111
115
|
const seenBoard = await seen.board(person, id)
|
|
112
116
|
const cards = (await readCards(root, id, entry.board)).map(card => decorate(card, states, seenBoard))
|
|
113
|
-
|
|
117
|
+
const arrangement = await arrange.get(person, id)
|
|
118
|
+
return json(200, { id, title: entry.board?.title ?? id, ...(entry.board?.assistant ? { assistant: entry.board.assistant } : {}), ...(arrangement ? { arrangement } : {}), problems: entry.problems, cards }, { etag: tag })
|
|
114
119
|
}
|
|
115
120
|
|
|
116
121
|
async function card(request) {
|
|
@@ -142,16 +147,20 @@ export function createRoutes({ dataDir, people, identity, pollSeconds, presses,
|
|
|
142
147
|
const send = text => { if (open) controller.enqueue(encoder.encode(text)) }
|
|
143
148
|
const data = value => send(`data: ${JSON.stringify(value)}\n\n`)
|
|
144
149
|
let unwatch = () => {}
|
|
150
|
+
let unarrange = () => {}
|
|
145
151
|
const heartbeat = setInterval(() => send(': ping\n\n'), heartbeatMs)
|
|
146
152
|
finish = () => {
|
|
147
153
|
if (!open) return
|
|
148
154
|
open = false
|
|
149
155
|
clearInterval(heartbeat)
|
|
150
156
|
unwatch()
|
|
157
|
+
unarrange()
|
|
151
158
|
request.signal?.removeEventListener?.('abort', finish)
|
|
152
159
|
}
|
|
153
160
|
request.signal?.addEventListener?.('abort', finish)
|
|
154
161
|
send(': connected\n\n')
|
|
162
|
+
// The viewer's own arrangement changing elsewhere (another device) reaches this page too.
|
|
163
|
+
unarrange = arrange.subscribe((who, board) => { if (who === found.person && board === id) data({ type: 'arrange' }) })
|
|
155
164
|
unwatch = await watches.watch(join(found.root, id), async change => {
|
|
156
165
|
if (change.type === 'board') {
|
|
157
166
|
// Viewers may have changed: someone who may no longer see the board hears that and nothing else.
|
|
@@ -202,6 +211,24 @@ export function createRoutes({ dataDir, people, identity, pollSeconds, presses,
|
|
|
202
211
|
return states
|
|
203
212
|
}
|
|
204
213
|
|
|
214
|
+
/** The viewer's own pins and order for a board (`{ board, pinned, order }`), or back to the board's (`{ board, reset: true }`). */
|
|
215
|
+
async function arrangeRoute(request) {
|
|
216
|
+
let body
|
|
217
|
+
try {
|
|
218
|
+
body = await request.json()
|
|
219
|
+
} catch {
|
|
220
|
+
return json(400, { error: 'The body must be JSON.' })
|
|
221
|
+
}
|
|
222
|
+
const board = body?.board
|
|
223
|
+
const arrangement = body?.reset === true ? null : parseArrangement(body)
|
|
224
|
+
if (typeof board !== 'string' || !ID_PATTERN.test(board) || arrangement === undefined) return json(400, { error: 'The body must be { board, pinned: [card], order: [card] } or { board, reset: true }.' })
|
|
225
|
+
const found = await access(board)
|
|
226
|
+
if (found.refused) return found.refused
|
|
227
|
+
if (arrangement === null) await arrange.reset(found.person, board)
|
|
228
|
+
else await arrange.set(found.person, board, arrangement)
|
|
229
|
+
return json(200, { ok: true })
|
|
230
|
+
}
|
|
231
|
+
|
|
205
232
|
async function action(request) {
|
|
206
233
|
let body
|
|
207
234
|
try {
|
|
@@ -257,6 +284,7 @@ export function createRoutes({ dataDir, people, identity, pollSeconds, presses,
|
|
|
257
284
|
{ path: CARD_PATH, methods: ['GET'], requestBody: 'buffered', fetch: card },
|
|
258
285
|
{ path: EVENTS_PATH, methods: ['GET'], requestBody: 'buffered', fetch: events },
|
|
259
286
|
{ path: SEEN_PATH, methods: ['POST'], requestBody: 'buffered', fetch: seenRoute },
|
|
287
|
+
{ path: ARRANGE_PATH, methods: ['POST'], requestBody: 'buffered', fetch: arrangeRoute },
|
|
260
288
|
{ path: LOCALE_PATH, methods: ['GET'], requestBody: 'buffered', fetch: locale },
|
|
261
289
|
{ path: ACTION_PATH, methods: ['POST'], requestBody: 'buffered', fetch: action },
|
|
262
290
|
]
|
package/src/schema.js
CHANGED
|
@@ -169,6 +169,20 @@ function timeWindow(c, value, path) {
|
|
|
169
169
|
if (fromOk && toOk && Date.parse(value.to) <= Date.parse(value.from)) c.fail(join(path, 'to'), 'must be after from')
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
+
/**
|
|
173
|
+
* A card's `raise`: during this window the card floats above the rest of the board. `from` and `until` are each
|
|
174
|
+
* optional, but at least one is needed: a card raised until a time, from a time on (until its producer rewrites it), or
|
|
175
|
+
* between the two. `label` says why in a few words.
|
|
176
|
+
*/
|
|
177
|
+
function raiseWindow(c, value, path) {
|
|
178
|
+
if (!c.object(value, path, [], ['from', 'until', 'label'])) return
|
|
179
|
+
if (!('from' in value) && !('until' in value)) return c.fail(path, 'needs from, until or both')
|
|
180
|
+
const fromOk = !('from' in value) || c.dateTime(value.from, join(path, 'from'))
|
|
181
|
+
const untilOk = !('until' in value) || c.dateTime(value.until, join(path, 'until'))
|
|
182
|
+
if (fromOk && untilOk && 'from' in value && 'until' in value && Date.parse(value.until) <= Date.parse(value.from)) c.fail(join(path, 'until'), 'must be after from')
|
|
183
|
+
c.optional(value, 'label', v => c.label(v, join(path, 'label'), { max: 40 }))
|
|
184
|
+
}
|
|
185
|
+
|
|
172
186
|
/** The optional icon, tone, key and active window of a list or status row; keys must not repeat within a card. */
|
|
173
187
|
function rowLook(c, row, path, keys, { toneRequired = false } = {}) {
|
|
174
188
|
icon(c, row, path)
|
|
@@ -554,7 +568,8 @@ export function isSafeLink(value) {
|
|
|
554
568
|
/** Problems with one card file's parsed content. */
|
|
555
569
|
export function checkCard(card) {
|
|
556
570
|
const c = new Check()
|
|
557
|
-
if (!c.object(card, '', ['type', 'title', 'updatedAt', 'data'], ['staleAfter', 'link', 'stat', 'actions', 'icon', 'tone', 'detail'])) return c.problems
|
|
571
|
+
if (!c.object(card, '', ['type', 'title', 'updatedAt', 'data'], ['staleAfter', 'link', 'stat', 'actions', 'icon', 'tone', 'detail', 'raise'])) return c.problems
|
|
572
|
+
c.optional(card, 'raise', v => raiseWindow(c, v, 'raise'))
|
|
558
573
|
c.optional(card, 'detail', v => checkDetail(c, v, 'detail'))
|
|
559
574
|
if (!c.oneOf(card.type, 'type', CARD_TYPES)) return c.problems
|
|
560
575
|
c.label(card.title, 'title', { max: 120 })
|
package/src/seen.js
CHANGED
|
@@ -11,18 +11,12 @@
|
|
|
11
11
|
* <dataDir>/.seen/<hash of the person>.json
|
|
12
12
|
* { "boards": { "<board>": { "<card>": { "v": "…", "rows": { "<key>": "…" } } } } }
|
|
13
13
|
*/
|
|
14
|
-
import {
|
|
15
|
-
import { mkdir, readFile, rename, stat, writeFile } from 'node:fs/promises'
|
|
16
|
-
import { join } from 'node:path'
|
|
14
|
+
import { createPersonStore, hash } from './person-store.js'
|
|
17
15
|
import { ID_PATTERN, KEY_PATTERN } from './schema.js'
|
|
18
16
|
|
|
19
17
|
const VERSION = /^[A-Za-z0-9_-]{1,32}$/
|
|
20
18
|
const MAX_ROWS = 100
|
|
21
19
|
|
|
22
|
-
function hash(value) {
|
|
23
|
-
return createHash('sha256').update(JSON.stringify(value)).digest('base64url').slice(0, 12)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
20
|
/** The content version of a card, and of each of its keyed rows. */
|
|
27
21
|
export function cardVersions(card) {
|
|
28
22
|
const { updatedAt, ...content } = card
|
|
@@ -65,47 +59,23 @@ export function parseSeen(body) {
|
|
|
65
59
|
return { board: body.board, cards: clean }
|
|
66
60
|
}
|
|
67
61
|
|
|
68
|
-
/** The seen marks of every person, one small file each
|
|
62
|
+
/** The seen marks of every person, one small file each. */
|
|
69
63
|
export function createSeenStore(dir) {
|
|
70
|
-
const
|
|
71
|
-
const queues = new Map()
|
|
72
|
-
|
|
73
|
-
async function readFileOf(person) {
|
|
74
|
-
try {
|
|
75
|
-
const value = JSON.parse(await readFile(fileOf(person), 'utf8'))
|
|
76
|
-
return typeof value?.boards === 'object' && value.boards !== null ? value : { boards: {} }
|
|
77
|
-
} catch {
|
|
78
|
-
return { boards: {} }
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
64
|
+
const store = createPersonStore(dir, () => ({ boards: {} }), value => (typeof value?.boards === 'object' && value.boards !== null ? value : undefined))
|
|
82
65
|
return {
|
|
83
66
|
/** What a person has seen on a board, by card id; undefined when they never looked at it. */
|
|
84
67
|
async board(person, board) {
|
|
85
|
-
return (await
|
|
68
|
+
return (await store.read(person)).boards[board]
|
|
86
69
|
},
|
|
87
70
|
|
|
88
|
-
|
|
89
|
-
async tag(person) {
|
|
90
|
-
const info = await stat(fileOf(person)).catch(() => undefined)
|
|
91
|
-
return info ? `${info.size}:${info.mtimeMs}` : '-'
|
|
92
|
-
},
|
|
71
|
+
tag: store.tag,
|
|
93
72
|
|
|
94
73
|
/** Record cards a person has seen on a board, keeping only cards that still exist (`present`). */
|
|
95
74
|
record(person, board, cards, present) {
|
|
96
|
-
|
|
97
|
-
const previous = queues.get(path) ?? Promise.resolve()
|
|
98
|
-
const next = previous.catch(() => {}).then(async () => {
|
|
99
|
-
const value = await readFileOf(person)
|
|
75
|
+
return store.update(person, value => {
|
|
100
76
|
const kept = Object.fromEntries(Object.entries({ ...value.boards[board], ...cards }).filter(([id]) => present.has(id)))
|
|
101
|
-
value.boards[board]
|
|
102
|
-
await mkdir(dir, { recursive: true, mode: 0o700 })
|
|
103
|
-
const tmp = `${path}.${process.pid}.tmp`
|
|
104
|
-
await writeFile(tmp, JSON.stringify(value), { mode: 0o600 })
|
|
105
|
-
await rename(tmp, path)
|
|
77
|
+
return { ...value, boards: { ...value.boards, [board]: kept } }
|
|
106
78
|
})
|
|
107
|
-
queues.set(path, next)
|
|
108
|
-
return next.finally(() => { if (queues.get(path) === next) queues.delete(path) })
|
|
109
79
|
},
|
|
110
80
|
}
|
|
111
81
|
}
|