Skip to content

Commit a7e01bd

Browse files
committed
implement remaining message types
1 parent 5ba0c3d commit a7e01bd

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
],
5656
"dependencies": {
5757
"@babel/runtime": "^7.12.5",
58-
"@matrix-org/matrix-sdk-crypto-js": "^0.1.0-alpha.2",
58+
"@matrix-org/matrix-sdk-crypto-js": "^0.1.0-alpha.3",
5959
"another-json": "^0.2.0",
6060
"bs58": "^5.0.0",
6161
"content-type": "^1.0.4",

spec/unit/rust-crypto/OutgoingRequestProcessor.spec.ts

+54
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import {
2222
KeysClaimRequest,
2323
KeysQueryRequest,
2424
KeysUploadRequest,
25+
RoomMessageRequest,
2526
SignatureUploadRequest,
27+
ToDeviceRequest,
2628
} from "@matrix-org/matrix-sdk-crypto-js";
2729

2830
import { TypedEventEmitter } from "../../../src/models/typed-event-emitter";
@@ -103,6 +105,58 @@ describe("OutgoingRequestProcessor", () => {
103105
});
104106
}
105107

108+
it("should handle ToDeviceRequests", async () => {
109+
const testBody = '{ "foo": "bar" }';
110+
const outgoingRequest = new ToDeviceRequest("1234", "test/type", "test/txnid", testBody);
111+
112+
const reqProm = processor.makeOutgoingRequest(outgoingRequest);
113+
114+
const testResponse = '{ "result": 1 }';
115+
httpBackend
116+
.when("PUT", "/_matrix")
117+
.check((req) => {
118+
expect(req.path).toEqual("https://example.com/_matrix/client/v3/sendToDevice/test%2Ftype/test%2Ftxnid");
119+
expect(req.rawData).toEqual(testBody);
120+
expect(req.headers["Accept"]).toEqual("application/json");
121+
expect(req.headers["Content-Type"]).toEqual("application/json");
122+
})
123+
.respond(200, testResponse, true);
124+
125+
const markSentCallPromise = awaitCallToMarkAsSent();
126+
await httpBackend.flushAllExpected();
127+
128+
await Promise.all([reqProm, markSentCallPromise]);
129+
expect(olmMachine.markRequestAsSent).toHaveBeenCalledWith("1234", outgoingRequest.type, testResponse);
130+
httpBackend.verifyNoOutstandingRequests();
131+
});
132+
133+
it("should handle RoomMessageRequests", async () => {
134+
const testBody = '{ "foo": "bar" }';
135+
const outgoingRequest = new RoomMessageRequest("1234", "test/room", "test/txnid", "test/type", testBody);
136+
137+
const reqProm = processor.makeOutgoingRequest(outgoingRequest);
138+
139+
const testResponse = '{ "result": 1 }';
140+
httpBackend
141+
.when("PUT", "/_matrix")
142+
.check((req) => {
143+
expect(req.path).toEqual(
144+
"https://example.com/_matrix/client/v3/room/test%2Froom/send/test%2Ftype/test%2Ftxnid",
145+
);
146+
expect(req.rawData).toEqual(testBody);
147+
expect(req.headers["Accept"]).toEqual("application/json");
148+
expect(req.headers["Content-Type"]).toEqual("application/json");
149+
})
150+
.respond(200, testResponse, true);
151+
152+
const markSentCallPromise = awaitCallToMarkAsSent();
153+
await httpBackend.flushAllExpected();
154+
155+
await Promise.all([reqProm, markSentCallPromise]);
156+
expect(olmMachine.markRequestAsSent).toHaveBeenCalledWith("1234", outgoingRequest.type, testResponse);
157+
httpBackend.verifyNoOutstandingRequests();
158+
});
159+
106160
it("does not explode with unknown requests", async () => {
107161
const outgoingRequest = { id: "5678", type: 987 };
108162
const markSentCallPromise = awaitCallToMarkAsSent();

src/rust-crypto/OutgoingRequestProcessor.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import {
2020
KeysClaimRequest,
2121
KeysQueryRequest,
2222
KeysUploadRequest,
23+
RoomMessageRequest,
2324
SignatureUploadRequest,
25+
ToDeviceRequest,
2426
} from "@matrix-org/matrix-sdk-crypto-js";
2527

2628
import { logger } from "../logger";
@@ -67,8 +69,17 @@ export class OutgoingRequestProcessor {
6769
resp = await this.rawJsonRequest(Method.Post, "/_matrix/client/v3/keys/signatures/upload", {}, msg.body);
6870
} else if (msg instanceof KeysBackupRequest) {
6971
resp = await this.rawJsonRequest(Method.Put, "/_matrix/client/v3/room_keys/keys", {}, msg.body);
72+
} else if (msg instanceof ToDeviceRequest) {
73+
const path =
74+
`/_matrix/client/v3/sendToDevice/${encodeURIComponent(msg.event_type)}/` +
75+
encodeURIComponent(msg.txn_id);
76+
resp = await this.rawJsonRequest(Method.Put, path, {}, msg.body);
77+
} else if (msg instanceof RoomMessageRequest) {
78+
const path =
79+
`/_matrix/client/v3/room/${encodeURIComponent(msg.room_id)}/send/` +
80+
`${encodeURIComponent(msg.event_type)}/${encodeURIComponent(msg.txn_id)}`;
81+
resp = await this.rawJsonRequest(Method.Put, path, {}, msg.body);
7082
} else {
71-
// TODO: ToDeviceRequest, RoomMessageRequest
7283
logger.warn("Unsupported outgoing message", Object.getPrototypeOf(msg));
7384
resp = "";
7485
}

0 commit comments

Comments
 (0)