Files
matrix-rust-sdk/examples/wasm_command_bot/src/lib.rs
Benjamin Kampmann e8278b5d68 feat: add Result return value to sync_* (#1037)
Inspired by the changes in #1013 I was thinking about the use case for `sync_*` and how we handle error cases. Most notably while we give the callback the option to stop the loop, we don't really give an indication to the outside, how to interpret that cancellation: was there a failure? should we restart?

Take e.g. a connectivity issue on the wire, we'd constantly loop and just `warn`, what you might or might not see. Even if you handle that in the `sync_with_result_callback` and thus break the loop, the outer caller now still doesn't know whether everything is honky dory or whether they should restart. 

This Changes reworks that area by having all the `sync` return `Result<(), Error>`, where `()` means it was ended by the inner callback (which in `sync()` never occurs) or `Error` is the error either the inner `result_callback` found or the that was coming from the `send` in the first place. Thus allowing us to e.g. back down to sync as it was a dead wire or restart it if there was only a temporary problem. Making all that a just a bit more "rust-y".
2022-09-26 17:14:15 +02:00

104 lines
3.1 KiB
Rust

use matrix_sdk::{
config::SyncSettings,
deserialized_responses::SyncResponse,
ruma::{
events::{
room::message::{
MessageType, OriginalSyncRoomMessageEvent, RoomMessageEventContent,
TextMessageEventContent,
},
AnyMessageLikeEventContent, AnySyncMessageLikeEvent, AnySyncTimelineEvent,
SyncMessageLikeEvent,
},
RoomId,
},
Client, LoopCtrl,
};
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: &OriginalSyncRoomMessageEvent) {
let msg_body = if let OriginalSyncRoomMessageEvent {
content:
RoomMessageEventContent {
msgtype: MessageType::Text(TextMessageEventContent { body: msg_body, .. }),
..
},
..
} = event
{
msg_body
} else {
return;
};
console::log_1(&format!("Received message event {:?}", &msg_body).into());
if msg_body.contains("!party") {
let content = AnyMessageLikeEventContent::RoomMessage(
RoomMessageEventContent::text_plain("🎉🎊🥳 let's PARTY!! 🥳🎊🎉"),
);
println!("sending");
if let Some(room) = self.0.get_joined_room(room_id) {
// send our message to the room we found the "!party" command in
// the last parameter is an optional transaction id which we
// don't care about.
room.send(content, None).await.unwrap();
}
println!("message sent");
}
}
async fn on_sync_response(&self, response: SyncResponse) -> LoopCtrl {
console::log_1(&"Synced".to_owned().into());
for (room_id, room) in response.rooms.join {
for event in room.timeline.events {
if let Ok(AnySyncTimelineEvent::MessageLike(
AnySyncMessageLikeEvent::RoomMessage(SyncMessageLikeEvent::Original(ev)),
)) = event.event.deserialize()
{
self.on_room_message(&room_id, &ev).await
}
}
}
LoopCtrl::Continue
}
}
#[wasm_bindgen]
pub async fn run() -> Result<JsValue, JsValue> {
console_error_panic_hook::set_once();
let homeserver_url = "http://localhost:8008";
let username = "example";
let password = "wordpass";
let homeserver_url = Url::parse(homeserver_url).unwrap();
let client = Client::new(homeserver_url).await.unwrap();
client
.login_username(username, password)
.initial_device_display_name("rust-sdk-wasm")
.send()
.await
.unwrap();
let bot = WasmBot(client.clone());
client.sync_once(SyncSettings::default()).await.unwrap();
let settings = SyncSettings::default().token(client.sync_token().await.unwrap());
client.sync_with_callback(settings, |response| bot.on_sync_response(response)).await.unwrap();
Ok(JsValue::NULL)
}