@openclaw/matrix 2026.2.15 → 2026.2.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/package.json +2 -2
- package/src/actions.ts +1 -1
- package/src/channel.directory.test.ts +17 -7
- package/src/channel.ts +1 -1
- package/src/directory-live.test.ts +20 -0
- package/src/directory-live.ts +51 -33
- package/src/group-mentions.ts +1 -1
- package/src/matrix/actions/client.ts +7 -25
- package/src/matrix/actions/limits.test.ts +15 -0
- package/src/matrix/actions/limits.ts +6 -0
- package/src/matrix/actions/messages.ts +2 -4
- package/src/matrix/actions/pins.test.ts +74 -0
- package/src/matrix/actions/pins.ts +36 -28
- package/src/matrix/actions/reactions.test.ts +109 -0
- package/src/matrix/actions/reactions.ts +23 -17
- package/src/matrix/client/config.ts +2 -2
- package/src/matrix/client/create-client.ts +1 -1
- package/src/matrix/client/shared.ts +1 -1
- package/src/matrix/client/storage.ts +1 -1
- package/src/matrix/client-bootstrap.ts +39 -0
- package/src/matrix/deps.ts +83 -3
- package/src/matrix/monitor/auto-join.ts +2 -2
- package/src/matrix/monitor/handler.ts +1 -1
- package/src/matrix/monitor/index.ts +1 -1
- package/src/matrix/monitor/media.test.ts +7 -23
- package/src/matrix/monitor/mentions.test.ts +154 -0
- package/src/matrix/monitor/mentions.ts +31 -0
- package/src/matrix/monitor/replies.test.ts +132 -0
- package/src/matrix/monitor/replies.ts +11 -8
- package/src/matrix/send/client.ts +6 -25
- package/src/matrix/send.test.ts +7 -5
- package/src/onboarding.ts +3 -7
- package/src/resolve-targets.test.ts +20 -1
- package/src/resolve-targets.ts +20 -29
- package/src/tool-actions.ts +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { resolveMatrixRoomId } from "../send.js";
|
|
2
2
|
import { resolveActionClient } from "./client.js";
|
|
3
|
+
import { resolveMatrixActionLimit } from "./limits.js";
|
|
3
4
|
import {
|
|
4
5
|
EventType,
|
|
5
6
|
RelationType,
|
|
@@ -9,6 +10,23 @@ import {
|
|
|
9
10
|
type ReactionEventContent,
|
|
10
11
|
} from "./types.js";
|
|
11
12
|
|
|
13
|
+
function getReactionsPath(roomId: string, messageId: string): string {
|
|
14
|
+
return `/_matrix/client/v1/rooms/${encodeURIComponent(roomId)}/relations/${encodeURIComponent(messageId)}/${RelationType.Annotation}/${EventType.Reaction}`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function listReactionEvents(
|
|
18
|
+
client: NonNullable<MatrixActionClientOpts["client"]>,
|
|
19
|
+
roomId: string,
|
|
20
|
+
messageId: string,
|
|
21
|
+
limit: number,
|
|
22
|
+
): Promise<MatrixRawEvent[]> {
|
|
23
|
+
const res = (await client.doRequest("GET", getReactionsPath(roomId, messageId), {
|
|
24
|
+
dir: "b",
|
|
25
|
+
limit,
|
|
26
|
+
})) as { chunk: MatrixRawEvent[] };
|
|
27
|
+
return res.chunk;
|
|
28
|
+
}
|
|
29
|
+
|
|
12
30
|
export async function listMatrixReactions(
|
|
13
31
|
roomId: string,
|
|
14
32
|
messageId: string,
|
|
@@ -17,18 +35,10 @@ export async function listMatrixReactions(
|
|
|
17
35
|
const { client, stopOnDone } = await resolveActionClient(opts);
|
|
18
36
|
try {
|
|
19
37
|
const resolvedRoom = await resolveMatrixRoomId(client, roomId);
|
|
20
|
-
const limit =
|
|
21
|
-
|
|
22
|
-
? Math.max(1, Math.floor(opts.limit))
|
|
23
|
-
: 100;
|
|
24
|
-
// @vector-im/matrix-bot-sdk uses doRequest for relations
|
|
25
|
-
const res = (await client.doRequest(
|
|
26
|
-
"GET",
|
|
27
|
-
`/_matrix/client/v1/rooms/${encodeURIComponent(resolvedRoom)}/relations/${encodeURIComponent(messageId)}/${RelationType.Annotation}/${EventType.Reaction}`,
|
|
28
|
-
{ dir: "b", limit },
|
|
29
|
-
)) as { chunk: MatrixRawEvent[] };
|
|
38
|
+
const limit = resolveMatrixActionLimit(opts.limit, 100);
|
|
39
|
+
const chunk = await listReactionEvents(client, resolvedRoom, messageId, limit);
|
|
30
40
|
const summaries = new Map<string, MatrixReactionSummary>();
|
|
31
|
-
for (const event of
|
|
41
|
+
for (const event of chunk) {
|
|
32
42
|
const content = event.content as ReactionEventContent;
|
|
33
43
|
const key = content["m.relates_to"]?.key;
|
|
34
44
|
if (!key) {
|
|
@@ -62,17 +72,13 @@ export async function removeMatrixReactions(
|
|
|
62
72
|
const { client, stopOnDone } = await resolveActionClient(opts);
|
|
63
73
|
try {
|
|
64
74
|
const resolvedRoom = await resolveMatrixRoomId(client, roomId);
|
|
65
|
-
const
|
|
66
|
-
"GET",
|
|
67
|
-
`/_matrix/client/v1/rooms/${encodeURIComponent(resolvedRoom)}/relations/${encodeURIComponent(messageId)}/${RelationType.Annotation}/${EventType.Reaction}`,
|
|
68
|
-
{ dir: "b", limit: 200 },
|
|
69
|
-
)) as { chunk: MatrixRawEvent[] };
|
|
75
|
+
const chunk = await listReactionEvents(client, resolvedRoom, messageId, 200);
|
|
70
76
|
const userId = await client.getUserId();
|
|
71
77
|
if (!userId) {
|
|
72
78
|
return { removed: 0 };
|
|
73
79
|
}
|
|
74
80
|
const targetEmoji = opts.emoji?.trim();
|
|
75
|
-
const toRemove =
|
|
81
|
+
const toRemove = chunk
|
|
76
82
|
.filter((event) => event.sender === userId)
|
|
77
83
|
.filter((event) => {
|
|
78
84
|
if (!targetEmoji) {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { MatrixClient } from "@vector-im/matrix-bot-sdk";
|
|
2
2
|
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "openclaw/plugin-sdk/account-id";
|
|
3
|
-
import type { CoreConfig } from "../../types.js";
|
|
4
|
-
import type { MatrixAuth, MatrixResolvedConfig } from "./types.js";
|
|
5
3
|
import { getMatrixRuntime } from "../../runtime.js";
|
|
4
|
+
import type { CoreConfig } from "../../types.js";
|
|
6
5
|
import { ensureMatrixSdkLoggingConfigured } from "./logging.js";
|
|
6
|
+
import type { MatrixAuth, MatrixResolvedConfig } from "./types.js";
|
|
7
7
|
|
|
8
8
|
function clean(value?: string): string {
|
|
9
9
|
return value?.trim() ?? "";
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
1
2
|
import type { IStorageProvider, ICryptoStorageProvider } from "@vector-im/matrix-bot-sdk";
|
|
2
3
|
import {
|
|
3
4
|
LogService,
|
|
@@ -5,7 +6,6 @@ import {
|
|
|
5
6
|
SimpleFsStorageProvider,
|
|
6
7
|
RustSdkCryptoStorageProvider,
|
|
7
8
|
} from "@vector-im/matrix-bot-sdk";
|
|
8
|
-
import fs from "node:fs";
|
|
9
9
|
import { ensureMatrixSdkLoggingConfigured } from "./logging.js";
|
|
10
10
|
import {
|
|
11
11
|
maybeMigrateLegacyStorage,
|
|
@@ -2,10 +2,10 @@ import type { MatrixClient } from "@vector-im/matrix-bot-sdk";
|
|
|
2
2
|
import { LogService } from "@vector-im/matrix-bot-sdk";
|
|
3
3
|
import { normalizeAccountId } from "openclaw/plugin-sdk/account-id";
|
|
4
4
|
import type { CoreConfig } from "../../types.js";
|
|
5
|
-
import type { MatrixAuth } from "./types.js";
|
|
6
5
|
import { resolveMatrixAuth } from "./config.js";
|
|
7
6
|
import { createMatrixClient } from "./create-client.js";
|
|
8
7
|
import { DEFAULT_ACCOUNT_KEY } from "./storage.js";
|
|
8
|
+
import type { MatrixAuth } from "./types.js";
|
|
9
9
|
|
|
10
10
|
type SharedMatrixClientState = {
|
|
11
11
|
client: MatrixClient;
|
|
@@ -2,8 +2,8 @@ import crypto from "node:crypto";
|
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import os from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
|
-
import type { MatrixStoragePaths } from "./types.js";
|
|
6
5
|
import { getMatrixRuntime } from "../../runtime.js";
|
|
6
|
+
import type { MatrixStoragePaths } from "./types.js";
|
|
7
7
|
|
|
8
8
|
export const DEFAULT_ACCOUNT_KEY = "default";
|
|
9
9
|
const STORAGE_META_FILENAME = "storage-meta.json";
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { createMatrixClient } from "./client.js";
|
|
2
|
+
|
|
3
|
+
type MatrixClientBootstrapAuth = {
|
|
4
|
+
homeserver: string;
|
|
5
|
+
userId: string;
|
|
6
|
+
accessToken: string;
|
|
7
|
+
encryption?: boolean;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
type MatrixCryptoPrepare = {
|
|
11
|
+
prepare: (rooms?: string[]) => Promise<void>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
type MatrixBootstrapClient = Awaited<ReturnType<typeof createMatrixClient>>;
|
|
15
|
+
|
|
16
|
+
export async function createPreparedMatrixClient(opts: {
|
|
17
|
+
auth: MatrixClientBootstrapAuth;
|
|
18
|
+
timeoutMs?: number;
|
|
19
|
+
accountId?: string;
|
|
20
|
+
}): Promise<MatrixBootstrapClient> {
|
|
21
|
+
const client = await createMatrixClient({
|
|
22
|
+
homeserver: opts.auth.homeserver,
|
|
23
|
+
userId: opts.auth.userId,
|
|
24
|
+
accessToken: opts.auth.accessToken,
|
|
25
|
+
encryption: opts.auth.encryption,
|
|
26
|
+
localTimeoutMs: opts.timeoutMs,
|
|
27
|
+
accountId: opts.accountId,
|
|
28
|
+
});
|
|
29
|
+
if (opts.auth.encryption && client.crypto) {
|
|
30
|
+
try {
|
|
31
|
+
const joinedRooms = await client.getJoinedRooms();
|
|
32
|
+
await (client.crypto as MatrixCryptoPrepare).prepare(joinedRooms);
|
|
33
|
+
} catch {
|
|
34
|
+
// Ignore crypto prep failures for one-off requests.
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
await client.start();
|
|
38
|
+
return client;
|
|
39
|
+
}
|
package/src/matrix/deps.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import { createRequire } from "node:module";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
6
|
-
import {
|
|
6
|
+
import type { RuntimeEnv } from "openclaw/plugin-sdk";
|
|
7
7
|
|
|
8
8
|
const MATRIX_SDK_PACKAGE = "@vector-im/matrix-bot-sdk";
|
|
9
9
|
|
|
@@ -22,6 +22,85 @@ function resolvePluginRoot(): string {
|
|
|
22
22
|
return path.resolve(currentDir, "..", "..");
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
type CommandResult = {
|
|
26
|
+
code: number;
|
|
27
|
+
stdout: string;
|
|
28
|
+
stderr: string;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
async function runFixedCommandWithTimeout(params: {
|
|
32
|
+
argv: string[];
|
|
33
|
+
cwd: string;
|
|
34
|
+
timeoutMs: number;
|
|
35
|
+
env?: NodeJS.ProcessEnv;
|
|
36
|
+
}): Promise<CommandResult> {
|
|
37
|
+
return await new Promise((resolve) => {
|
|
38
|
+
const [command, ...args] = params.argv;
|
|
39
|
+
if (!command) {
|
|
40
|
+
resolve({
|
|
41
|
+
code: 1,
|
|
42
|
+
stdout: "",
|
|
43
|
+
stderr: "command is required",
|
|
44
|
+
});
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const proc = spawn(command, args, {
|
|
49
|
+
cwd: params.cwd,
|
|
50
|
+
env: { ...process.env, ...params.env },
|
|
51
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
let stdout = "";
|
|
55
|
+
let stderr = "";
|
|
56
|
+
let settled = false;
|
|
57
|
+
let timer: NodeJS.Timeout | null = null;
|
|
58
|
+
|
|
59
|
+
const finalize = (result: CommandResult) => {
|
|
60
|
+
if (settled) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
settled = true;
|
|
64
|
+
if (timer) {
|
|
65
|
+
clearTimeout(timer);
|
|
66
|
+
}
|
|
67
|
+
resolve(result);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
proc.stdout?.on("data", (chunk: Buffer | string) => {
|
|
71
|
+
stdout += chunk.toString();
|
|
72
|
+
});
|
|
73
|
+
proc.stderr?.on("data", (chunk: Buffer | string) => {
|
|
74
|
+
stderr += chunk.toString();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
timer = setTimeout(() => {
|
|
78
|
+
proc.kill("SIGKILL");
|
|
79
|
+
finalize({
|
|
80
|
+
code: 124,
|
|
81
|
+
stdout,
|
|
82
|
+
stderr: stderr || `command timed out after ${params.timeoutMs}ms`,
|
|
83
|
+
});
|
|
84
|
+
}, params.timeoutMs);
|
|
85
|
+
|
|
86
|
+
proc.on("error", (err) => {
|
|
87
|
+
finalize({
|
|
88
|
+
code: 1,
|
|
89
|
+
stdout,
|
|
90
|
+
stderr: err.message,
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
proc.on("close", (code) => {
|
|
95
|
+
finalize({
|
|
96
|
+
code: code ?? 1,
|
|
97
|
+
stdout,
|
|
98
|
+
stderr,
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
25
104
|
export async function ensureMatrixSdkInstalled(params: {
|
|
26
105
|
runtime: RuntimeEnv;
|
|
27
106
|
confirm?: (message: string) => Promise<boolean>;
|
|
@@ -42,7 +121,8 @@ export async function ensureMatrixSdkInstalled(params: {
|
|
|
42
121
|
? ["pnpm", "install"]
|
|
43
122
|
: ["npm", "install", "--omit=dev", "--silent"];
|
|
44
123
|
params.runtime.log?.(`matrix: installing dependencies via ${command[0]} (${root})…`);
|
|
45
|
-
const result = await
|
|
124
|
+
const result = await runFixedCommandWithTimeout({
|
|
125
|
+
argv: command,
|
|
46
126
|
cwd: root,
|
|
47
127
|
timeoutMs: 300_000,
|
|
48
128
|
env: { COREPACK_ENABLE_DOWNLOAD_PROMPT: "0" },
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { MatrixClient } from "@vector-im/matrix-bot-sdk";
|
|
2
|
-
import type { RuntimeEnv } from "openclaw/plugin-sdk";
|
|
3
2
|
import { AutojoinRoomsMixin } from "@vector-im/matrix-bot-sdk";
|
|
4
|
-
import type {
|
|
3
|
+
import type { RuntimeEnv } from "openclaw/plugin-sdk";
|
|
5
4
|
import { getMatrixRuntime } from "../../runtime.js";
|
|
5
|
+
import type { CoreConfig } from "../../types.js";
|
|
6
6
|
|
|
7
7
|
export function registerMatrixAutoJoin(params: {
|
|
8
8
|
client: MatrixClient;
|
|
@@ -11,7 +11,6 @@ import {
|
|
|
11
11
|
type RuntimeLogger,
|
|
12
12
|
} from "openclaw/plugin-sdk";
|
|
13
13
|
import type { CoreConfig, MatrixRoomConfig, ReplyToMode } from "../../types.js";
|
|
14
|
-
import type { MatrixRawEvent, RoomMessageEventContent } from "./types.js";
|
|
15
14
|
import { fetchEventSummary } from "../actions/summary.js";
|
|
16
15
|
import {
|
|
17
16
|
formatPollAsText,
|
|
@@ -36,6 +35,7 @@ import { resolveMentions } from "./mentions.js";
|
|
|
36
35
|
import { deliverMatrixReplies } from "./replies.js";
|
|
37
36
|
import { resolveMatrixRoomConfig } from "./rooms.js";
|
|
38
37
|
import { resolveMatrixThreadRootId, resolveMatrixThreadTarget } from "./threads.js";
|
|
38
|
+
import type { MatrixRawEvent, RoomMessageEventContent } from "./types.js";
|
|
39
39
|
import { EventType, RelationType } from "./types.js";
|
|
40
40
|
|
|
41
41
|
export type MatrixMonitorHandlerParams = {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { format } from "node:util";
|
|
2
2
|
import { mergeAllowlist, summarizeMapping, type RuntimeEnv } from "openclaw/plugin-sdk";
|
|
3
|
-
import type { CoreConfig, ReplyToMode } from "../../types.js";
|
|
4
3
|
import { resolveMatrixTargets } from "../../resolve-targets.js";
|
|
5
4
|
import { getMatrixRuntime } from "../../runtime.js";
|
|
5
|
+
import type { CoreConfig, ReplyToMode } from "../../types.js";
|
|
6
6
|
import { resolveMatrixAccount } from "../accounts.js";
|
|
7
7
|
import { setActiveMatrixClient } from "../active-client.js";
|
|
8
8
|
import {
|
|
@@ -22,14 +22,12 @@ describe("downloadMatrixMedia", () => {
|
|
|
22
22
|
setMatrixRuntime(runtimeStub);
|
|
23
23
|
});
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
function makeEncryptedMediaFixture() {
|
|
26
26
|
const decryptMedia = vi.fn().mockResolvedValue(Buffer.from("decrypted"));
|
|
27
|
-
|
|
28
27
|
const client = {
|
|
29
28
|
crypto: { decryptMedia },
|
|
30
29
|
mxcToHttp: vi.fn().mockReturnValue("https://example/mxc"),
|
|
31
30
|
} as unknown as import("@vector-im/matrix-bot-sdk").MatrixClient;
|
|
32
|
-
|
|
33
31
|
const file = {
|
|
34
32
|
url: "mxc://example/file",
|
|
35
33
|
key: {
|
|
@@ -43,6 +41,11 @@ describe("downloadMatrixMedia", () => {
|
|
|
43
41
|
hashes: { sha256: "hash" },
|
|
44
42
|
v: "v2",
|
|
45
43
|
};
|
|
44
|
+
return { decryptMedia, client, file };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
it("decrypts encrypted media when file payloads are present", async () => {
|
|
48
|
+
const { decryptMedia, client, file } = makeEncryptedMediaFixture();
|
|
46
49
|
|
|
47
50
|
const result = await downloadMatrixMedia({
|
|
48
51
|
client,
|
|
@@ -64,26 +67,7 @@ describe("downloadMatrixMedia", () => {
|
|
|
64
67
|
});
|
|
65
68
|
|
|
66
69
|
it("rejects encrypted media that exceeds maxBytes before decrypting", async () => {
|
|
67
|
-
const decryptMedia =
|
|
68
|
-
|
|
69
|
-
const client = {
|
|
70
|
-
crypto: { decryptMedia },
|
|
71
|
-
mxcToHttp: vi.fn().mockReturnValue("https://example/mxc"),
|
|
72
|
-
} as unknown as import("@vector-im/matrix-bot-sdk").MatrixClient;
|
|
73
|
-
|
|
74
|
-
const file = {
|
|
75
|
-
url: "mxc://example/file",
|
|
76
|
-
key: {
|
|
77
|
-
kty: "oct",
|
|
78
|
-
key_ops: ["encrypt", "decrypt"],
|
|
79
|
-
alg: "A256CTR",
|
|
80
|
-
k: "secret",
|
|
81
|
-
ext: true,
|
|
82
|
-
},
|
|
83
|
-
iv: "iv",
|
|
84
|
-
hashes: { sha256: "hash" },
|
|
85
|
-
v: "v2",
|
|
86
|
-
};
|
|
70
|
+
const { decryptMedia, client, file } = makeEncryptedMediaFixture();
|
|
87
71
|
|
|
88
72
|
await expect(
|
|
89
73
|
downloadMatrixMedia({
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
// Mock the runtime before importing resolveMentions
|
|
4
|
+
vi.mock("../../runtime.js", () => ({
|
|
5
|
+
getMatrixRuntime: () => ({
|
|
6
|
+
channel: {
|
|
7
|
+
mentions: {
|
|
8
|
+
matchesMentionPatterns: (text: string, patterns: RegExp[]) =>
|
|
9
|
+
patterns.some((p) => p.test(text)),
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
}),
|
|
13
|
+
}));
|
|
14
|
+
|
|
15
|
+
import { resolveMentions } from "./mentions.js";
|
|
16
|
+
|
|
17
|
+
describe("resolveMentions", () => {
|
|
18
|
+
const userId = "@bot:matrix.org";
|
|
19
|
+
const mentionRegexes = [/@bot/i];
|
|
20
|
+
|
|
21
|
+
describe("m.mentions field", () => {
|
|
22
|
+
it("detects mention via m.mentions.user_ids", () => {
|
|
23
|
+
const result = resolveMentions({
|
|
24
|
+
content: {
|
|
25
|
+
msgtype: "m.text",
|
|
26
|
+
body: "hello",
|
|
27
|
+
"m.mentions": { user_ids: ["@bot:matrix.org"] },
|
|
28
|
+
},
|
|
29
|
+
userId,
|
|
30
|
+
text: "hello",
|
|
31
|
+
mentionRegexes,
|
|
32
|
+
});
|
|
33
|
+
expect(result.wasMentioned).toBe(true);
|
|
34
|
+
expect(result.hasExplicitMention).toBe(true);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("detects room mention via m.mentions.room", () => {
|
|
38
|
+
const result = resolveMentions({
|
|
39
|
+
content: {
|
|
40
|
+
msgtype: "m.text",
|
|
41
|
+
body: "hello everyone",
|
|
42
|
+
"m.mentions": { room: true },
|
|
43
|
+
},
|
|
44
|
+
userId,
|
|
45
|
+
text: "hello everyone",
|
|
46
|
+
mentionRegexes,
|
|
47
|
+
});
|
|
48
|
+
expect(result.wasMentioned).toBe(true);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe("formatted_body matrix.to links", () => {
|
|
53
|
+
it("detects mention in formatted_body with plain user ID", () => {
|
|
54
|
+
const result = resolveMentions({
|
|
55
|
+
content: {
|
|
56
|
+
msgtype: "m.text",
|
|
57
|
+
body: "Bot: hello",
|
|
58
|
+
formatted_body: '<a href="https://matrix.to/#/@bot:matrix.org">Bot</a>: hello',
|
|
59
|
+
},
|
|
60
|
+
userId,
|
|
61
|
+
text: "Bot: hello",
|
|
62
|
+
mentionRegexes: [],
|
|
63
|
+
});
|
|
64
|
+
expect(result.wasMentioned).toBe(true);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("detects mention in formatted_body with URL-encoded user ID", () => {
|
|
68
|
+
const result = resolveMentions({
|
|
69
|
+
content: {
|
|
70
|
+
msgtype: "m.text",
|
|
71
|
+
body: "Bot: hello",
|
|
72
|
+
formatted_body: '<a href="https://matrix.to/#/%40bot%3Amatrix.org">Bot</a>: hello',
|
|
73
|
+
},
|
|
74
|
+
userId,
|
|
75
|
+
text: "Bot: hello",
|
|
76
|
+
mentionRegexes: [],
|
|
77
|
+
});
|
|
78
|
+
expect(result.wasMentioned).toBe(true);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("detects mention with single quotes in href", () => {
|
|
82
|
+
const result = resolveMentions({
|
|
83
|
+
content: {
|
|
84
|
+
msgtype: "m.text",
|
|
85
|
+
body: "Bot: hello",
|
|
86
|
+
formatted_body: "<a href='https://matrix.to/#/@bot:matrix.org'>Bot</a>: hello",
|
|
87
|
+
},
|
|
88
|
+
userId,
|
|
89
|
+
text: "Bot: hello",
|
|
90
|
+
mentionRegexes: [],
|
|
91
|
+
});
|
|
92
|
+
expect(result.wasMentioned).toBe(true);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("does not detect mention for different user ID", () => {
|
|
96
|
+
const result = resolveMentions({
|
|
97
|
+
content: {
|
|
98
|
+
msgtype: "m.text",
|
|
99
|
+
body: "Other: hello",
|
|
100
|
+
formatted_body: '<a href="https://matrix.to/#/@other:matrix.org">Other</a>: hello',
|
|
101
|
+
},
|
|
102
|
+
userId,
|
|
103
|
+
text: "Other: hello",
|
|
104
|
+
mentionRegexes: [],
|
|
105
|
+
});
|
|
106
|
+
expect(result.wasMentioned).toBe(false);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("does not false-positive on partial user ID match", () => {
|
|
110
|
+
const result = resolveMentions({
|
|
111
|
+
content: {
|
|
112
|
+
msgtype: "m.text",
|
|
113
|
+
body: "Bot2: hello",
|
|
114
|
+
formatted_body: '<a href="https://matrix.to/#/@bot2:matrix.org">Bot2</a>: hello',
|
|
115
|
+
},
|
|
116
|
+
userId: "@bot:matrix.org",
|
|
117
|
+
text: "Bot2: hello",
|
|
118
|
+
mentionRegexes: [],
|
|
119
|
+
});
|
|
120
|
+
expect(result.wasMentioned).toBe(false);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
describe("regex patterns", () => {
|
|
125
|
+
it("detects mention via regex pattern in body text", () => {
|
|
126
|
+
const result = resolveMentions({
|
|
127
|
+
content: {
|
|
128
|
+
msgtype: "m.text",
|
|
129
|
+
body: "hey @bot can you help?",
|
|
130
|
+
},
|
|
131
|
+
userId,
|
|
132
|
+
text: "hey @bot can you help?",
|
|
133
|
+
mentionRegexes,
|
|
134
|
+
});
|
|
135
|
+
expect(result.wasMentioned).toBe(true);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe("no mention", () => {
|
|
140
|
+
it("returns false when no mention is present", () => {
|
|
141
|
+
const result = resolveMentions({
|
|
142
|
+
content: {
|
|
143
|
+
msgtype: "m.text",
|
|
144
|
+
body: "hello world",
|
|
145
|
+
},
|
|
146
|
+
userId,
|
|
147
|
+
text: "hello world",
|
|
148
|
+
mentionRegexes,
|
|
149
|
+
});
|
|
150
|
+
expect(result.wasMentioned).toBe(false);
|
|
151
|
+
expect(result.hasExplicitMention).toBe(false);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
});
|
|
@@ -4,12 +4,36 @@ import { getMatrixRuntime } from "../../runtime.js";
|
|
|
4
4
|
type MessageContentWithMentions = {
|
|
5
5
|
msgtype: string;
|
|
6
6
|
body: string;
|
|
7
|
+
formatted_body?: string;
|
|
7
8
|
"m.mentions"?: {
|
|
8
9
|
user_ids?: string[];
|
|
9
10
|
room?: boolean;
|
|
10
11
|
};
|
|
11
12
|
};
|
|
12
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Check if the formatted_body contains a matrix.to mention link for the given user ID.
|
|
16
|
+
* Many Matrix clients (including Element) use HTML links in formatted_body instead of
|
|
17
|
+
* or in addition to the m.mentions field.
|
|
18
|
+
*/
|
|
19
|
+
function checkFormattedBodyMention(formattedBody: string | undefined, userId: string): boolean {
|
|
20
|
+
if (!formattedBody || !userId) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
// Escape special regex characters in the user ID (e.g., @user:matrix.org)
|
|
24
|
+
const escapedUserId = userId.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
25
|
+
// Match matrix.to links with the user ID, handling both URL-encoded and plain formats
|
|
26
|
+
// Example: href="https://matrix.to/#/@user:matrix.org" or href="https://matrix.to/#/%40user%3Amatrix.org"
|
|
27
|
+
const plainPattern = new RegExp(`href=["']https://matrix\\.to/#/${escapedUserId}["']`, "i");
|
|
28
|
+
if (plainPattern.test(formattedBody)) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
// Also check URL-encoded version (@ -> %40, : -> %3A)
|
|
32
|
+
const encodedUserId = encodeURIComponent(userId).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
33
|
+
const encodedPattern = new RegExp(`href=["']https://matrix\\.to/#/${encodedUserId}["']`, "i");
|
|
34
|
+
return encodedPattern.test(formattedBody);
|
|
35
|
+
}
|
|
36
|
+
|
|
13
37
|
export function resolveMentions(params: {
|
|
14
38
|
content: MessageContentWithMentions;
|
|
15
39
|
userId?: string | null;
|
|
@@ -20,9 +44,16 @@ export function resolveMentions(params: {
|
|
|
20
44
|
const mentionedUsers = Array.isArray(mentions?.user_ids)
|
|
21
45
|
? new Set(mentions.user_ids)
|
|
22
46
|
: new Set<string>();
|
|
47
|
+
|
|
48
|
+
// Check formatted_body for matrix.to mention links (legacy/alternative mention format)
|
|
49
|
+
const mentionedInFormattedBody = params.userId
|
|
50
|
+
? checkFormattedBodyMention(params.content.formatted_body, params.userId)
|
|
51
|
+
: false;
|
|
52
|
+
|
|
23
53
|
const wasMentioned =
|
|
24
54
|
Boolean(mentions?.room) ||
|
|
25
55
|
(params.userId ? mentionedUsers.has(params.userId) : false) ||
|
|
56
|
+
mentionedInFormattedBody ||
|
|
26
57
|
getMatrixRuntime().channel.mentions.matchesMentionPatterns(
|
|
27
58
|
params.text ?? "",
|
|
28
59
|
params.mentionRegexes,
|