2020-05-08 14:12:21 +00:00
|
|
|
use matrix_sdk::{
|
2020-05-12 12:30:33 +00:00
|
|
|
api::r0::sync::sync_events::Response as SyncResponse,
|
2020-06-20 21:18:20 +00:00
|
|
|
events::room::message::{MessageEventContent, TextMessageEventContent},
|
|
|
|
events::MessageEventStub,
|
2020-05-08 14:12:21 +00:00
|
|
|
identifiers::RoomId,
|
2020-05-12 12:30:33 +00:00
|
|
|
Client, ClientConfig, SyncSettings,
|
2020-05-08 14:12:21 +00:00
|
|
|
};
|
|
|
|
use url::Url;
|
|
|
|
use wasm_bindgen::prelude::*;
|
2020-05-12 12:30:33 +00:00
|
|
|
use web_sys::console;
|
|
|
|
|
|
|
|
struct WasmBot(Client);
|
|
|
|
|
|
|
|
impl WasmBot {
|
2020-06-20 21:18:20 +00:00
|
|
|
async fn on_room_message(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
event: MessageEventStub<MessageEventContent>,
|
|
|
|
) {
|
|
|
|
let msg_body = if let MessageEventStub {
|
2020-05-12 12:30:33 +00:00
|
|
|
content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }),
|
|
|
|
..
|
2020-06-20 21:18:20 +00:00
|
|
|
} = event
|
2020-05-12 12:30:33 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-08 14:12:21 +00:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub async fn run() -> Result<JsValue, JsValue> {
|
|
|
|
let homeserver_url = "http://localhost:8008";
|
|
|
|
let username = "user";
|
|
|
|
let password = "password";
|
|
|
|
|
|
|
|
let client_config = ClientConfig::new();
|
|
|
|
let homeserver_url = Url::parse(&homeserver_url).unwrap();
|
2020-05-22 14:19:30 +00:00
|
|
|
let client = Client::new_with_config(homeserver_url, client_config).unwrap();
|
2020-05-08 14:12:21 +00:00
|
|
|
|
|
|
|
client
|
2020-05-12 12:30:33 +00:00
|
|
|
.login(username, password, None, Some("rust-sdk-wasm"))
|
2020-05-08 14:12:21 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2020-05-12 12:30:33 +00:00
|
|
|
let bot = WasmBot(client.clone());
|
2020-05-08 14:12:21 +00:00
|
|
|
|
2020-05-12 12:30:33 +00:00
|
|
|
client.sync(SyncSettings::default()).await.unwrap();
|
2020-05-08 14:12:21 +00:00
|
|
|
|
2020-05-12 12:30:33 +00:00
|
|
|
let settings = SyncSettings::default().token(client.sync_token().await.unwrap());
|
|
|
|
client
|
|
|
|
.sync_forever(settings, |response| bot.on_sync_response(response))
|
|
|
|
.await;
|
2020-05-08 14:12:21 +00:00
|
|
|
|
|
|
|
Ok(JsValue::NULL)
|
|
|
|
}
|