Skip to content

Commit b4f444b

Browse files
committed
Preserve rich content across client edits
Signed-off-by: Zach Marley <[email protected]>
1 parent 60ed428 commit b4f444b

4 files changed

Lines changed: 221 additions & 8 deletions

File tree

‎docs/channels.md‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,12 @@ Channels supports plain-text Markdown authoring with a shared durable outbox and
203203
Channel and thread messages render CommonMark plus GFM headings, emphasis, lists, quotes,
204204
tables, task lists, strikethrough and code, while preserving chat-style single line breaks.
205205
Only credential-free HTTPS links are active; raw HTML is ignored and inline remote images
206-
are not loaded. Existing image Markdown is projected as an attachment instead. Custom emoji
207-
remain event-local and are not substituted inside links or code. Authenticated live traffic
208-
reconciles through the same session. Channel creation and composer preview/toolbars are not
209-
implemented. Reply counts open a bounded thread view; attachments are links. Routine freshness
210-
labels are not shown; Conversation options → Diagnostics
206+
are not loaded. Existing image Markdown is projected as an attachment instead. Spoiler markup
207+
renders as ordinary literal text; there is no reveal interaction. Kind 40008 rich-content diff
208+
rows are not admitted into channel timelines. Custom emoji remain event-local and are not
209+
substituted inside links or code. Authenticated live traffic reconciles through the same session.
210+
Channel creation and composer preview/toolbars are not implemented. Reply counts open a bounded
211+
thread view; attachments are links. Routine freshness labels are not shown; Conversation options → Diagnostics
211212
exposes refresh, outbox inspection and timings. Packaged builds do not
212213
include the development relay broker. GitHub fetches public data only; signed-in
213214
GitHub actions remain on GitHub. A saved-groups/stars failure keeps its specific
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { createMessages } from "./messages";
3+
import { createOutbox, type OutboxStorage } from "./outbox";
4+
import type { EventData } from "./events";
5+
import { foldMessages } from "./fold";
6+
import { keypair, message, signed } from "./testing";
7+
8+
const relay = keypair(),
9+
alice = keypair();
10+
const channel = "compat";
11+
const imageUrl = "https://relay.test/media/photo.png";
12+
const fileUrl = "https://relay.test/media/report.pdf";
13+
const imageIMeta = () => [
14+
"imeta",
15+
`url ${imageUrl}`,
16+
"m image/png",
17+
"dim 640x480",
18+
];
19+
const fileIMeta = () => [
20+
"imeta",
21+
`url ${fileUrl}`,
22+
"m application/pdf",
23+
"size 1234",
24+
];
25+
26+
function memoryStorage(): OutboxStorage {
27+
return { load: () => [], save: () => {} };
28+
}
29+
30+
function localOutbox(viewer: string) {
31+
const writer = {
32+
sign: vi.fn(async (event) => signed(alice, event)),
33+
publish: vi.fn(async () => {}),
34+
kinds: [9, 40003],
35+
};
36+
return createOutbox(viewer, writer, memoryStorage()).outbox;
37+
}
38+
39+
function fold(events: EventData | readonly EventData[]) {
40+
return foldMessages(
41+
channel,
42+
relay.pubkey,
43+
Array.isArray(events) ? events : [events],
44+
)[0];
45+
}
46+
47+
describe("cross-client rich content compatibility", () => {
48+
it.each([
49+
{
50+
name: "old Markdown image",
51+
content: `Before ![photo](${imageUrl}) after`,
52+
tags: [],
53+
expectedContent: "Before after",
54+
expectedAttachments: [{ url: imageUrl, kind: "image" }],
55+
},
56+
{
57+
name: "new imeta image",
58+
content: "Before attachment after",
59+
tags: [imageIMeta()],
60+
expectedContent: "Before attachment after",
61+
expectedAttachments: [
62+
{
63+
url: imageUrl,
64+
kind: "image",
65+
mime: "image/png",
66+
name: "photo.png",
67+
dimensions: { width: 640, height: 480 },
68+
},
69+
],
70+
},
71+
{
72+
name: "new imeta file with attachment link label",
73+
content: `Before [Quarterly Report](${fileUrl}) after`,
74+
tags: [fileIMeta()],
75+
expectedContent: "Before after",
76+
expectedAttachments: [
77+
{
78+
url: fileUrl,
79+
kind: "file",
80+
mime: "application/pdf",
81+
size: 1234,
82+
name: "Quarterly Report",
83+
},
84+
],
85+
},
86+
])(
87+
"folds representative $name content",
88+
({ content, tags, expectedContent, expectedAttachments }) => {
89+
const row = fold(message(alice, channel, content, 10, tags));
90+
expect(row?.content).toBe(expectedContent);
91+
expect(row?.attachments).toEqual(expectedAttachments);
92+
},
93+
);
94+
95+
it("uses the latest authorized edit imeta as the attachment metadata source", () => {
96+
const original = message(
97+
alice,
98+
channel,
99+
`Original [Report](${fileUrl})`,
100+
10,
101+
[fileIMeta()],
102+
);
103+
const edit = signed(alice, {
104+
kind: 40003,
105+
content: `Edited ![photo](${imageUrl})`,
106+
created_at: 11,
107+
tags: [["h", channel], ["e", original.id], imageIMeta()],
108+
});
109+
const spoofedLaterEdit = signed(keypair(), {
110+
kind: 40003,
111+
content: "Spoofed",
112+
created_at: 12,
113+
tags: [["h", channel], ["e", original.id], fileIMeta()],
114+
});
115+
116+
expect(fold([original, edit, spoofedLaterEdit])).toMatchObject({
117+
content: "Edited",
118+
edited: true,
119+
attachments: [
120+
{
121+
url: imageUrl,
122+
kind: "image",
123+
mime: "image/png",
124+
name: "photo.png",
125+
dimensions: { width: 640, height: 480 },
126+
},
127+
],
128+
});
129+
});
130+
131+
it("preserves original imeta attachments on text-only edits without imeta", () => {
132+
const original = message(alice, channel, "Original attachment", 10, [
133+
imageIMeta(),
134+
]);
135+
const edit = signed(alice, {
136+
kind: 40003,
137+
content: "Edited text",
138+
created_at: 11,
139+
tags: [
140+
["h", channel],
141+
["e", original.id],
142+
],
143+
});
144+
145+
expect(fold([original, edit])).toMatchObject({
146+
content: "Edited text",
147+
edited: true,
148+
attachments: [
149+
{
150+
url: imageUrl,
151+
kind: "image",
152+
mime: "image/png",
153+
name: "photo.png",
154+
dimensions: { width: 640, height: 480 },
155+
},
156+
],
157+
});
158+
});
159+
160+
it("does not admit kind 40008 diff rows into the channel fold", () => {
161+
const diff = signed(alice, {
162+
kind: 40008,
163+
content: JSON.stringify({ patches: [] }),
164+
created_at: 10,
165+
tags: [["h", channel]],
166+
});
167+
168+
expect(foldMessages(channel, relay.pubkey, [diff])).toEqual([]);
169+
});
170+
171+
it("renders spoiler syntax as ordinary literal text", () => {
172+
const row = fold(message(alice, channel, "Before ||secret|| after", 10));
173+
174+
expect(row?.content).toBe("Before ||secret|| after");
175+
expect(row?.attachments).toEqual([]);
176+
});
177+
178+
it("emits original imeta tags on text edits so old clients retain attachments", () => {
179+
const original = message(alice, channel, `Original ${imageUrl}`, 10, [
180+
imageIMeta(),
181+
]);
182+
const outbox = localOutbox(alice.pubkey);
183+
const messages = createMessages(
184+
outbox,
185+
alice.pubkey,
186+
(id) => (id === original.id ? original : undefined),
187+
() => [],
188+
() => {},
189+
);
190+
191+
const editId = messages.edit(original.id, "Edited text");
192+
const edit = outbox
193+
.snapshot()
194+
.find((item) => item.event.id === editId)?.event;
195+
196+
expect(edit?.tags).toEqual(
197+
expect.arrayContaining([
198+
["h", channel],
199+
["e", original.id],
200+
imageIMeta(),
201+
]),
202+
);
203+
});
204+
});

‎src/features/relay/fold.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,10 @@ export function foldMessages(
253253
const body = objectBody(content);
254254
if (typeof body?.content === "string") content = body.content;
255255
}
256-
const imetaUrls = imetaAttachmentUrls(event);
256+
const attachmentEvent = edits[0]?.tags.some(([name]) => name === "imeta")
257+
? edits[0]
258+
: event;
259+
const imetaUrls = imetaAttachmentUrls(attachmentEvent);
257260
// Every CommonMark image begins with `![`, and every attachment title link
258261
// needs an imeta URL match; avoid parsing ordinary messages.
259262
const projected =
@@ -289,7 +292,7 @@ export function foldMessages(
289292
),
290293
]),
291294
attachments: Object.freeze(
292-
parseAttachments(event, projected.urls, attachmentNames),
295+
parseAttachments(attachmentEvent, projected.urls, attachmentNames),
293296
),
294297
emoji: emojiTags(
295298
edits[0]?.tags.some(([name]) => name === "emoji") ? edits[0] : event,

‎src/features/relay/messages.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ export function createMessages(
7777
return writer(40003, channelId).send({
7878
kind: 40003,
7979
content: text(content),
80-
tags: [["h", channelId], ["e", messageId], ...emojiTags(content)],
80+
tags: [
81+
["h", channelId],
82+
["e", messageId],
83+
...original.tags.filter((tag) => tag[0] === "imeta"),
84+
...emojiTags(content),
85+
],
8186
});
8287
},
8388
react(messageId: string, content: string) {

0 commit comments

Comments
 (0)