From c2297db8793155988bf3784ea57b335fdc534b55 Mon Sep 17 00:00:00 2001 From: Glenn Gore Date: Wed, 25 Nov 2026 09:28:01 -0800 Subject: [PATCH] feat(rooms): a room page can send a chat message (rooms.putMessage) The member portal could add only notes, because putNote is the one write a page has and it requires a title. Keyring shows a record with text and no title as an ordinary chat message, so a portal member could not answer in a room's chat. rooms.putMessage({ hostDid, roomId, body }) writes the text with no title, as the member, through writeMessageAsMember in @openvtc/pnm-core/rooms: kind "message" and writtenAt where the room's profile lists them, else an untitled record, which the reading rules show as a message. Sealed on attributed and private rooms, in the clear on open ones. Same gates as putNote: the page is the community's portal, the room is shared with it, the share's member is used, never the page's, and the write is confirmed in the wallet's window, worded as a message. Signed-off-by: Glenn Gore --- CLAUDE.md | 11 ++- packages/core/CHANGELOG.md | 8 ++ packages/core/src/rooms/act.ts | 35 +++++++ packages/core/tests/rooms.member-name.mjs | 41 +++++++++ packages/extension/src/bridge-protocol.ts | 7 +- packages/extension/src/provider.ts | 5 + packages/extension/src/room-consent.tsx | 15 ++- .../extension/src/room-page-background.ts | 21 +++++ packages/extension/src/room-page-offscreen.ts | 5 + packages/extension/src/room-page-policy.ts | 5 +- packages/extension/tests/room-page.test.mts | 91 ++++++++++++++++++- 11 files changed, 236 insertions(+), 8 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index b1e43973..6ef94c2a 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -1048,7 +1048,7 @@ without checking it is the request that was built (`roomsKeysAct` does). ## A portal page uses rooms through the wallet's own methods (P7b) `window.vtaWallet.rooms.{list, choose, forget, info, browse, read, conversation, -putNote, getFile, putFile}` is how a community's member portal shows a member their rooms +putNote, putMessage, getFile, putFile}` is how a community's member portal shows a member their rooms (VTI `data-rooms-files.md` §8.5). It does **not** loosen "a page is not a member": `requestTask` and `signTrustTask` still refuse the whole `rooms/` family. The methods fix the verb, run the flow in the extension, and return @@ -1098,6 +1098,15 @@ page's `putNote` on an open room writes `rooms/records/put/0.4` with like every host request) — there is no group to seal under — after the same confirmation, which says the note will be readable by the room's host. +**A page sends a chat message with `putMessage`, never a note without a +title.** `writeMessageAsMember` writes the text with no `title`, plus +`kind: "message"` and `writtenAt` where the room's profile lists them — so +every reader shows it as a message, and a room listing neither still shows the +untitled record as one (*Untitled records*, `shownKind`). It is confirmed in the +wallet's window like a note, worded as a message. Keep `putNote`'s title +required: a note and a message are different records, and a page choosing +between them says which. + **An open room's file carries its own key.** With no storage key to derive from, `putRoomFile` mints 32 random bytes for the file, seals the blob exactly as on a sealed tier, and writes `rooms/records/put/0.4` with the manifest — `fileKey` included — in diff --git a/packages/core/CHANGELOG.md b/packages/core/CHANGELOG.md index 4fb846ba..23332cf4 100644 --- a/packages/core/CHANGELOG.md +++ b/packages/core/CHANGELOG.md @@ -8,6 +8,14 @@ For history before this file, see `git log` on `packages/core`. ## [Unreleased] +### Added + +- **`writeMessageAsMember`** writes a chat message to a room as the member: + the text with no `title`, plus `kind: "message"` and `writtenAt` where the + room's profile lists them (`MESSAGE_KIND`). A room listing neither takes it + as an untitled record, which readers show as a message. Sealed on + `attributed` / `private`, in the clear on `open`; create-only. + ### Changed - **BREAKING: a relying-party session step-up no longer takes the session's diff --git a/packages/core/src/rooms/act.ts b/packages/core/src/rooms/act.ts index 895df8eb..dea02a17 100644 --- a/packages/core/src/rooms/act.ts +++ b/packages/core/src/rooms/act.ts @@ -591,6 +591,41 @@ export async function writeMemberNameAsMember( return { principal, key, version }; } +/** + * Write a chat message, as the member: a new record whose text is `body`, and + * no `title`. + * + * Where the room's profile lists `kind`, the record says `kind: "message"`, which + * every reader shows as a message; where it lists `writtenAt`, the record carries + * when it was written. A room listing neither still takes the message as an + * untitled record, which a reader keeping a conversation shows as one + * (*Untitled records*, `shownKind`) — never as a note with a made-up title. + * Sealed and signed by the VTA as the member on `attributed` and `private`, in the + * clear on `open` — as the room says, never assumed. Create-only. + */ +export async function writeMessageAsMember( + room: PersonaRoom, + params: { body: string; now?: () => Date }, +): Promise<{ principal: string; key: string; version: number }> { + if (params.body.trim() === "") throw new RecordWriteRefused("Not written: a message needs some text."); + const listed = (await roomRecordMembersAsMember(room)) ?? []; + const now = (params.now ?? (() => new Date()))().toISOString().replace(/\.\d{3}Z$/, "Z"); + const shape = { + ...(listed.includes("kind") ? { kind: MESSAGE_KIND } : {}), + ...(listed.includes("writtenAt") ? { writtenAt: now } : {}), + }; + const { principal, info } = await roomInfoAsMember(room); + const key = opaqueRecordKey(); + const version = + info.visibility === "open" + ? await putCleartextAsMember(room, { key, body: params.body, ...shape }) + : await writeSealedAsMember(room, { roomId: room.roomId, key, author: principal, body: params.body, ...shape }); + return { principal, key, version }; +} + +/** `RecordKind` for a chat message. */ +export const MESSAGE_KIND = "message"; + /** The member's own name record, as rule 2 gives it from the records its client holds: what a rewrite near the head writes again. */ export interface OwnMemberName { key: string; diff --git a/packages/core/tests/rooms.member-name.mjs b/packages/core/tests/rooms.member-name.mjs index 68df006c..f41278cf 100644 --- a/packages/core/tests/rooms.member-name.mjs +++ b/packages/core/tests/rooms.member-name.mjs @@ -22,6 +22,8 @@ import { RecordWriteRefused, rewriteMemberNameAsMember, writeMemberNameAsMember, + writeMessageAsMember, + shownKind, } from "../dist/rooms/index.js"; const ALICE = "did:webvh:QmAlice:people.example:alice-room"; @@ -456,3 +458,42 @@ test("near the head: nothing is written when the write is within 500, the name i assert.deepEqual(puts(closed.sent), [], "the writer's gate holds for a rewrite too"); }); + +// ── A chat message (writeMessageAsMember) ────────────────────────────────── + +test("message writer: on a sealed tier the body is kind message, the text and writtenAt, no title, signed as the member", async () => { + const r = room({ recordMembers: ["kind", "writtenAt"] }); + const out = await writeMessageAsMember(r.room, { body: "Lunch at one?", now: () => new Date("2026-11-25T12:00:00.456Z") }); + assert.equal(out.principal, ALICE); + const [body] = r.sealed; + assert.deepEqual(Object.keys(body).sort(), ["author", "body", "key", "kind", "roomId", "version", "writtenAt"]); + assert.equal(body.kind, "message"); + assert.equal(body.body, "Lunch at one?"); + assert.equal(body.writtenAt, "2026-11-25T12:00:00Z"); + assert.equal(body.author, ALICE); + assert.equal(puts(r.sent)[0].payload.expectedVersion, 0, "create-only"); + assert.equal(shownKind(body), "message"); +}); + +test("message writer: a room whose profile lists neither still takes it, untitled, which readers show as a message", async () => { + const r = room({ recordMembers: undefined }); + await writeMessageAsMember(r.room, { body: "hi" }); + const [body] = r.sealed; + assert.deepEqual(Object.keys(body).sort(), ["author", "body", "key", "roomId", "version"]); + assert.equal(shownKind(body), "message"); +}); + +test("message writer: on an open room the cleartext is the message, in the clear, never sealed", async () => { + const r = room({ recordMembers: ["kind"], visibility: "open" }); + await writeMessageAsMember(r.room, { body: "hi" }); + const [put] = puts(r.sent); + assert.deepEqual(put.payload.cleartext, { kind: "message", body: "hi" }); + assert.deepEqual(r.sealed, []); +}); + +test("message writer: an empty message is refused before anything is asked", async () => { + const r = room({ recordMembers: ["kind"] }); + await assert.rejects(writeMessageAsMember(r.room, { body: " \n" }), RecordWriteRefused); + assert.deepEqual(r.vtaAsked, []); + assert.deepEqual(r.sent, []); +}); diff --git a/packages/extension/src/bridge-protocol.ts b/packages/extension/src/bridge-protocol.ts index fcc0bb7f..bfeeed44 100644 --- a/packages/extension/src/bridge-protocol.ts +++ b/packages/extension/src/bridge-protocol.ts @@ -2501,7 +2501,9 @@ export type RoomsPageOp = body?: string; description?: string; tags?: string[]; - }; + } + /** A chat message: text and no title (`writeMessageAsMember`). Confirmed like a note. */ + | { op: "putMessage"; hostDid: string; roomId: string; body: string }; export interface RuntimeRoomsPageRequest { type: typeof RUNTIME_ROOMS_PAGE; @@ -3661,7 +3663,8 @@ export type OffscreenRoomsPageOp = body?: string; description?: string; tags?: string[]; - }; + } + | { op: "putMessage"; hostDid: string; roomId: string; member: string; body: string }; export interface OffscreenRoomsPageRequest { target: typeof OFFSCREEN_TARGET; diff --git a/packages/extension/src/provider.ts b/packages/extension/src/provider.ts index 946aad8b..6d131ccc 100644 --- a/packages/extension/src/provider.ts +++ b/packages/extension/src/provider.ts @@ -370,6 +370,10 @@ export interface VtaWalletRooms { description?: string; tags?: string[]; }): Promise<{ key: string; version: number }>; + /** Write a chat message as the member — text, no title — after they confirm it. + * It reads as a message in every reader (`kind: "message"` where the room + * takes it, else an untitled record). */ + putMessage(params: { hostDid: string; roomId: string; body: string }): Promise<{ key: string; version: number }>; /** A record's file, once the whole of it has verified against its author's digest. */ getFile(params: { hostDid: string; @@ -675,6 +679,7 @@ if (!window.vtaWallet) { read: (p) => call("rooms", { op: "read", hostDid: p.hostDid, roomId: p.roomId, key: p.key }), conversation: (p) => call("rooms", { op: "conversation", hostDid: p.hostDid, roomId: p.roomId }), putNote: (p) => call("rooms", { ...p, op: "putNote" }), + putMessage: (p) => call("rooms", { op: "putMessage", hostDid: p.hostDid, roomId: p.roomId, body: p.body }), offers: (p) => call("rooms", { op: "offers", hostDid: p.hostDid }), acceptOffer: (p) => call("rooms", { diff --git a/packages/extension/src/room-consent.tsx b/packages/extension/src/room-consent.tsx index 9f0cafd5..7a0b8b53 100644 --- a/packages/extension/src/room-consent.tsx +++ b/packages/extension/src/room-consent.tsx @@ -219,10 +219,12 @@ function Share({ function Write({ view, decide }: { view: RoomWriteConsentView; decide: (approved: boolean) => void }) { const isFile = "file" in view.what; + const isMessage = "message" in view.what; + const thing = isFile ? "file" : isMessage ? "message" : "note"; return ( <> - Add {isFile ? "a file" : "a note"} to this room as you? + {isMessage ? "Send this message to the room as you?" : `Add a ${thing} to this room as you?`}
{originHost(view.origin)} asks to write to a room, signed as you. Every member of the room will read it as @@ -230,7 +232,7 @@ function Write({ view, decide }: { view: RoomWriteConsentView; decide: (approved
{view.visibility === "open" && (
- This room is open: {isFile ? "the file's record, and the key that opens it, are" : "the note is"}{" "} + This room is open: {isFile ? "the file's record, and the key that opens it, are" : `the ${thing} is`}{" "} stored in the clear, so the room's host can read {isFile ? "the file" : "it"} as well as its members.
)} @@ -258,6 +260,13 @@ function Write({ view, decide }: { view: RoomWriteConsentView; decide: (approved {view.what.mediaType ? ` · ${view.what.mediaType}` : ""} {view.what.title ?
Title: {view.what.title}
: null} + ) : "message" in view.what ? ( + <> + {view.what.excerpt} +
+ {view.what.bodyChars} characters +
+ ) : ( <> {view.what.title} @@ -273,7 +282,7 @@ function Write({ view, decide }: { view: RoomWriteConsentView; decide: (approved - decide(true)}>Add {isFile ? "file" : "note"} + decide(true)}>{isMessage ? "Send message" : `Add ${thing}`} ); diff --git a/packages/extension/src/room-page-background.ts b/packages/extension/src/room-page-background.ts index c449f534..d7eedf96 100644 --- a/packages/extension/src/room-page-background.ts +++ b/packages/extension/src/room-page-background.ts @@ -307,6 +307,27 @@ export class RoomPageGates { ...(p.tags !== undefined ? { tags: p.tags } : {}), }); } + case "putMessage": { + const { vta } = await this.atPortal(origin, p.hostDid, { needReach: true }); + const member = await this.granted(origin, p.hostDid, vta, p.roomId); + const info = (await this.offscreen(vta, { + op: "info", + hostDid: p.hostDid, + roomId: p.roomId, + member, + })) as PageRoomInfoView; + const decision = await this.deps.prompt({ + kind: "write", + origin, + hostDid: p.hostDid, + roomId: p.roomId, + member: info.member, + visibility: info.visibility, + what: { message: true, excerpt: excerpt(p.body), bodyChars: [...p.body].length }, + }); + if (!decision.approved) throw new RoomPageRefusal("the member declined to send this message", "declined"); + return this.offscreen(vta, { op: "putMessage", hostDid: p.hostDid, roomId: p.roomId, member, body: p.body }); + } } } diff --git a/packages/extension/src/room-page-offscreen.ts b/packages/extension/src/room-page-offscreen.ts index b1d143be..a4115443 100644 --- a/packages/extension/src/room-page-offscreen.ts +++ b/packages/extension/src/room-page-offscreen.ts @@ -36,6 +36,7 @@ import { readSealedVersion, roomProfileView, roomsKeysProfileRead, + writeMessageAsMember, writeSealedAsMember, type PersonaRoom, } from "@openvtc/pnm-core/rooms"; @@ -284,6 +285,10 @@ export async function runRoomsPageOp( }); return { key, version }; } + case "putMessage": { + const { key, version } = await writeMessageAsMember(await room(op.roomId, op.member), { body: op.body }); + return { key, version }; + } } } diff --git a/packages/extension/src/room-page-policy.ts b/packages/extension/src/room-page-policy.ts index 2d65ad33..45c09726 100644 --- a/packages/extension/src/room-page-policy.ts +++ b/packages/extension/src/room-page-policy.ts @@ -346,7 +346,7 @@ function tags(v: unknown): string | null { return null; } -const OPS = new Set(["list", "choose", "forget", "info", "browse", "read", "conversation", "putNote", "offers", "acceptOffer"]); +const OPS = new Set(["list", "choose", "forget", "info", "browse", "read", "conversation", "putNote", "putMessage", "offers", "acceptOffer"]); /** Why a page's room call is malformed, or `null` when it is well formed. Checked before anything else. */ export function roomsPageParamsRefusal(raw: unknown): string | null { @@ -377,6 +377,8 @@ export function roomsPageParamsRefusal(raw: unknown): string | null { text(op.description, "description", MAX_DESCRIPTION) ?? tags(op.tags) ); + case "putMessage": + return text(op.body, "body", MAX_BODY, true); default: return null; } @@ -452,6 +454,7 @@ export interface RoomWriteConsentView { visibility: string; what: | { note: true; title: string; excerpt: string; bodyChars: number } + | { message: true; excerpt: string; bodyChars: number } | { file: true; name: string; size: number; mediaType?: string; title?: string }; } diff --git a/packages/extension/tests/room-page.test.mts b/packages/extension/tests/room-page.test.mts index 60c24bf2..60983450 100644 --- a/packages/extension/tests/room-page.test.mts +++ b/packages/extension/tests/room-page.test.mts @@ -74,6 +74,9 @@ test("a page call is checked for shape before anything else", () => { roomsPageParamsRefusal({ op: "putNote", hostDid: HOST, roomId: ROOM, title: "t", tags: new Array(33).fill("x") })!, /at most 32/, ); + assert.equal(roomsPageParamsRefusal({ op: "putMessage", hostDid: HOST, roomId: ROOM, body: "hi" }), null); + assert.match(roomsPageParamsRefusal({ op: "putMessage", hostDid: HOST, roomId: ROOM, body: " \n" })!, /body is required/); + assert.match(roomsPageParamsRefusal({ op: "putMessage", hostDid: HOST, roomId: ROOM })!, /body is required/); assert.match(roomFilesPageOpenRefusal({ kind: "put", hostDid: HOST, roomId: ROOM, name: "a", size: 65 * 1024 * 1024 })!, /more than/); assert.equal(roomFilesPageOpenRefusal({ kind: "get", hostDid: HOST, roomId: ROOM, key: "k" }), null); }); @@ -420,6 +423,86 @@ test("every note is confirmed, showing who signs and what is written", async () assert.ok(h.asked.some((a) => a.op === "putNote")); }); +test("every message is confirmed as a message, and nothing is written until it is", async () => { + const h = harness(); + h.answer = { approved: true, selectedRooms: [as(ROOM)] }; + await h.gates.handle(PORTAL, { op: "choose", hostDid: HOST }); + + h.answer = { approved: false }; + const denied = await h.gates.handle(PORTAL, { op: "putMessage", hostDid: HOST, roomId: ROOM, body: "Lunch at one?" }); + assert.deepEqual(denied, { ok: false, error: "the member declined to send this message", code: "declined" }); + assert.ok(!h.asked.some((a) => a.op === "putMessage"), "nothing was written"); + const view = h.prompts.at(-1) as RoomWriteConsentView; + assert.equal(view.kind, "write"); + assert.equal(view.member, MEMBER); + assert.deepEqual(view.what, { message: true, excerpt: "Lunch at one?", bodyChars: 13 }); + + h.answer = { approved: true }; + const ok = await h.gates.handle(PORTAL, { op: "putMessage", hostDid: HOST, roomId: ROOM, body: "Lunch at one?" }); + assert.ok(ok.ok); + assert.ok(h.asked.some((a) => a.op === "putMessage")); +}); + +test("a message sent before the room is shared is refused, and nothing is asked", async () => { + const h = harness(); + const res = await h.gates.handle(PORTAL, { op: "putMessage", hostDid: HOST, roomId: ROOM, body: "hi" }); + assert.equal((res as { code?: string }).code, "notShared"); + assert.ok(!h.prompts.some((p) => (p as { kind?: string }).kind === "write"), "no write prompt"); + assert.ok(!h.asked.some((a) => a.op === "putMessage")); +}); + +test("the offscreen message for an open room is a message in the clear: kind and writtenAt where the room takes them, no title", async () => { + for (const [recordMembers, expected] of [ + [["kind", "writtenAt"], ["body", "kind", "writtenAt"]], + [undefined, ["body"]], + ] as const) { + const sent: Record[] = []; + const out = await runRoomsPageOp( + { + agent: async () => ({ + holderDid: "did:key:zWallet", + sender: { + async send(envelope: { type: string; payload: { document: Record } }) { + if (envelope.type.includes("rooms/keys/profile/read/")) { + return (recordMembers === undefined + ? { roomId: OPEN_ROOM, status: "absent" } + : { roomId: OPEN_ROOM, status: "found", profile: { name: "R", recordMembers } }) as Res; + } + if (!envelope.type.includes("rooms/keys/act/")) throw new Error(`unexpected ${envelope.type}`); + const doc = structuredClone(envelope.payload.document); + doc.proof = { type: "DataIntegrityProof" }; + return { principal: doc.issuer, document: doc } as Res; + }, + } as never, + }), + portal: async () => ({ portalOrigin: PORTAL, endpoint: `${PORTAL}/v1/trust-tasks` }), + completed: () => ({ + async sendCompleted(doc: Record) { + sent.push(doc); + const type = String(doc.type); + const payload = doc.payload as { roomId: string; key?: string }; + if (type.includes("rooms/info/")) { + return { roomId: payload.roomId, visibility: "open", epoch: 1, limits: {}, usage: {} } as Res; + } + if (type.includes("rooms/records/put/")) return { key: payload.key, version: 4 } as Res; + throw new Error(`unexpected ${type}`); + }, + }), + }, + { vtaDid: VTA }, + { op: "putMessage", hostDid: HOST, roomId: OPEN_ROOM, member: MEMBER, body: "Lunch at one?" }, + ); + const put = sent.find((d) => String(d.type).includes("rooms/records/put/"))!; + assert.ok(put, "a put reached the community"); + assert.equal(put.issuer, MEMBER, "as the member"); + const cleartext = (put.payload as { cleartext: Record }).cleartext; + assert.deepEqual(Object.keys(cleartext).sort(), [...expected]); + assert.equal(cleartext.body, "Lunch at one?"); + if (recordMembers) assert.equal(cleartext.kind, "message"); + assert.deepEqual(out, { key: (put.payload as { key: string }).key, version: 4 }); + } +}); + // ── Files ─────────────────────────────────────────────────────────────────── class FakePort implements PortLike { @@ -805,12 +888,18 @@ test("a share names its member, and every request for the page carries it", asyn await h.gates.handle(PORTAL, { op: "conversation", hostDid: HOST, roomId: ROOM, member: "did:key:zSomeoneElse", extra: 1 }); h.answer = { approved: true }; await h.gates.handle(PORTAL, { op: "putNote", hostDid: HOST, roomId: ROOM, title: "t" }); + await h.gates.handle(PORTAL, { op: "putMessage", hostDid: HOST, roomId: ROOM, body: "hi", member: "did:key:zSomeoneElse" }); assert.deepEqual( h.asked.find((a) => a.op === "conversation"), { op: "conversation", hostDid: HOST, roomId: ROOM, member: MEMBER }, "the share's member, never the page's, and nothing else the page added", ); - for (const op of ["info", "browse", "read", "conversation", "putNote"]) { + assert.deepEqual( + h.asked.find((a) => a.op === "putMessage"), + { op: "putMessage", hostDid: HOST, roomId: ROOM, member: MEMBER, body: "hi" }, + "a message is written as the share's member, with nothing the page added", + ); + for (const op of ["info", "browse", "read", "conversation", "putNote", "putMessage"]) { const asked = h.asked.filter((a) => a.op === op); assert.ok(asked.length > 0, op); assert.ok(asked.every((a) => a.member === MEMBER), `${op} names the member`);