Skip to content

Commit 137343a

Browse files
Duncanwpfleger96
andcommitted
fix(timeline): place message_deleted tombstone at original message position
When a moderator deletes a message the relay emits a kind:40099 system message at the current timestamp. The tombstone was appearing appended at the end of the visible timeline rather than in-place at the original message's position (Will ruling: replace in place). Fix: in formatTimelineMessages, before mapping a KIND_SYSTEM_MESSAGE event to a TimelineMessage, parse its content. If type is message_deleted and target_event_id is present, look up the target in timelineEventsById and use its created_at for both the TimelineMessage createdAt and time fields. Falls back to the tombstone's own created_at when the target is not in the current window. Tests: three new cases in formatTimelineMessages.test.mjs — in-place positioning when target is present, graceful fallback when target is absent, and no repositioning for other system message types. Co-authored-by: Will Pfleger <[email protected]> Signed-off-by: Will Pfleger <[email protected]>
1 parent 81bc027 commit 137343a

2 files changed

Lines changed: 167 additions & 2 deletions

File tree

‎desktop/src/features/messages/lib/formatTimelineMessages.test.mjs‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,3 +773,143 @@ test("verified agent owner may publish a suppression edit", () => {
773773
true,
774774
);
775775
});
776+
777+
// ---------------------------------------------------------------------------
778+
// Tombstone in-place: message_deleted system messages use the target's
779+
// created_at rather than the tombstone's own timestamp.
780+
// ---------------------------------------------------------------------------
781+
782+
const RELAY_SECRET_INPLACE = new Uint8Array(32).fill(9);
783+
const RELAY_PUBKEY_INPLACE = getPublicKey(RELAY_SECRET_INPLACE);
784+
const HEX64_ORIGINAL = "cc".repeat(32);
785+
const HEX64_TOMBSTONE = "dd".repeat(32);
786+
const ORIGINAL_TS = 1_700_000_000;
787+
const TOMBSTONE_TS = 1_700_100_000; // 27h later — would appear far after original
788+
789+
function originalMessage(overrides = {}) {
790+
return {
791+
id: HEX64_ORIGINAL,
792+
pubkey: PUBKEY_A,
793+
kind: 9,
794+
created_at: ORIGINAL_TS,
795+
content: "the message that gets deleted",
796+
tags: [["h", CHANNEL_ID]],
797+
sig: "sig",
798+
...overrides,
799+
};
800+
}
801+
802+
function deletionMarker(targetId, overrides = {}) {
803+
return {
804+
id: `${HEX64_TOMBSTONE.slice(0, 62)}ee`,
805+
pubkey: RELAY_PUBKEY_INPLACE,
806+
kind: 9005,
807+
created_at: TOMBSTONE_TS,
808+
content: "",
809+
tags: [
810+
["h", CHANNEL_ID],
811+
["e", targetId],
812+
],
813+
sig: "sig",
814+
...overrides,
815+
};
816+
}
817+
818+
function tombstoneSystemMessage(targetId, publicReason, overrides = {}) {
819+
return {
820+
id: HEX64_TOMBSTONE,
821+
pubkey: RELAY_PUBKEY_INPLACE,
822+
kind: 40099,
823+
created_at: TOMBSTONE_TS,
824+
content: JSON.stringify({
825+
type: "message_deleted",
826+
actor: RELAY_PUBKEY_INPLACE,
827+
target_event_id: targetId,
828+
public_reason: publicReason,
829+
}),
830+
tags: [["h", CHANNEL_ID]],
831+
sig: "sig",
832+
...overrides,
833+
};
834+
}
835+
836+
test("message_deleted tombstone adopts target's created_at (in-place position)", () => {
837+
// Mutation evidence:
838+
// - Remove the effectiveCreatedAt override → tombstone.createdAt equals
839+
// TOMBSTONE_TS instead of ORIGINAL_TS → RED.
840+
const events = [
841+
originalMessage(),
842+
deletionMarker(HEX64_ORIGINAL),
843+
tombstoneSystemMessage(HEX64_ORIGINAL, "spam"),
844+
];
845+
const messages = formatTimelineMessages(events, null, undefined, null);
846+
847+
// The original message is filtered out by the kind:9005 deletion marker.
848+
assert.equal(
849+
messages.find((m) => m.id === HEX64_ORIGINAL),
850+
undefined,
851+
"original message must be filtered out",
852+
);
853+
854+
// The tombstone system message is present and positioned at the original's
855+
// timestamp, not at the time the tombstone was emitted.
856+
const tombstone = messages.find((m) => m.id === HEX64_TOMBSTONE);
857+
assert.ok(
858+
tombstone !== undefined,
859+
"tombstone system message must be present",
860+
);
861+
assert.equal(
862+
tombstone.createdAt,
863+
ORIGINAL_TS,
864+
"tombstone must use the original message's created_at for in-place positioning",
865+
);
866+
assert.notEqual(
867+
tombstone.createdAt,
868+
TOMBSTONE_TS,
869+
"tombstone must NOT use its own creation timestamp",
870+
);
871+
});
872+
873+
test("message_deleted tombstone falls back to its own created_at when target is absent", () => {
874+
// When the original message is not in the event window (e.g. it was deleted
875+
// long ago and is no longer paginated in), the tombstone uses its own
876+
// created_at. This verifies the graceful fallback branch.
877+
const events = [
878+
// Only the deletion marker and tombstone — no original message event.
879+
deletionMarker(HEX64_ORIGINAL),
880+
tombstoneSystemMessage(HEX64_ORIGINAL, "spam"),
881+
];
882+
const messages = formatTimelineMessages(events, null, undefined, null);
883+
884+
const tombstone = messages.find((m) => m.id === HEX64_TOMBSTONE);
885+
assert.ok(tombstone !== undefined, "tombstone must still render");
886+
assert.equal(
887+
tombstone.createdAt,
888+
TOMBSTONE_TS,
889+
"tombstone must use its own created_at when target is absent",
890+
);
891+
});
892+
893+
test("non-message_deleted system message is not repositioned", () => {
894+
// Other system message types (join, leave, etc.) must keep their own
895+
// created_at — the in-place logic is strictly scoped to message_deleted.
896+
const joinSystemMsg = {
897+
id: HEX64_TOMBSTONE,
898+
pubkey: RELAY_PUBKEY_INPLACE,
899+
kind: 40099,
900+
created_at: TOMBSTONE_TS,
901+
content: JSON.stringify({
902+
type: "member_joined",
903+
actor: PUBKEY_A,
904+
}),
905+
tags: [["h", CHANNEL_ID]],
906+
sig: "sig",
907+
};
908+
const events = [joinSystemMsg];
909+
const [msg] = formatTimelineMessages(events, null, undefined, null);
910+
assert.equal(
911+
msg.createdAt,
912+
TOMBSTONE_TS,
913+
"non-tombstone system messages must not be repositioned",
914+
);
915+
});

‎desktop/src/features/messages/lib/formatTimelineMessages.ts‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,35 @@ export function formatTimelineMessages(
475475
const authorProfile = profiles?.[authorPubkey.toLowerCase()];
476476
const isAgent = role === "bot" || authorProfile?.isAgent === true;
477477
const ownerPubkey = isAgent ? (authorProfile?.ownerPubkey ?? null) : null;
478+
// Tombstone in-place: a `message_deleted` system message should appear at
479+
// the original message's position in the timeline, not appended at the
480+
// time the moderator acted. Look up the target's `created_at` and use it
481+
// so the tombstone slots into the original message's chronological slot.
482+
// Falls back to the tombstone's own `created_at` when the target is not
483+
// in the current window (e.g. deleted message scrolled out of view).
484+
const effectiveCreatedAt = (() => {
485+
if (event.kind !== KIND_SYSTEM_MESSAGE) return event.created_at;
486+
try {
487+
const payload = JSON.parse(event.content) as {
488+
type?: string;
489+
target_event_id?: string;
490+
};
491+
if (
492+
payload.type === "message_deleted" &&
493+
typeof payload.target_event_id === "string"
494+
) {
495+
const target = timelineEventsById.get(payload.target_event_id);
496+
if (target) return target.created_at;
497+
}
498+
} catch {
499+
// Non-JSON or unexpected shape — fall through.
500+
}
501+
return event.created_at;
502+
})();
478503
return {
479504
id: event.id,
480505
renderKey: event.localKey ?? event.id,
481-
createdAt: event.created_at,
506+
createdAt: effectiveCreatedAt,
482507
pubkey: authorPubkey,
483508
signerPubkey: normalizePubkey(event.pubkey),
484509
author,
@@ -502,7 +527,7 @@ export function formatTimelineMessages(
502527
role === "bot"
503528
? respondToLookup?.get(authorPubkey.toLowerCase())
504529
: undefined,
505-
time: formatTime(event.created_at),
530+
time: formatTime(effectiveCreatedAt),
506531
body: edit ? edit.content : event.content,
507532
parentId: thread.parentId,
508533
rootId: thread.rootId,

0 commit comments

Comments
 (0)