Matrix: defer sender lookups until needed

This commit is contained in:
Gustavo Madeira Santana
2026-03-12 08:42:37 +00:00
parent 627555eda2
commit 76ca201bb9
2 changed files with 14 additions and 3 deletions

View File

@@ -255,13 +255,14 @@ describe("matrix monitor handler pairing account scope", () => {
it("skips media downloads for unmentioned group media messages", async () => {
const downloadContent = vi.fn(async () => Buffer.from("image"));
const getMemberDisplayName = vi.fn(async () => "sender");
const { handler, resolveAgentRoute } = createMatrixHandlerTestHarness({
client: {
downloadContent,
},
isDirectMessage: false,
mentionRegexes: [/@bot/i],
getMemberDisplayName: async () => "sender",
getMemberDisplayName,
});
await handler("!room:example.org", {
@@ -281,6 +282,7 @@ describe("matrix monitor handler pairing account scope", () => {
} as MatrixRawEvent);
expect(downloadContent).not.toHaveBeenCalled();
expect(getMemberDisplayName).not.toHaveBeenCalled();
expect(resolveAgentRoute).not.toHaveBeenCalled();
});
@@ -304,6 +306,7 @@ describe("matrix monitor handler pairing account scope", () => {
nextBatch: null,
prevBatch: null,
}));
const getMemberDisplayName = vi.fn(async () => "sender");
const { handler, resolveAgentRoute } = createMatrixHandlerTestHarness({
client: {
getEvent,
@@ -311,7 +314,7 @@ describe("matrix monitor handler pairing account scope", () => {
},
isDirectMessage: false,
mentionRegexes: [/@bot/i],
getMemberDisplayName: async () => "sender",
getMemberDisplayName,
});
await handler("!room:example.org", {
@@ -332,6 +335,7 @@ describe("matrix monitor handler pairing account scope", () => {
expect(getEvent).not.toHaveBeenCalled();
expect(getRelations).not.toHaveBeenCalled();
expect(getMemberDisplayName).not.toHaveBeenCalled();
expect(resolveAgentRoute).not.toHaveBeenCalled();
});

View File

@@ -294,7 +294,11 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam
}
}
const senderName = await getMemberDisplayName(roomId, senderId);
let senderNamePromise: Promise<string> | null = null;
const getSenderName = async (): Promise<string> => {
senderNamePromise ??= getMemberDisplayName(roomId, senderId).catch(() => senderId);
return await senderNamePromise;
};
const storeAllowFrom = await readStoreAllowFrom();
const roomUsers = roomConfig?.users ?? [];
const accessState = resolveMatrixMonitorAccessState({
@@ -324,6 +328,7 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam
const allowMatchMeta = formatAllowlistMatchMeta(directAllowMatch);
if (!directAllowMatch.allowed) {
if (!isReactionEvent && dmPolicy === "pairing") {
const senderName = await getSenderName();
const { code, created } = await core.channel.pairing.upsertPairingRequest({
channel: "matrix",
id: senderId,
@@ -400,6 +405,7 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam
}
if (isReactionEvent) {
const senderName = await getSenderName();
await handleInboundMatrixReaction({
client,
core,
@@ -541,6 +547,7 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam
if (!bodyText) {
return;
}
const senderName = await getSenderName();
const messageId = event.event_id ?? "";
const replyToEventId = content["m.relates_to"]?.["m.in_reply_to"]?.event_id;