diff --git a/matrix_sdk/examples/wasm_send_message/Cargo.toml b/matrix_sdk/examples/wasm_send_message/Cargo.toml index 3174fff5..397ef6c8 100644 --- a/matrix_sdk/examples/wasm_send_message/Cargo.toml +++ b/matrix_sdk/examples/wasm_send_message/Cargo.toml @@ -14,6 +14,7 @@ matrix-sdk = { path = "../..", default-features = false, features = ["encryption url = "2.1.1" wasm-bindgen = { version = "0.2.62", features = ["serde-serialize"] } wasm-bindgen-futures = "0.4.12" +web-sys = { version = "0.3.39", features = ["console"] } [workspace] diff --git a/matrix_sdk/examples/wasm_send_message/src/lib.rs b/matrix_sdk/examples/wasm_send_message/src/lib.rs index 7e4a58e9..3b1cf231 100644 --- a/matrix_sdk/examples/wasm_send_message/src/lib.rs +++ b/matrix_sdk/examples/wasm_send_message/src/lib.rs @@ -1,11 +1,50 @@ use matrix_sdk::{ - events::room::message::{MessageEventContent, TextMessageEventContent}, + api::r0::sync::sync_events::Response as SyncResponse, + events::collections::all::RoomEvent, + events::room::message::{MessageEvent, MessageEventContent, TextMessageEventContent}, identifiers::RoomId, - Client, ClientConfig, + Client, ClientConfig, SyncSettings, }; -use std::convert::TryFrom; use url::Url; use wasm_bindgen::prelude::*; +use web_sys::console; + +struct WasmBot(Client); + +impl WasmBot { + async fn on_room_message(&self, room_id: &RoomId, event: RoomEvent) { + let msg_body = if let RoomEvent::RoomMessage(MessageEvent { + content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }), + .. + }) = event + { + msg_body.clone() + } else { + return; + }; + + console::log_1(&format!("Received message event {:?}", &msg_body).into()); + + if msg_body.starts_with("!party") { + let content = MessageEventContent::Text(TextMessageEventContent::new_plain( + "🎉🎊🥳 let's PARTY with wasm!! 🥳🎊🎉".to_string(), + )); + + self.0.room_send(&room_id, content, None).await.unwrap(); + } + } + async fn on_sync_response(&self, response: SyncResponse) { + console::log_1(&format!("Synced").into()); + + for (room_id, room) in response.rooms.join { + for event in room.timeline.events { + if let Ok(event) = event.deserialize() { + self.on_room_message(&room_id, event).await + } + } + } + } +} #[wasm_bindgen] pub async fn run() -> Result { @@ -18,20 +57,18 @@ pub async fn run() -> Result { let client = Client::new_with_config(homeserver_url, None, client_config).unwrap(); client - .login(username, password, None, Some("rust-sdk")) + .login(username, password, None, Some("rust-sdk-wasm")) .await .unwrap(); - let room_id = RoomId::try_from("!KpLWMcXcHKDMfEYNqA:localhost").unwrap(); + let bot = WasmBot(client.clone()); - let content = MessageEventContent::Text(TextMessageEventContent { - body: "hello from wasm".to_string(), - format: None, - formatted_body: None, - relates_to: None, - }); + client.sync(SyncSettings::default()).await.unwrap(); - client.room_send(&room_id, content, None).await.unwrap(); + let settings = SyncSettings::default().token(client.sync_token().await.unwrap()); + client + .sync_forever(settings, |response| bot.on_sync_response(response)) + .await; Ok(JsValue::NULL) }