From 12bf0f53a81d86c3c14659f6ada2ac4231587670 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 22 Mar 2021 20:24:30 +0100 Subject: [PATCH] matrix-sdk: Fix the WASM example --- .../examples/wasm_command_bot/Cargo.toml | 12 +++---- .../examples/wasm_command_bot/README.md | 4 +-- .../examples/wasm_command_bot/package.json | 2 +- .../examples/wasm_command_bot/src/lib.rs | 32 +++++++++++++------ 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/matrix_sdk/examples/wasm_command_bot/Cargo.toml b/matrix_sdk/examples/wasm_command_bot/Cargo.toml index 9735e0e7..91ad579d 100644 --- a/matrix_sdk/examples/wasm_command_bot/Cargo.toml +++ b/matrix_sdk/examples/wasm_command_bot/Cargo.toml @@ -10,15 +10,15 @@ edition = "2018" crate-type = ["cdylib"] [dependencies] -url = "2.1.1" -wasm-bindgen = { version = "0.2.62", features = ["serde-serialize"] } -wasm-bindgen-futures = "0.4.12" -console_error_panic_hook = "*" -web-sys = { version = "0.3.39", features = ["console"] } +url = "2.2.1" +wasm-bindgen = { version = "0.2.72", features = ["serde-serialize"] } +wasm-bindgen-futures = "0.4.22" +console_error_panic_hook = "0.1.6" +web-sys = { version = "0.3.49", features = ["console"] } [dependencies.matrix-sdk] path = "../.." default-features = false -features = ["native-tls"] +features = ["native-tls", "encryption"] [workspace] diff --git a/matrix_sdk/examples/wasm_command_bot/README.md b/matrix_sdk/examples/wasm_command_bot/README.md index 49d1c688..d71bd422 100644 --- a/matrix_sdk/examples/wasm_command_bot/README.md +++ b/matrix_sdk/examples/wasm_command_bot/README.md @@ -7,6 +7,4 @@ You can build the example locally with: and then visiting http://localhost:8080 in a browser should run the example! -Note: Encryption isn't supported yet - -This example is loosely based off of [this example](https://github.com/seanmonstar/reqwest/tree/master/examples/wasm_github_fetch), an example usage of `fetch` from `wasm-bindgen`. \ No newline at end of file +This example is loosely based off of [this example](https://github.com/seanmonstar/reqwest/tree/master/examples/wasm_github_fetch), an example usage of `fetch` from `wasm-bindgen`. diff --git a/matrix_sdk/examples/wasm_command_bot/package.json b/matrix_sdk/examples/wasm_command_bot/package.json index 2a84b1f7..f3f40092 100644 --- a/matrix_sdk/examples/wasm_command_bot/package.json +++ b/matrix_sdk/examples/wasm_command_bot/package.json @@ -5,8 +5,8 @@ }, "devDependencies": { "@wasm-tool/wasm-pack-plugin": "1.0.1", - "text-encoding": "^0.7.0", "html-webpack-plugin": "^3.2.0", + "text-encoding": "^0.7.0", "webpack": "^4.29.4", "webpack-cli": "^3.1.1", "webpack-dev-server": "^3.1.0" diff --git a/matrix_sdk/examples/wasm_command_bot/src/lib.rs b/matrix_sdk/examples/wasm_command_bot/src/lib.rs index 097cbb8e..e7e8c0e5 100644 --- a/matrix_sdk/examples/wasm_command_bot/src/lib.rs +++ b/matrix_sdk/examples/wasm_command_bot/src/lib.rs @@ -1,7 +1,7 @@ use matrix_sdk::{ deserialized_responses::SyncResponse, events::{ - room::message::{MessageEventContent, TextMessageEventContent}, + room::message::{MessageEventContent, MessageType, TextMessageEventContent}, AnyMessageEventContent, AnySyncMessageEvent, AnySyncRoomEvent, SyncMessageEvent, }, identifiers::RoomId, @@ -17,35 +17,49 @@ impl WasmBot { async fn on_room_message( &self, room_id: &RoomId, - event: SyncMessageEvent, + event: &SyncMessageEvent, ) { let msg_body = if let SyncMessageEvent { - content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }), + content: + MessageEventContent { + msgtype: MessageType::Text(TextMessageEventContent { body: msg_body, .. }), + .. + }, .. } = event { - msg_body.clone() + msg_body } else { return; }; console::log_1(&format!("Received message event {:?}", &msg_body).into()); - if msg_body.starts_with("!party") { - let content = AnyMessageEventContent::RoomMessage(MessageEventContent::Text( - TextMessageEventContent::plain("🎉🎊🥳 let's PARTY with wasm!! 🥳🎊🎉".to_string()), + if msg_body.contains("!party") { + let content = AnyMessageEventContent::RoomMessage(MessageEventContent::text_plain( + "🎉🎊🥳 let's PARTY!! 🥳🎊🎉", )); - self.0.room_send(&room_id, content, None).await.unwrap(); + println!("sending"); + + self.0 + // send our message to the room we found the "!party" command in + // the last parameter is an optional Uuid which we don't care about. + .room_send(room_id, content, None) + .await + .unwrap(); + + println!("message sent"); } } + async fn on_sync_response(&self, response: SyncResponse) -> LoopCtrl { console::log_1(&"Synced".to_string().into()); for (room_id, room) in response.rooms.join { for event in room.timeline.events { if let AnySyncRoomEvent::Message(AnySyncMessageEvent::RoomMessage(ev)) = event { - self.on_room_message(&room_id, ev).await + self.on_room_message(&room_id, &ev).await } } }