fix(matrix): keep mention enrichment best-effort

This commit is contained in:
Gustavo Madeira Santana
2026-04-02 01:56:32 -04:00
parent 29d4c99894
commit 4b641e35a2
2 changed files with 26 additions and 2 deletions

View File

@@ -215,6 +215,14 @@ function resolveMentionUserId(match: MatrixMentionCandidate): string | null {
return match.userId ?? null;
}
async function resolveMatrixSelfUserId(client: MatrixClient): Promise<string | null> {
const getUserId = (client as { getUserId?: () => Promise<string> | string }).getUserId;
if (typeof getUserId !== "function") {
return null;
}
return await Promise.resolve(getUserId.call(client)).catch(() => null);
}
function mutateInlineTokensWithMentions(params: {
children: MarkdownInlineToken[];
userIds: string[];
@@ -303,7 +311,7 @@ async function resolveMarkdownMentionState(params: {
const markdown = maskEscapedMentions(params.markdown ?? "");
const tokens = md.parse(markdown, {});
restoreEscapedMentionsInBlockTokens(tokens);
const selfUserId = await params.client.getUserId().catch(() => null);
const selfUserId = await resolveMatrixSelfUserId(params.client);
const userIds: string[] = [];
const seenUserIds = new Set<string>();
let roomMentioned = false;

View File

@@ -407,6 +407,22 @@ export async function sendSingleTextMessageMatrix(
);
}
async function getPreviousMatrixEvent(
client: MatrixClient,
roomId: string,
eventId: string,
): Promise<Record<string, unknown> | null> {
const getEvent = (
client as {
getEvent?: (roomId: string, eventId: string) => Promise<Record<string, unknown>>;
}
).getEvent;
if (typeof getEvent !== "function") {
return null;
}
return await Promise.resolve(getEvent.call(client, roomId, eventId)).catch(() => null);
}
export async function editMessageMatrix(
roomId: string,
originalEventId: string,
@@ -441,7 +457,7 @@ export async function editMessageMatrix(
content: newContent,
markdown: convertedText,
});
const previousEvent = await client.getEvent(resolvedRoom, originalEventId).catch(() => null);
const previousEvent = await getPreviousMatrixEvent(client, resolvedRoom, originalEventId);
const previousContent = resolvePreviousEditContent(previousEvent);
const replaceMentions = diffMatrixMentions(
extractMatrixMentions(newContent),