fix(desktop): refetch home feed immediately on live mentions (#230)

This commit is contained in:
Wes
2026-04-03 18:49:43 -07:00
committed by GitHub
parent 5923a7ba1c
commit bf083dda5d
8 changed files with 616 additions and 147 deletions

View File

@@ -1267,6 +1267,58 @@ function getThreadReferenceFromTags(tags: string[][]) {
};
}
function appendMentionTags(
tags: string[][],
mentionPubkeys: string[] | undefined,
selfPubkey: string,
) {
const selfLower = selfPubkey.toLowerCase();
const seen = new Set<string>([selfLower]);
for (const pk of mentionPubkeys ?? []) {
const lower = pk.toLowerCase();
if (seen.has(lower)) {
continue;
}
seen.add(lower);
tags.push(["p", lower]);
}
}
function buildTopLevelMessageTags(
channelId: string,
mentionPubkeys: string[] | undefined,
selfPubkey: string,
) {
const tags: string[][] = [["h", channelId]];
appendMentionTags(tags, mentionPubkeys, selfPubkey);
return tags;
}
function buildReplyMessageTags(
channelId: string,
authorPubkey: string,
parentEventId: string,
rootEventId: string,
mentionPubkeys: string[] | undefined,
) {
// Preserve the reply tag ordering that the desktop message hooks already
// expect locally: author p, h, mention ps, then thread e-tags.
const tags: string[][] = [
["p", authorPubkey],
["h", channelId],
];
appendMentionTags(tags, mentionPubkeys, authorPubkey);
if (parentEventId === rootEventId) {
tags.push(["e", rootEventId, "", "reply"]);
return tags;
}
tags.push(["e", rootEventId, "", "root"]);
tags.push(["e", parentEventId, "", "reply"]);
return tags;
}
function getMockMessageStore(channelId: string): RelayEvent[] {
const existing = mockMessages.get(channelId);
if (existing) {
@@ -2934,16 +2986,27 @@ async function handleSendChannelMessage(
channelId: string;
content: string;
parentEventId?: string | null;
kind?: number | null;
mentionPubkeys?: string[];
},
config: E2eConfig | undefined,
): Promise<RawSendChannelMessageResponse> {
const kind = args.kind ?? 9;
const identity = getIdentity(config);
if (!identity) {
const createdAt = Math.floor(Date.now() / 1000);
const mockPubkey = getMockMemberPubkey(config);
if (!args.parentEventId) {
const event = createMockEvent(9, args.content, [["h", args.channelId]]);
const event = createMockEvent(
kind,
args.content,
buildTopLevelMessageTags(
args.channelId,
args.mentionPubkeys,
mockPubkey,
),
);
recordMockMessage(args.channelId, event);
emitMockLiveEvent(args.channelId, event);
@@ -2990,35 +3053,16 @@ async function handleSendChannelMessage(
const event: RelayEvent = {
id: crypto.randomUUID().replace(/-/g, ""),
pubkey: getMockMemberPubkey(config),
pubkey: mockPubkey,
created_at: createdAt,
kind: 9,
tags: (() => {
const authorPubkey = getMockMemberPubkey(config);
// Match production tag ordering: author p, h, mention ps, then e-tags.
const tags: string[][] = [
["p", authorPubkey],
["h", args.channelId],
];
// Best-effort client-side normalization (relay is authoritative).
const selfLower = authorPubkey.toLowerCase();
const seen = new Set<string>([selfLower]);
for (const pk of args.mentionPubkeys ?? []) {
const lower = pk.toLowerCase();
if (!seen.has(lower)) {
seen.add(lower);
tags.push(["p", lower]);
}
}
// Thread e-tags come after mention p-tags.
if (rootEventId === args.parentEventId) {
tags.push(["e", rootEventId, "", "reply"]);
} else {
tags.push(["e", rootEventId, "", "root"]);
tags.push(["e", args.parentEventId, "", "reply"]);
}
return tags;
})(),
kind,
tags: buildReplyMessageTags(
args.channelId,
mockPubkey,
args.parentEventId,
rootEventId,
args.mentionPubkeys,
),
content: args.content.trim(),
sig: "mocksig".repeat(20).slice(0, 128),
};
@@ -3036,32 +3080,22 @@ async function handleSendChannelMessage(
}
const relayIdentity = getRelayIdentity(config);
const tags: string[][] = [
["p", relayIdentity.pubkey],
["h", args.channelId],
];
// Add mention p-tags (deduplicated, excluding self).
const selfLower = relayIdentity.pubkey.toLowerCase();
const seen = new Set<string>([selfLower]);
for (const pk of args.mentionPubkeys ?? []) {
const lower = pk.toLowerCase();
if (!seen.has(lower)) {
seen.add(lower);
tags.push(["p", lower]);
}
}
// Add thread e-tags if replying.
if (args.parentEventId) {
// Simplified: treat parent as both root and reply for direct replies.
// The relay's NIP-10 resolver handles ancestry validation.
tags.push(["e", args.parentEventId, "", "root"]);
tags.push(["e", args.parentEventId, "", "reply"]);
}
const tags = args.parentEventId
? buildReplyMessageTags(
args.channelId,
relayIdentity.pubkey,
args.parentEventId,
args.parentEventId,
args.mentionPubkeys,
)
: buildTopLevelMessageTags(
args.channelId,
args.mentionPubkeys,
relayIdentity.pubkey,
);
const result = await submitSignedEvent(config, {
kind: 9,
kind,
content: args.content.trim(),
tags,
});