git clone https://garage44.eu/jeroen/midas.git.. /web/src/lib/feed.ts
import type {WorkspaceEvent} from '@/lib/types'
/** The event kinds the Messages page is made of (collab.rs). */
export const FEED_KINDS = ['note', 'notes_taken', 'note_expired', 'decision_asked', 'decision_answered']
/** The person, as a message's sender or recipient: questions go to them, answers come from them. */
export const PERSON = 'you'
/**
* One message between sessions, from the hub, or between a session and the
* person. `read`: true once its recipient took it, false while it waits, null
* where reading does not apply (a question, an answer). `expired`: why it was
* thrown away unread.
*/
export interface Message {
key: string
id: string | null
ts: string
from: string
to: string
text: string
/** say, nudge, overlap, …; `question` and `answer` for decisions. */
kind: string
read: boolean | null
expired: string | null
}
/** A worktree and its landing gate are one session: a note to either is read when either takes its notes. */
const base = (owner: string) => owner.replace(/^gate-/, '')
/**
* The feed, oldest first. A note counts as read when a later `notes_taken` names
* its id; from a hub that does not give ids yet, when its recipient took its
* notes at or after the time it was left.
*/
export function buildFeed(events: WorkspaceEvent[]): Message[] {
const takenIds = new Set<string>()
const lastTaken = new Map<string, string>()
/** The last time each recipient took notes from a hub that names them by id. */
const lastTakenById = new Map<string, string>()
const expired = new Map<string, string>()
for (const e of events) {
if (e.kind === 'notes_taken') {
const who = base(String(e.actor ?? ''))
if ((lastTaken.get(who) ?? '') < e.ts) lastTaken.set(who, e.ts)
if (Array.isArray(e.ids)) {
for (const id of e.ids as string[]) takenIds.add(id)
if ((lastTakenById.get(who) ?? '') < e.ts) lastTakenById.set(who, e.ts)
}
} else if (e.kind === 'note_expired' && e.id) {
expired.set(String(e.id), String(e.reason ?? 'expired'))
}
}
const out: Message[] = []
events.forEach((e, i) => {
const id = e.id != null ? String(e.id) : null
if (e.kind === 'note') {
const to = String(e.to ?? '')
const gone = id ? (expired.get(id) ?? null) : null
// Taken since by a hub that names what it handed over: by id. Otherwise by time.
const byId = id != null && (lastTakenById.get(base(to)) ?? '') >= e.ts
const read = gone ? false : byId ? takenIds.has(id) : (lastTaken.get(base(to)) ?? '') >= e.ts
out.push({key: id ?? `n${i}`, id, ts: e.ts, from: String(e.actor ?? 'hub'), to, text: String(e.text ?? ''), kind: String(e.note_kind ?? 'say'), read, expired: gone})
} else if (e.kind === 'decision_asked') {
const options = (e.options as string[] | undefined) ?? []
const text = `${e.question ?? ''}${options.length ? `\n(${options.join(' / ')})` : ''}`
out.push({key: `q${id ?? i}`, id, ts: e.ts, from: String(e.actor ?? ''), to: PERSON, text, kind: 'question', read: null, expired: null})
} else if (e.kind === 'decision_answered') {
const asked = events.find((x) => x.kind === 'decision_asked' && x.id === e.id)
const note = e.note ? ` — ${e.note}` : ''
out.push({key: `a${id ?? i}`, id, ts: e.ts, from: PERSON, to: String(asked?.actor ?? ''), text: `${id ?? ''}: ${e.answer ?? ''}${note}`, kind: 'answer', read: null, expired: null})
}
})
return out
}
/** Messages to or from one session (its landing gate's too). */
export function involving(feed: Message[], session: string): Message[] {
if (!session) return feed
return feed.filter((m) => base(m.from) === session || base(m.to) === session)
}