diff --git a/package.json b/package.json index 4796be39956..65c3ceb3b13 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ ], "dependencies": { "@babel/runtime": "^7.12.5", - "@matrix-org/matrix-sdk-crypto-js": "^0.1.0-alpha.2", + "@matrix-org/matrix-sdk-crypto-js": "^0.1.0-alpha.3", "another-json": "^0.2.0", "bs58": "^5.0.0", "content-type": "^1.0.4", diff --git a/spec/unit/rust-crypto/OutgoingRequestProcessor.spec.ts b/spec/unit/rust-crypto/OutgoingRequestProcessor.spec.ts index 6b9a5c7ca9d..b86e573353c 100644 --- a/spec/unit/rust-crypto/OutgoingRequestProcessor.spec.ts +++ b/spec/unit/rust-crypto/OutgoingRequestProcessor.spec.ts @@ -22,7 +22,9 @@ import { KeysClaimRequest, KeysQueryRequest, KeysUploadRequest, + RoomMessageRequest, SignatureUploadRequest, + ToDeviceRequest, } from "@matrix-org/matrix-sdk-crypto-js"; import { TypedEventEmitter } from "../../../src/models/typed-event-emitter"; @@ -103,6 +105,58 @@ describe("OutgoingRequestProcessor", () => { }); } + it("should handle ToDeviceRequests", async () => { + const testBody = '{ "foo": "bar" }'; + const outgoingRequest = new ToDeviceRequest("1234", "test/type", "test/txnid", testBody); + + const reqProm = processor.makeOutgoingRequest(outgoingRequest); + + const testResponse = '{ "result": 1 }'; + httpBackend + .when("PUT", "/_matrix") + .check((req) => { + expect(req.path).toEqual("https://example.com/_matrix/client/v3/sendToDevice/test%2Ftype/test%2Ftxnid"); + expect(req.rawData).toEqual(testBody); + expect(req.headers["Accept"]).toEqual("application/json"); + expect(req.headers["Content-Type"]).toEqual("application/json"); + }) + .respond(200, testResponse, true); + + const markSentCallPromise = awaitCallToMarkAsSent(); + await httpBackend.flushAllExpected(); + + await Promise.all([reqProm, markSentCallPromise]); + expect(olmMachine.markRequestAsSent).toHaveBeenCalledWith("1234", outgoingRequest.type, testResponse); + httpBackend.verifyNoOutstandingRequests(); + }); + + it("should handle RoomMessageRequests", async () => { + const testBody = '{ "foo": "bar" }'; + const outgoingRequest = new RoomMessageRequest("1234", "test/room", "test/txnid", "test/type", testBody); + + const reqProm = processor.makeOutgoingRequest(outgoingRequest); + + const testResponse = '{ "result": 1 }'; + httpBackend + .when("PUT", "/_matrix") + .check((req) => { + expect(req.path).toEqual( + "https://example.com/_matrix/client/v3/room/test%2Froom/send/test%2Ftype/test%2Ftxnid", + ); + expect(req.rawData).toEqual(testBody); + expect(req.headers["Accept"]).toEqual("application/json"); + expect(req.headers["Content-Type"]).toEqual("application/json"); + }) + .respond(200, testResponse, true); + + const markSentCallPromise = awaitCallToMarkAsSent(); + await httpBackend.flushAllExpected(); + + await Promise.all([reqProm, markSentCallPromise]); + expect(olmMachine.markRequestAsSent).toHaveBeenCalledWith("1234", outgoingRequest.type, testResponse); + httpBackend.verifyNoOutstandingRequests(); + }); + it("does not explode with unknown requests", async () => { const outgoingRequest = { id: "5678", type: 987 }; const markSentCallPromise = awaitCallToMarkAsSent(); diff --git a/src/rust-crypto/OutgoingRequestProcessor.ts b/src/rust-crypto/OutgoingRequestProcessor.ts index ecbe6dc1e04..c0163f8525c 100644 --- a/src/rust-crypto/OutgoingRequestProcessor.ts +++ b/src/rust-crypto/OutgoingRequestProcessor.ts @@ -20,7 +20,9 @@ import { KeysClaimRequest, KeysQueryRequest, KeysUploadRequest, + RoomMessageRequest, SignatureUploadRequest, + ToDeviceRequest, } from "@matrix-org/matrix-sdk-crypto-js"; import { logger } from "../logger"; @@ -67,8 +69,17 @@ export class OutgoingRequestProcessor { resp = await this.rawJsonRequest(Method.Post, "/_matrix/client/v3/keys/signatures/upload", {}, msg.body); } else if (msg instanceof KeysBackupRequest) { resp = await this.rawJsonRequest(Method.Put, "/_matrix/client/v3/room_keys/keys", {}, msg.body); + } else if (msg instanceof ToDeviceRequest) { + const path = + `/_matrix/client/v3/sendToDevice/${encodeURIComponent(msg.event_type)}/` + + encodeURIComponent(msg.txn_id); + resp = await this.rawJsonRequest(Method.Put, path, {}, msg.body); + } else if (msg instanceof RoomMessageRequest) { + const path = + `/_matrix/client/v3/room/${encodeURIComponent(msg.room_id)}/send/` + + `${encodeURIComponent(msg.event_type)}/${encodeURIComponent(msg.txn_id)}`; + resp = await this.rawJsonRequest(Method.Put, path, {}, msg.body); } else { - // TODO: ToDeviceRequest, RoomMessageRequest logger.warn("Unsupported outgoing message", Object.getPrototypeOf(msg)); resp = ""; }