Integrate bugbash/quinn-edit-mentions: notify newly-added mentions on message edit (8ace8eed)

Co-authored-by: Tyler Longwell <tlongwell@block.xyz>
Signed-off-by: Tyler Longwell <tlongwell@block.xyz>

* commit '2981f4ccf':
  Fix edit-to-add-mention not notifying newly-mentioned party (8ace8eed)
This commit is contained in:
npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d
2026-07-16 20:39:41 -04:00
11 changed files with 225 additions and 11 deletions

View File

@@ -76,7 +76,11 @@ export type ChannelPaneProps = {
onCloseThread: () => void;
onDelete?: (message: TimelineMessage) => void;
onEdit?: (message: TimelineMessage) => void;
onEditSave?: (content: string, mediaTags?: string[][]) => Promise<void>;
onEditSave?: (
content: string,
mediaTags?: string[][],
mentionPubkeys?: string[],
) => Promise<void>;
onMarkUnread?: (message: TimelineMessage) => void;
onMarkRead?: (message: TimelineMessage) => void;
onExpandThreadReplies: (message: TimelineMessage) => void;

View File

@@ -144,13 +144,22 @@ export function useChannelPaneHandlers({
);
const handleEditSave = React.useCallback(
async (content: string, mediaTags?: string[][]) => {
async (
content: string,
mediaTags?: string[][],
mentionPubkeys?: string[],
) => {
const eventId = editTargetIdRef.current;
if (!eventId) {
return;
}
await editMutateRef.current({ eventId, content, mediaTags });
await editMutateRef.current({
eventId,
content,
mediaTags,
mentionPubkeys,
});
setEditTargetId(null);
},
[setEditTargetId],

View File

@@ -685,9 +685,12 @@ export function useEditMessageMutation(channel: Channel | null) {
eventId: string;
content: string;
mediaTags?: string[][];
// Pubkeys of mentions *newly added* by this edit, diffed at the composer.
// Only these receive a `p` tag so a typo-fix edit re-wakes nobody.
mentionPubkeys?: string[];
}
>({
mutationFn: async ({ eventId, content, mediaTags }) => {
mutationFn: async ({ eventId, content, mediaTags, mentionPubkeys }) => {
if (!channel) {
throw new Error("No channel selected.");
}
@@ -698,7 +701,14 @@ export function useEditMessageMutation(channel: Channel | null) {
// guard rejects any non-imeta prefix), mirroring the send path.
const { mediaTags: imetaTags, emojiTags } = splitOutgoingTags(mediaTags);
await editMessage(channel.id, eventId, content, imetaTags, emojiTags);
await editMessage(
channel.id,
eventId,
content,
imetaTags,
emojiTags,
mentionPubkeys,
);
},
onSuccess: (_data, { eventId, content, mediaTags }) => {
if (!channel) {

View File

@@ -0,0 +1,46 @@
import assert from "node:assert/strict";
import test from "node:test";
import { diffAddedMentionPubkeys } from "./threading.ts";
const ALICE = "a".repeat(64);
const BOB = "b".repeat(64);
const SELF = "c".repeat(64);
test("returns mentions the edit newly adds", () => {
// Original mentioned Alice; edit adds Bob.
assert.deepEqual(diffAddedMentionPubkeys([ALICE], [ALICE, BOB], SELF), [BOB]);
});
test("typo-fix edit with unchanged mentions re-wakes nobody", () => {
assert.deepEqual(diffAddedMentionPubkeys([ALICE], [ALICE], SELF), []);
});
test("adding the first mention to a previously unmentioned body", () => {
assert.deepEqual(diffAddedMentionPubkeys([], [ALICE], SELF), [ALICE]);
});
test("removing a mention adds nothing", () => {
assert.deepEqual(diffAddedMentionPubkeys([ALICE, BOB], [ALICE], SELF), []);
});
test("case-only difference is not treated as newly added", () => {
// Original stored uppercase, edit resolves lowercase (or vice versa).
assert.deepEqual(
diffAddedMentionPubkeys([ALICE.toUpperCase()], [ALICE], SELF),
[],
);
});
test("self-mention added in the edit is scrubbed, never notified", () => {
assert.deepEqual(diffAddedMentionPubkeys([ALICE], [ALICE, SELF], SELF), []);
});
test("duplicate added mention collapses to one", () => {
assert.deepEqual(diffAddedMentionPubkeys([], [BOB, BOB, BOB], SELF), [BOB]);
});
test("re-adding a removed mention counts as newly added", () => {
// Original had no Bob (he was removed in a prior state); this edit adds him.
assert.deepEqual(diffAddedMentionPubkeys([ALICE], [ALICE, BOB], SELF), [BOB]);
});

View File

@@ -74,6 +74,30 @@ export function normalizeMentionPubkeys(
return result;
}
/**
* Mentions an edit *newly adds*, relative to the original message body.
*
* The composer resolves both bodies to pubkey lists with the same
* channel-roster resolver the send path uses, then hands them here. We
* normalize the edited body's set (lowercase / dedup / drop self) and keep
* only pubkeys that were not already present in the original body — compared
* case-insensitively so a case-only difference is never treated as "new".
*
* A typo-fix edit that leaves the mention set unchanged yields `[]`, so the
* edit event carries no `p` tags and re-wakes nobody. Only genuinely new
* mentions get notified.
*/
export function diffAddedMentionPubkeys(
originalPubkeys: string[],
editedPubkeys: string[],
selfPubkey: string,
): string[] {
const original = new Set(originalPubkeys.map((pk) => pk.toLowerCase()));
return normalizeMentionPubkeys(editedPubkeys, selfPubkey).filter(
(pubkey) => !original.has(pubkey),
);
}
export function buildReplyTags(
channelId: string,
authorPubkey: string,

View File

@@ -26,6 +26,7 @@ import {
useMediaUpload,
} from "@/features/messages/lib/useMediaUpload";
import { useMentions } from "@/features/messages/lib/useMentions";
import { diffAddedMentionPubkeys } from "@/features/messages/lib/threading";
import { getPersistentAgentAudienceScope } from "@/features/messages/lib/persistentAgentAudience";
import { useIdentityQuery } from "@/shared/api/hooks";
import type { UserProfileLookup } from "@/features/profile/lib/identity";
@@ -113,7 +114,11 @@ type MessageComposerProps = {
* return `false` to let the arrow key fall through normally.
*/
onEditLastOwnMessage?: () => boolean;
onEditSave?: (content: string, mediaTags?: string[][]) => Promise<void>;
onEditSave?: (
content: string,
mediaTags?: string[][],
mentionPubkeys?: string[],
) => Promise<void>;
/** Captures send context synchronously before awaits can change navigation. */
onCaptureSendContext?: () => {
parentEventId: string | null;
@@ -273,6 +278,8 @@ function MessageComposerImpl({
const onEditSaveRef = React.useRef(onEditSave);
const onEditLastOwnMessageRef = React.useRef(onEditLastOwnMessage);
const editTargetRef = React.useRef(editTarget);
const extractMentionPubkeysRef = React.useRef(mentions.extractMentionPubkeys);
const ownerPubkeyRef = React.useRef(ownerPubkey);
disabledRef.current = disabled;
isSendingRef.current = isSending;
isUploadingRef.current = media.isUploading;
@@ -280,6 +287,8 @@ function MessageComposerImpl({
onEditSaveRef.current = onEditSave;
onEditLastOwnMessageRef.current = onEditLastOwnMessage;
editTargetRef.current = editTarget;
extractMentionPubkeysRef.current = mentions.extractMentionPubkeys;
ownerPubkeyRef.current = ownerPubkey;
const isAutocompleteOpenRef = React.useRef(false);
isAutocompleteOpenRef.current =
@@ -621,6 +630,16 @@ function MessageComposerImpl({
buildCustomEmojiTags(finalContent, customEmoji),
) ?? [];
// Notify only mentions this edit *newly adds* (see
// diffAddedMentionPubkeys): a typo-fix edit that leaves the mention set
// unchanged emits no `p` tags and re-wakes nobody. Computed before the
// composer state is cleared below.
const addedMentionPubkeys = diffAddedMentionPubkeys(
extractMentionPubkeysRef.current(editTargetRef.current.body),
extractMentionPubkeysRef.current(finalContent),
ownerPubkeyRef.current ?? "",
);
const savedContent = trimmed;
const savedImeta = [...currentPendingImeta];
const savedSpoileredAttachmentUrls = new Set(spoileredAttachmentUrls);
@@ -634,7 +653,11 @@ function MessageComposerImpl({
setIsEmojiPickerOpen(false);
try {
await onEditSaveRef.current(finalContent, outgoingTags);
await onEditSaveRef.current(
finalContent,
outgoingTags,
addedMentionPubkeys,
);
} catch {
setComposerContent(savedContent);
richText.setContent(savedContent);

View File

@@ -65,7 +65,11 @@ type MessageThreadPanelProps = {
onDelete?: (message: TimelineMessage) => void;
onEdit?: (message: TimelineMessage) => void;
onEditLastOwnMessage?: () => boolean;
onEditSave?: (content: string, mediaTags?: string[][]) => Promise<void>;
onEditSave?: (
content: string,
mediaTags?: string[][],
mentionPubkeys?: string[],
) => Promise<void>;
onMarkUnread?: (message: TimelineMessage) => void;
onMarkRead?: (message: TimelineMessage) => void;
onExpandReplies: (message: TimelineMessage) => void;