@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.
Files changed (36) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/package.json +2 -2
  3. package/src/actions.ts +1 -1
  4. package/src/channel.directory.test.ts +17 -7
  5. package/src/channel.ts +1 -1
  6. package/src/directory-live.test.ts +20 -0
  7. package/src/directory-live.ts +51 -33
  8. package/src/group-mentions.ts +1 -1
  9. package/src/matrix/actions/client.ts +7 -25
  10. package/src/matrix/actions/limits.test.ts +15 -0
  11. package/src/matrix/actions/limits.ts +6 -0
  12. package/src/matrix/actions/messages.ts +2 -4
  13. package/src/matrix/actions/pins.test.ts +74 -0
  14. package/src/matrix/actions/pins.ts +36 -28
  15. package/src/matrix/actions/reactions.test.ts +109 -0
  16. package/src/matrix/actions/reactions.ts +23 -17
  17. package/src/matrix/client/config.ts +2 -2
  18. package/src/matrix/client/create-client.ts +1 -1
  19. package/src/matrix/client/shared.ts +1 -1
  20. package/src/matrix/client/storage.ts +1 -1
  21. package/src/matrix/client-bootstrap.ts +39 -0
  22. package/src/matrix/deps.ts +83 -3
  23. package/src/matrix/monitor/auto-join.ts +2 -2
  24. package/src/matrix/monitor/handler.ts +1 -1
  25. package/src/matrix/monitor/index.ts +1 -1
  26. package/src/matrix/monitor/media.test.ts +7 -23
  27. package/src/matrix/monitor/mentions.test.ts +154 -0
  28. package/src/matrix/monitor/mentions.ts +31 -0
  29. package/src/matrix/monitor/replies.test.ts +132 -0
  30. package/src/matrix/monitor/replies.ts +11 -8
  31. package/src/matrix/send/client.ts +6 -25
  32. package/src/matrix/send.test.ts +7 -5
  33. package/src/onboarding.ts +3 -7
  34. package/src/resolve-targets.test.ts +20 -1
  35. package/src/resolve-targets.ts +20 -29
  36. package/src/tool-actions.ts +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## 2026.2.19
4
+
5
+ ### Changes
6
+
7
+ - Version alignment with core OpenClaw release numbers.
8
+
9
+ ## 2026.2.16
10
+
11
+ ### Changes
12
+
13
+ - Version alignment with core OpenClaw release numbers.
14
+
3
15
  ## 2026.2.15
4
16
 
5
17
  ### Changes
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@openclaw/matrix",
3
- "version": "2026.2.15",
3
+ "version": "2026.2.19",
4
4
  "description": "OpenClaw Matrix channel plugin",
5
5
  "type": "module",
6
6
  "dependencies": {
7
7
  "@matrix-org/matrix-sdk-crypto-nodejs": "^0.4.0",
8
8
  "@vector-im/matrix-bot-sdk": "0.8.0-element.3",
9
9
  "markdown-it": "14.1.1",
10
- "music-metadata": "^11.12.0",
10
+ "music-metadata": "^11.12.1",
11
11
  "zod": "^4.3.6"
12
12
  },
13
13
  "devDependencies": {
package/src/actions.ts CHANGED
@@ -7,9 +7,9 @@ import {
7
7
  type ChannelMessageActionName,
8
8
  type ChannelToolSend,
9
9
  } from "openclaw/plugin-sdk";
10
- import type { CoreConfig } from "./types.js";
11
10
  import { resolveMatrixAccount } from "./matrix/accounts.js";
12
11
  import { handleMatrixAction } from "./tool-actions.js";
12
+ import type { CoreConfig } from "./types.js";
13
13
 
14
14
  export const matrixMessageActions: ChannelMessageActionAdapter = {
15
15
  listActions: ({ cfg }) => {
@@ -1,8 +1,8 @@
1
- import type { PluginRuntime } from "openclaw/plugin-sdk";
1
+ import type { PluginRuntime, RuntimeEnv } from "openclaw/plugin-sdk";
2
2
  import { beforeEach, describe, expect, it, vi } from "vitest";
3
- import type { CoreConfig } from "./types.js";
4
3
  import { matrixPlugin } from "./channel.js";
5
4
  import { setMatrixRuntime } from "./runtime.js";
5
+ import type { CoreConfig } from "./types.js";
6
6
 
7
7
  vi.mock("@vector-im/matrix-bot-sdk", () => ({
8
8
  ConsoleLogger: class {
@@ -24,10 +24,18 @@ vi.mock("@vector-im/matrix-bot-sdk", () => ({
24
24
  }));
25
25
 
26
26
  describe("matrix directory", () => {
27
+ const runtimeEnv: RuntimeEnv = {
28
+ log: vi.fn(),
29
+ error: vi.fn(),
30
+ exit: vi.fn((code: number): never => {
31
+ throw new Error(`exit ${code}`);
32
+ }),
33
+ };
34
+
27
35
  beforeEach(() => {
28
36
  setMatrixRuntime({
29
37
  state: {
30
- resolveStateDir: (_env, homeDir) => homeDir(),
38
+ resolveStateDir: (_env, homeDir) => (homeDir ?? (() => "/tmp"))(),
31
39
  },
32
40
  } as PluginRuntime);
33
41
  });
@@ -51,11 +59,12 @@ describe("matrix directory", () => {
51
59
  expect(matrixPlugin.directory?.listGroups).toBeTruthy();
52
60
 
53
61
  await expect(
54
- matrixPlugin.directory!.listPeers({
62
+ matrixPlugin.directory!.listPeers!({
55
63
  cfg,
56
64
  accountId: undefined,
57
65
  query: undefined,
58
66
  limit: undefined,
67
+ runtime: runtimeEnv,
59
68
  }),
60
69
  ).resolves.toEqual(
61
70
  expect.arrayContaining([
@@ -67,11 +76,12 @@ describe("matrix directory", () => {
67
76
  );
68
77
 
69
78
  await expect(
70
- matrixPlugin.directory!.listGroups({
79
+ matrixPlugin.directory!.listGroups!({
71
80
  cfg,
72
81
  accountId: undefined,
73
82
  query: undefined,
74
83
  limit: undefined,
84
+ runtime: runtimeEnv,
75
85
  }),
76
86
  ).resolves.toEqual(
77
87
  expect.arrayContaining([
@@ -130,11 +140,11 @@ describe("matrix directory", () => {
130
140
  },
131
141
  } as unknown as CoreConfig;
132
142
 
133
- expect(matrixPlugin.groups.resolveRequireMention({ cfg, groupId: "!room:example.org" })).toBe(
143
+ expect(matrixPlugin.groups!.resolveRequireMention!({ cfg, groupId: "!room:example.org" })).toBe(
134
144
  true,
135
145
  );
136
146
  expect(
137
- matrixPlugin.groups.resolveRequireMention({
147
+ matrixPlugin.groups!.resolveRequireMention!({
138
148
  cfg,
139
149
  accountId: "assistant",
140
150
  groupId: "!room:example.org",
package/src/channel.ts CHANGED
@@ -9,7 +9,6 @@ import {
9
9
  setAccountEnabledInConfigSection,
10
10
  type ChannelPlugin,
11
11
  } from "openclaw/plugin-sdk";
12
- import type { CoreConfig } from "./types.js";
13
12
  import { matrixMessageActions } from "./actions.js";
14
13
  import { MatrixConfigSchema } from "./config-schema.js";
15
14
  import { listMatrixDirectoryGroupsLive, listMatrixDirectoryPeersLive } from "./directory-live.js";
@@ -31,6 +30,7 @@ import { sendMessageMatrix } from "./matrix/send.js";
31
30
  import { matrixOnboardingAdapter } from "./onboarding.js";
32
31
  import { matrixOutbound } from "./outbound.js";
33
32
  import { resolveMatrixTargets } from "./resolve-targets.js";
33
+ import type { CoreConfig } from "./types.js";
34
34
 
35
35
  // Mutex for serializing account startup (workaround for concurrent dynamic import race condition)
36
36
  let matrixStartupLock: Promise<void> = Promise.resolve();
@@ -51,4 +51,24 @@ describe("matrix directory live", () => {
51
51
 
52
52
  expect(resolveMatrixAuth).toHaveBeenCalledWith({ cfg, accountId: "assistant" });
53
53
  });
54
+
55
+ it("returns no peer results for empty query without resolving auth", async () => {
56
+ const result = await listMatrixDirectoryPeersLive({
57
+ cfg,
58
+ query: " ",
59
+ });
60
+
61
+ expect(result).toEqual([]);
62
+ expect(resolveMatrixAuth).not.toHaveBeenCalled();
63
+ });
64
+
65
+ it("returns no group results for empty query without resolving auth", async () => {
66
+ const result = await listMatrixDirectoryGroupsLive({
67
+ cfg,
68
+ query: "",
69
+ });
70
+
71
+ expect(result).toEqual([]);
72
+ expect(resolveMatrixAuth).not.toHaveBeenCalled();
73
+ });
54
74
  });
@@ -22,6 +22,15 @@ type MatrixAliasLookup = {
22
22
  room_id?: string;
23
23
  };
24
24
 
25
+ type MatrixDirectoryLiveParams = {
26
+ cfg: unknown;
27
+ accountId?: string | null;
28
+ query?: string | null;
29
+ limit?: number | null;
30
+ };
31
+
32
+ type MatrixResolvedAuth = Awaited<ReturnType<typeof resolveMatrixAuth>>;
33
+
25
34
  async function fetchMatrixJson<T>(params: {
26
35
  homeserver: string;
27
36
  path: string;
@@ -48,17 +57,42 @@ function normalizeQuery(value?: string | null): string {
48
57
  return value?.trim().toLowerCase() ?? "";
49
58
  }
50
59
 
51
- export async function listMatrixDirectoryPeersLive(params: {
52
- cfg: unknown;
53
- accountId?: string | null;
54
- query?: string | null;
55
- limit?: number | null;
56
- }): Promise<ChannelDirectoryEntry[]> {
60
+ function resolveMatrixDirectoryLimit(limit?: number | null): number {
61
+ return typeof limit === "number" && limit > 0 ? limit : 20;
62
+ }
63
+
64
+ async function resolveMatrixDirectoryContext(
65
+ params: MatrixDirectoryLiveParams,
66
+ ): Promise<{ query: string; auth: MatrixResolvedAuth } | null> {
57
67
  const query = normalizeQuery(params.query);
58
68
  if (!query) {
59
- return [];
69
+ return null;
60
70
  }
61
71
  const auth = await resolveMatrixAuth({ cfg: params.cfg as never, accountId: params.accountId });
72
+ return { query, auth };
73
+ }
74
+
75
+ function createGroupDirectoryEntry(params: {
76
+ id: string;
77
+ name: string;
78
+ handle?: string;
79
+ }): ChannelDirectoryEntry {
80
+ return {
81
+ kind: "group",
82
+ id: params.id,
83
+ name: params.name,
84
+ handle: params.handle,
85
+ } satisfies ChannelDirectoryEntry;
86
+ }
87
+
88
+ export async function listMatrixDirectoryPeersLive(
89
+ params: MatrixDirectoryLiveParams,
90
+ ): Promise<ChannelDirectoryEntry[]> {
91
+ const context = await resolveMatrixDirectoryContext(params);
92
+ if (!context) {
93
+ return [];
94
+ }
95
+ const { query, auth } = context;
62
96
  const res = await fetchMatrixJson<MatrixUserDirectoryResponse>({
63
97
  homeserver: auth.homeserver,
64
98
  accessToken: auth.accessToken,
@@ -66,7 +100,7 @@ export async function listMatrixDirectoryPeersLive(params: {
66
100
  method: "POST",
67
101
  body: {
68
102
  search_term: query,
69
- limit: typeof params.limit === "number" && params.limit > 0 ? params.limit : 20,
103
+ limit: resolveMatrixDirectoryLimit(params.limit),
70
104
  },
71
105
  });
72
106
  const results = res.results ?? [];
@@ -121,42 +155,26 @@ async function fetchMatrixRoomName(
121
155
  }
122
156
  }
123
157
 
124
- export async function listMatrixDirectoryGroupsLive(params: {
125
- cfg: unknown;
126
- accountId?: string | null;
127
- query?: string | null;
128
- limit?: number | null;
129
- }): Promise<ChannelDirectoryEntry[]> {
130
- const query = normalizeQuery(params.query);
131
- if (!query) {
158
+ export async function listMatrixDirectoryGroupsLive(
159
+ params: MatrixDirectoryLiveParams,
160
+ ): Promise<ChannelDirectoryEntry[]> {
161
+ const context = await resolveMatrixDirectoryContext(params);
162
+ if (!context) {
132
163
  return [];
133
164
  }
134
- const auth = await resolveMatrixAuth({ cfg: params.cfg as never, accountId: params.accountId });
135
- const limit = typeof params.limit === "number" && params.limit > 0 ? params.limit : 20;
165
+ const { query, auth } = context;
166
+ const limit = resolveMatrixDirectoryLimit(params.limit);
136
167
 
137
168
  if (query.startsWith("#")) {
138
169
  const roomId = await resolveMatrixRoomAlias(auth.homeserver, auth.accessToken, query);
139
170
  if (!roomId) {
140
171
  return [];
141
172
  }
142
- return [
143
- {
144
- kind: "group",
145
- id: roomId,
146
- name: query,
147
- handle: query,
148
- } satisfies ChannelDirectoryEntry,
149
- ];
173
+ return [createGroupDirectoryEntry({ id: roomId, name: query, handle: query })];
150
174
  }
151
175
 
152
176
  if (query.startsWith("!")) {
153
- return [
154
- {
155
- kind: "group",
156
- id: query,
157
- name: query,
158
- } satisfies ChannelDirectoryEntry,
159
- ];
177
+ return [createGroupDirectoryEntry({ id: query, name: query })];
160
178
  }
161
179
 
162
180
  const joined = await fetchMatrixJson<MatrixJoinedRoomsResponse>({
@@ -1,7 +1,7 @@
1
1
  import type { ChannelGroupContext, GroupToolPolicyConfig } from "openclaw/plugin-sdk";
2
- import type { CoreConfig } from "./types.js";
3
2
  import { resolveMatrixAccountConfig } from "./matrix/accounts.js";
4
3
  import { resolveMatrixRoomConfig } from "./matrix/monitor/rooms.js";
4
+ import type { CoreConfig } from "./types.js";
5
5
 
6
6
  function stripLeadingPrefixCaseInsensitive(value: string, prefix: string): string {
7
7
  return value.toLowerCase().startsWith(prefix.toLowerCase())
@@ -1,14 +1,10 @@
1
1
  import { normalizeAccountId } from "openclaw/plugin-sdk/account-id";
2
- import type { CoreConfig } from "../../types.js";
3
- import type { MatrixActionClient, MatrixActionClientOpts } from "./types.js";
4
2
  import { getMatrixRuntime } from "../../runtime.js";
3
+ import type { CoreConfig } from "../../types.js";
5
4
  import { getActiveMatrixClient } from "../active-client.js";
6
- import {
7
- createMatrixClient,
8
- isBunRuntime,
9
- resolveMatrixAuth,
10
- resolveSharedMatrixClient,
11
- } from "../client.js";
5
+ import { createPreparedMatrixClient } from "../client-bootstrap.js";
6
+ import { isBunRuntime, resolveMatrixAuth, resolveSharedMatrixClient } from "../client.js";
7
+ import type { MatrixActionClient, MatrixActionClientOpts } from "./types.js";
12
8
 
13
9
  export function ensureNodeRuntime() {
14
10
  if (isBunRuntime()) {
@@ -42,24 +38,10 @@ export async function resolveActionClient(
42
38
  cfg: getMatrixRuntime().config.loadConfig() as CoreConfig,
43
39
  accountId,
44
40
  });
45
- const client = await createMatrixClient({
46
- homeserver: auth.homeserver,
47
- userId: auth.userId,
48
- accessToken: auth.accessToken,
49
- encryption: auth.encryption,
50
- localTimeoutMs: opts.timeoutMs,
41
+ const client = await createPreparedMatrixClient({
42
+ auth,
43
+ timeoutMs: opts.timeoutMs,
51
44
  accountId,
52
45
  });
53
- if (auth.encryption && client.crypto) {
54
- try {
55
- const joinedRooms = await client.getJoinedRooms();
56
- await (client.crypto as { prepare: (rooms?: string[]) => Promise<void> }).prepare(
57
- joinedRooms,
58
- );
59
- } catch {
60
- // Ignore crypto prep failures for one-off actions.
61
- }
62
- }
63
- await client.start();
64
46
  return { client, stopOnDone: true };
65
47
  }
@@ -0,0 +1,15 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { resolveMatrixActionLimit } from "./limits.js";
3
+
4
+ describe("resolveMatrixActionLimit", () => {
5
+ it("uses fallback for non-finite values", () => {
6
+ expect(resolveMatrixActionLimit(undefined, 20)).toBe(20);
7
+ expect(resolveMatrixActionLimit(Number.NaN, 20)).toBe(20);
8
+ });
9
+
10
+ it("normalizes finite numbers to positive integers", () => {
11
+ expect(resolveMatrixActionLimit(7.9, 20)).toBe(7);
12
+ expect(resolveMatrixActionLimit(0, 20)).toBe(1);
13
+ expect(resolveMatrixActionLimit(-3, 20)).toBe(1);
14
+ });
15
+ });
@@ -0,0 +1,6 @@
1
+ export function resolveMatrixActionLimit(raw: unknown, fallback: number): number {
2
+ if (typeof raw !== "number" || !Number.isFinite(raw)) {
3
+ return fallback;
4
+ }
5
+ return Math.max(1, Math.floor(raw));
6
+ }
@@ -1,5 +1,6 @@
1
1
  import { resolveMatrixRoomId, sendMessageMatrix } from "../send.js";
2
2
  import { resolveActionClient } from "./client.js";
3
+ import { resolveMatrixActionLimit } from "./limits.js";
3
4
  import { summarizeMatrixRawEvent } from "./summary.js";
4
5
  import {
5
6
  EventType,
@@ -95,10 +96,7 @@ export async function readMatrixMessages(
95
96
  const { client, stopOnDone } = await resolveActionClient(opts);
96
97
  try {
97
98
  const resolvedRoom = await resolveMatrixRoomId(client, roomId);
98
- const limit =
99
- typeof opts.limit === "number" && Number.isFinite(opts.limit)
100
- ? Math.max(1, Math.floor(opts.limit))
101
- : 20;
99
+ const limit = resolveMatrixActionLimit(opts.limit, 20);
102
100
  const token = opts.before?.trim() || opts.after?.trim() || undefined;
103
101
  const dir = opts.after ? "f" : "b";
104
102
  // @vector-im/matrix-bot-sdk uses doRequest for room messages
@@ -0,0 +1,74 @@
1
+ import type { MatrixClient } from "@vector-im/matrix-bot-sdk";
2
+ import { describe, expect, it, vi } from "vitest";
3
+ import { listMatrixPins, pinMatrixMessage, unpinMatrixMessage } from "./pins.js";
4
+
5
+ function createPinsClient(seedPinned: string[], knownBodies: Record<string, string> = {}) {
6
+ let pinned = [...seedPinned];
7
+ const getRoomStateEvent = vi.fn(async () => ({ pinned: [...pinned] }));
8
+ const sendStateEvent = vi.fn(
9
+ async (_roomId: string, _type: string, _key: string, payload: any) => {
10
+ pinned = [...payload.pinned];
11
+ },
12
+ );
13
+ const getEvent = vi.fn(async (_roomId: string, eventId: string) => {
14
+ const body = knownBodies[eventId];
15
+ if (!body) {
16
+ throw new Error("missing");
17
+ }
18
+ return {
19
+ event_id: eventId,
20
+ sender: "@alice:example.org",
21
+ type: "m.room.message",
22
+ origin_server_ts: 123,
23
+ content: { msgtype: "m.text", body },
24
+ };
25
+ });
26
+
27
+ return {
28
+ client: {
29
+ getRoomStateEvent,
30
+ sendStateEvent,
31
+ getEvent,
32
+ stop: vi.fn(),
33
+ } as unknown as MatrixClient,
34
+ getPinned: () => pinned,
35
+ sendStateEvent,
36
+ };
37
+ }
38
+
39
+ describe("matrix pins actions", () => {
40
+ it("pins a message once even when asked twice", async () => {
41
+ const { client, getPinned, sendStateEvent } = createPinsClient(["$a"]);
42
+
43
+ const first = await pinMatrixMessage("!room:example.org", "$b", { client });
44
+ const second = await pinMatrixMessage("!room:example.org", "$b", { client });
45
+
46
+ expect(first.pinned).toEqual(["$a", "$b"]);
47
+ expect(second.pinned).toEqual(["$a", "$b"]);
48
+ expect(getPinned()).toEqual(["$a", "$b"]);
49
+ expect(sendStateEvent).toHaveBeenCalledTimes(2);
50
+ });
51
+
52
+ it("unpinds only the selected message id", async () => {
53
+ const { client, getPinned } = createPinsClient(["$a", "$b", "$c"]);
54
+
55
+ const result = await unpinMatrixMessage("!room:example.org", "$b", { client });
56
+
57
+ expect(result.pinned).toEqual(["$a", "$c"]);
58
+ expect(getPinned()).toEqual(["$a", "$c"]);
59
+ });
60
+
61
+ it("lists pinned ids and summarizes only resolvable events", async () => {
62
+ const { client } = createPinsClient(["$a", "$missing"], { $a: "hello" });
63
+
64
+ const result = await listMatrixPins("!room:example.org", { client });
65
+
66
+ expect(result.pinned).toEqual(["$a", "$missing"]);
67
+ expect(result.events).toEqual([
68
+ expect.objectContaining({
69
+ eventId: "$a",
70
+ body: "hello",
71
+ }),
72
+ ]);
73
+ });
74
+ });
@@ -4,23 +4,22 @@ import { fetchEventSummary, readPinnedEvents } from "./summary.js";
4
4
  import {
5
5
  EventType,
6
6
  type MatrixActionClientOpts,
7
+ type MatrixActionClient,
7
8
  type MatrixMessageSummary,
8
9
  type RoomPinnedEventsEventContent,
9
10
  } from "./types.js";
10
11
 
11
- export async function pinMatrixMessage(
12
+ type ActionClient = MatrixActionClient["client"];
13
+
14
+ async function withResolvedPinRoom<T>(
12
15
  roomId: string,
13
- messageId: string,
14
- opts: MatrixActionClientOpts = {},
15
- ): Promise<{ pinned: string[] }> {
16
+ opts: MatrixActionClientOpts,
17
+ run: (client: ActionClient, resolvedRoom: string) => Promise<T>,
18
+ ): Promise<T> {
16
19
  const { client, stopOnDone } = await resolveActionClient(opts);
17
20
  try {
18
21
  const resolvedRoom = await resolveMatrixRoomId(client, roomId);
19
- const current = await readPinnedEvents(client, resolvedRoom);
20
- const next = current.includes(messageId) ? current : [...current, messageId];
21
- const payload: RoomPinnedEventsEventContent = { pinned: next };
22
- await client.sendStateEvent(resolvedRoom, EventType.RoomPinnedEvents, "", payload);
23
- return { pinned: next };
22
+ return await run(client, resolvedRoom);
24
23
  } finally {
25
24
  if (stopOnDone) {
26
25
  client.stop();
@@ -28,33 +27,46 @@ export async function pinMatrixMessage(
28
27
  }
29
28
  }
30
29
 
31
- export async function unpinMatrixMessage(
30
+ async function updateMatrixPins(
32
31
  roomId: string,
33
32
  messageId: string,
34
- opts: MatrixActionClientOpts = {},
33
+ opts: MatrixActionClientOpts,
34
+ update: (current: string[]) => string[],
35
35
  ): Promise<{ pinned: string[] }> {
36
- const { client, stopOnDone } = await resolveActionClient(opts);
37
- try {
38
- const resolvedRoom = await resolveMatrixRoomId(client, roomId);
36
+ return await withResolvedPinRoom(roomId, opts, async (client, resolvedRoom) => {
39
37
  const current = await readPinnedEvents(client, resolvedRoom);
40
- const next = current.filter((id) => id !== messageId);
38
+ const next = update(current);
41
39
  const payload: RoomPinnedEventsEventContent = { pinned: next };
42
40
  await client.sendStateEvent(resolvedRoom, EventType.RoomPinnedEvents, "", payload);
43
41
  return { pinned: next };
44
- } finally {
45
- if (stopOnDone) {
46
- client.stop();
47
- }
48
- }
42
+ });
43
+ }
44
+
45
+ export async function pinMatrixMessage(
46
+ roomId: string,
47
+ messageId: string,
48
+ opts: MatrixActionClientOpts = {},
49
+ ): Promise<{ pinned: string[] }> {
50
+ return await updateMatrixPins(roomId, messageId, opts, (current) =>
51
+ current.includes(messageId) ? current : [...current, messageId],
52
+ );
53
+ }
54
+
55
+ export async function unpinMatrixMessage(
56
+ roomId: string,
57
+ messageId: string,
58
+ opts: MatrixActionClientOpts = {},
59
+ ): Promise<{ pinned: string[] }> {
60
+ return await updateMatrixPins(roomId, messageId, opts, (current) =>
61
+ current.filter((id) => id !== messageId),
62
+ );
49
63
  }
50
64
 
51
65
  export async function listMatrixPins(
52
66
  roomId: string,
53
67
  opts: MatrixActionClientOpts = {},
54
68
  ): Promise<{ pinned: string[]; events: MatrixMessageSummary[] }> {
55
- const { client, stopOnDone } = await resolveActionClient(opts);
56
- try {
57
- const resolvedRoom = await resolveMatrixRoomId(client, roomId);
69
+ return await withResolvedPinRoom(roomId, opts, async (client, resolvedRoom) => {
58
70
  const pinned = await readPinnedEvents(client, resolvedRoom);
59
71
  const events = (
60
72
  await Promise.all(
@@ -68,9 +80,5 @@ export async function listMatrixPins(
68
80
  )
69
81
  ).filter((event): event is MatrixMessageSummary => Boolean(event));
70
82
  return { pinned, events };
71
- } finally {
72
- if (stopOnDone) {
73
- client.stop();
74
- }
75
- }
83
+ });
76
84
  }
@@ -0,0 +1,109 @@
1
+ import type { MatrixClient } from "@vector-im/matrix-bot-sdk";
2
+ import { describe, expect, it, vi } from "vitest";
3
+ import { listMatrixReactions, removeMatrixReactions } from "./reactions.js";
4
+
5
+ function createReactionsClient(params: {
6
+ chunk: Array<{
7
+ event_id?: string;
8
+ sender?: string;
9
+ key?: string;
10
+ }>;
11
+ userId?: string | null;
12
+ }) {
13
+ const doRequest = vi.fn(async (_method: string, _path: string, _query: any) => ({
14
+ chunk: params.chunk.map((item) => ({
15
+ event_id: item.event_id ?? "",
16
+ sender: item.sender ?? "",
17
+ content: item.key
18
+ ? {
19
+ "m.relates_to": {
20
+ rel_type: "m.annotation",
21
+ event_id: "$target",
22
+ key: item.key,
23
+ },
24
+ }
25
+ : {},
26
+ })),
27
+ }));
28
+ const getUserId = vi.fn(async () => params.userId ?? null);
29
+ const redactEvent = vi.fn(async () => undefined);
30
+
31
+ return {
32
+ client: {
33
+ doRequest,
34
+ getUserId,
35
+ redactEvent,
36
+ stop: vi.fn(),
37
+ } as unknown as MatrixClient,
38
+ doRequest,
39
+ redactEvent,
40
+ };
41
+ }
42
+
43
+ describe("matrix reaction actions", () => {
44
+ it("aggregates reactions by key and unique sender", async () => {
45
+ const { client, doRequest } = createReactionsClient({
46
+ chunk: [
47
+ { event_id: "$1", sender: "@alice:example.org", key: "👍" },
48
+ { event_id: "$2", sender: "@bob:example.org", key: "👍" },
49
+ { event_id: "$3", sender: "@alice:example.org", key: "👎" },
50
+ { event_id: "$4", sender: "@bot:example.org" },
51
+ ],
52
+ userId: "@bot:example.org",
53
+ });
54
+
55
+ const result = await listMatrixReactions("!room:example.org", "$msg", { client, limit: 2.9 });
56
+
57
+ expect(doRequest).toHaveBeenCalledWith(
58
+ "GET",
59
+ expect.stringContaining("/rooms/!room%3Aexample.org/relations/%24msg/"),
60
+ expect.objectContaining({ limit: 2 }),
61
+ );
62
+ expect(result).toEqual(
63
+ expect.arrayContaining([
64
+ expect.objectContaining({
65
+ key: "👍",
66
+ count: 2,
67
+ users: expect.arrayContaining(["@alice:example.org", "@bob:example.org"]),
68
+ }),
69
+ expect.objectContaining({
70
+ key: "👎",
71
+ count: 1,
72
+ users: ["@alice:example.org"],
73
+ }),
74
+ ]),
75
+ );
76
+ });
77
+
78
+ it("removes only current-user reactions matching emoji filter", async () => {
79
+ const { client, redactEvent } = createReactionsClient({
80
+ chunk: [
81
+ { event_id: "$1", sender: "@me:example.org", key: "👍" },
82
+ { event_id: "$2", sender: "@me:example.org", key: "👎" },
83
+ { event_id: "$3", sender: "@other:example.org", key: "👍" },
84
+ ],
85
+ userId: "@me:example.org",
86
+ });
87
+
88
+ const result = await removeMatrixReactions("!room:example.org", "$msg", {
89
+ client,
90
+ emoji: "👍",
91
+ });
92
+
93
+ expect(result).toEqual({ removed: 1 });
94
+ expect(redactEvent).toHaveBeenCalledTimes(1);
95
+ expect(redactEvent).toHaveBeenCalledWith("!room:example.org", "$1");
96
+ });
97
+
98
+ it("returns removed=0 when current user id is unavailable", async () => {
99
+ const { client, redactEvent } = createReactionsClient({
100
+ chunk: [{ event_id: "$1", sender: "@me:example.org", key: "👍" }],
101
+ userId: null,
102
+ });
103
+
104
+ const result = await removeMatrixReactions("!room:example.org", "$msg", { client });
105
+
106
+ expect(result).toEqual({ removed: 0 });
107
+ expect(redactEvent).not.toHaveBeenCalled();
108
+ });
109
+ });