Skip to content

Commit

Permalink
implement remaining message types
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh committed Jan 20, 2023
1 parent 5ba0c3d commit a7e01bd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
54 changes: 54 additions & 0 deletions spec/unit/rust-crypto/OutgoingRequestProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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();
Expand Down
13 changes: 12 additions & 1 deletion src/rust-crypto/OutgoingRequestProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import {
KeysClaimRequest,
KeysQueryRequest,
KeysUploadRequest,
RoomMessageRequest,
SignatureUploadRequest,
ToDeviceRequest,
} from "@matrix-org/matrix-sdk-crypto-js";

import { logger } from "../logger";
Expand Down Expand Up @@ -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 = "";
}
Expand Down

0 comments on commit a7e01bd

Please sign in to comment.