From 59d19ffec6fc226d426244bc6f93aff7721e06f3 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 8 Jan 2024 15:12:43 +0100 Subject: [PATCH] feat: Introduce `UserId.clone`. This can be helpful when the `user_id` is dropped on the Rust side. --- src/identifiers.rs | 6 ++++++ tests/machine.test.ts | 8 +++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/identifiers.rs b/src/identifiers.rs index 96f36ac3b..0232c9b0c 100644 --- a/src/identifiers.rs +++ b/src/identifiers.rs @@ -53,6 +53,12 @@ impl UserId { pub fn to_string(&self) -> String { self.inner.as_str().to_owned() } + + /// Clone this `UserId`. + #[wasm_bindgen(js_name = "clone")] + pub fn clone_me(&self) -> Self { + self.clone() + } } /// A Matrix key ID. diff --git a/tests/machine.test.ts b/tests/machine.test.ts index 50cc6a2af..51834dbf7 100644 --- a/tests/machine.test.ts +++ b/tests/machine.test.ts @@ -242,7 +242,7 @@ describe(OlmMachine.name, () => { test("can update tracked users", async () => { const m = await machine(); - expect(await m.updateTrackedUsers([new UserId("@alice:example.org")])).toStrictEqual(undefined); + expect(await m.updateTrackedUsers([user.clone()])).toStrictEqual(undefined); }); test("can receive sync changes", async () => { @@ -509,7 +509,9 @@ describe(OlmMachine.name, () => { }); test("can share a room key", async () => { - const requests = await m.shareRoomKey(room, [new UserId("@example:localhost")], new EncryptionSettings()); + const other_user_id = new UserId("@example:localhost"); + + const requests = await m.shareRoomKey(room, [other_user_id.clone()], new EncryptionSettings()); expect(requests).toHaveLength(1); expect(requests[0]).toBeInstanceOf(ToDeviceRequest); @@ -523,7 +525,7 @@ describe(OlmMachine.name, () => { await m.markRequestAsSent(requests[0].id, RequestType.ToDevice, "{}"); - const requestsAfterMarkedAsSent = await m.shareRoomKey(room, [new UserId("@example:localhost")], new EncryptionSettings()); + const requestsAfterMarkedAsSent = await m.shareRoomKey(room, [other_user_id.clone()], new EncryptionSettings()); expect(requestsAfterMarkedAsSent).toHaveLength(0); });