mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-21 05:32:53 +00:00
fix(matrix): keep mention enrichment best-effort
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user