turt2live / matrix-bot-sdk

TypeScript/JavaScript SDK for Matrix bots
MIT License
199 stars 69 forks source link

Error while using client.crypto.decryptRoomEvent #368

Open alexgaill opened 2 months ago

alexgaill commented 2 months ago

Describe the bug I want to get the event history of a room and call matrix api: "GET", /_matrix/client/v3/rooms/{roomId}/messages After that, i need to decrypt every m.room.encrypted and use MatrixClient().crypto.decryptRoomEvent.

I got this error:

Error: Expect value to be String, but received Undefined at CryptoClient.decryptRoomEvent (C:\Users\Alexandre\Desktop\dev\minint\bot\node_modules\matrix-bot-sdk\lib\e2ee\CryptoClient.js:195:53) at descriptor.value (C:\Users\Alexandre\Desktop\dev\minint\bot\node_modules\matrix-bot-sdk\lib\e2ee\decorators.js:33:35) at getRoomEvents (file:///C:/Users/Alexandre/Desktop/dev/minint/bot/dist/commands/domain/exportRoom.js:29:70) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async exportRoom (file:///C:/Users/Alexandre/Desktop/dev/minint/bot/dist/commands/domain/exportRoom.js:2:20) { code: 'InvalidArg' }

To Reproduce My code:

async function getRoomEvents(client: MatrixClient, roomId: string): Promise<RoomEvent[]> {
  let events: RoomEvent[] = [];
  try {
    let endToken = undefined;
    do {
      const response = await client.doRequest("GET", `/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/messages`, {
        from: endToken,
        dir: "b",
        limit: 100
      });
      for (const event of response.chunk) {
        if (event.type === 'm.room.encrypted') {
          const decryptedMessage = await client.crypto.decryptRoomEvent(event, roomId);
          event.push(decryptedMessage)
        } else {
          events.push(event);
        }
      }
      endToken = response.end;
    } while (endToken != null);
  } catch (error: any) {
    console.error(`Erreur lors de la récupération des événements :`, error);
  }
  return events;
}

Expected behavior Get decrypted messages event.