Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Changelog

## 0.2.1

### New

- Automatic oversized message splitting: text messages rejected with `M_TOO_LARGE` (413) are retried as plain-text chunks (~12 KB each)
- Thread reply metadata: messages posted to threads now include `m.in_reply_to`, with optional `matrixReplyToEventId` override

### Fixes

- Attachments sent alongside text no longer incorrectly carry the reply-to relationship
- Incoming formatted messages were parsed twice; removed redundant `<mx-reply>` pre-strip pass
- `matrixSDKLogConfigured` flag no longer latches when `setLevel` is missing from the SDK logger

### Changes

- Bump `chat` SDK to 4.25.0
- Move `@chat-adapter/state-memory` and `@chat-adapter/state-redis` to devDependencies

## 0.2.0

### New
Expand Down
23 changes: 15 additions & 8 deletions e2e/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
nonce,
shutdownParticipant,
sleep,
waitForCondition,
waitForEvent,
waitForEncryptedRoom,
waitForFetchedMessage,
Expand Down Expand Up @@ -470,11 +471,11 @@ describe.skipIf(!hasCoreCredentials)("E2E Matrix Adapter", () => {
]);

const latestOffline = offlinePosts[offlinePosts.length - 1];
const caughtUpMessage = await waitForFetchedMessage(
const caughtUpMessage = await waitForMatchingMessage(
bot.adapter,
bot.adapter.encodeThreadId({ roomID: restartRoomID }),
latestOffline.id,
(message) => message.text.includes(restartTag),
(message) =>
message.id === latestOffline.id && message.text.includes(restartTag),
60_000
);
expect(caughtUpMessage.text).toContain(restartTag);
Expand Down Expand Up @@ -626,11 +627,10 @@ describe.skipIf(!hasCoreCredentials)("E2E Matrix Adapter", () => {
});
await sender.adapter.postMessage(threadId, `Thread reply ${replyTag}`);

await waitForFetchedMessage(
await waitForMatchingMessage(
bot.adapter,
bot.adapter.encodeThreadId({ roomID: threadListRoomID }),
rootPosted.id,
(message) => message.text.includes(rootTag)
(message) => message.id === rootPosted.id && message.text.includes(rootTag)
);
await waitForMatchingMessage(
bot.adapter,
Expand All @@ -653,8 +653,15 @@ describe.skipIf(!hasCoreCredentials)("E2E Matrix Adapter", () => {
expect(threadInfo.isDM).toBe(false);
expect(threadInfo.metadata?.roomID).toBe(threadListRoomID);

const threads = await bot.adapter.listThreads(channelId, { limit: 20 });
const summary = threads.threads.find((thread) => thread.id === threadId);
let summary:
| Awaited<ReturnType<typeof bot.adapter.listThreads>>["threads"][number]
| undefined;
await waitForCondition(async () => {
const threads = await bot.adapter.listThreads(channelId, { limit: 20 });
summary = threads.threads.find((thread) => thread.id === threadId);
return Boolean(summary && (summary.replyCount ?? 0) >= 1);
}, 45_000);

expect(summary).toBeTruthy();
expect(summary?.rootMessage.id).toBe(rootPosted.id);
expect(summary?.rootMessage.text).toContain(rootTag);
Expand Down
41 changes: 37 additions & 4 deletions e2e/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { randomBytes } from "node:crypto";
import { Chat, type Message, type ReactionEvent, type StateAdapter } from "chat";
import { createMemoryState } from "@chat-adapter/state-memory";
import { createRedisState } from "@chat-adapter/state-redis";
import "fake-indexeddb/auto";
import { EventType } from "matrix-js-sdk";
import type { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk";
import { MatrixAdapter } from "../src/index";
Expand Down Expand Up @@ -61,6 +62,7 @@ export async function createParticipantFromSession(opts: {
state?: StateAdapter;
}): Promise<E2EParticipant> {
const state = opts.state ?? createE2EState(opts.name);
const cryptoDatabasePrefix = createE2EIndexedDBPrefix(opts.session);
const adapter = new MatrixAdapter({
baseURL: env.baseURL,
auth: {
Expand All @@ -71,7 +73,8 @@ export async function createParticipantFromSession(opts: {
deviceID: opts.session.deviceID,
inviteAutoJoin: {},
e2ee: {
useIndexedDB: false,
cryptoDatabasePrefix,
useIndexedDB: true,
},
recoveryKey: opts.recoveryKey,
});
Expand Down Expand Up @@ -136,6 +139,14 @@ type MatrixLoginResponse = {
userID: string;
};

function createE2EIndexedDBPrefix(session: MatrixLoginResponse): string {
return `matrix-chat-adapter-e2e-${encodeForIndexedDBName(session.userID)}-${encodeForIndexedDBName(session.deviceID)}`;
}

function encodeForIndexedDBName(value: string): string {
return Buffer.from(value, "utf8").toString("base64url");
}

function generateDeviceID(): string {
return `E2E_${randomBytes(8).toString("hex").toUpperCase()}`;
}
Expand Down Expand Up @@ -283,15 +294,37 @@ export function waitForEvent<T>(
}

export async function waitForCondition(
condition: () => boolean,
condition: () => boolean | Promise<boolean>,
timeoutMs = 10_000,
intervalMs = 250
): Promise<void> {
const startedAt = Date.now();

while (true) {
if (condition()) {
return;
const remainingMs = timeoutMs - (Date.now() - startedAt);
if (remainingMs <= 0) {
throw new Error(`waitForCondition timed out after ${timeoutMs}ms`);
}

let timeout: ReturnType<typeof setTimeout> | undefined;
try {
const matched = await Promise.race([
Promise.resolve().then(condition),
new Promise<never>((_, reject) => {
timeout = setTimeout(() => {
reject(new Error(`waitForCondition timed out after ${timeoutMs}ms`));
}, remainingMs);
timeout.unref?.();
}),
]);

if (matched) {
return;
}
} finally {
if (timeout) {
clearTimeout(timeout);
}
}

if (Date.now() - startedAt >= timeoutMs) {
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@beeper/chat-adapter-matrix",
"version": "0.2.0",
"version": "0.2.1",
"description": "Matrix adapter for chat",
"engines": {
"node": ">=22"
Expand Down Expand Up @@ -33,18 +33,19 @@
"clean": "rm -rf dist"
},
"dependencies": {
"@chat-adapter/state-memory": "^4.17.0",
"@chat-adapter/state-redis": "^4.17.0",
"chat": "^4.17.0",
"chat": "^4.25.0",
"marked": "^15.0.12",
"matrix-js-sdk": "^41.0.0",
"node-html-parser": "^7.1.0"
},
"devDependencies": {
"@chat-adapter/state-memory": "^4.25.0",
"@chat-adapter/state-redis": "^4.25.0",
"@eslint/js": "^10.0.1",
"@types/node": "^22.10.2",
"@vitest/coverage-v8": "^2.1.8",
"eslint": "^10.0.2",
"fake-indexeddb": "^6.2.4",
"tsup": "^8.3.5",
"typescript": "^5.7.2",
"typescript-eslint": "^8.56.1",
Expand Down
47 changes: 28 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading