2020-05-08 14:12:21 +00:00
|
|
|
use matrix_sdk::{
|
2021-01-22 17:14:08 +00:00
|
|
|
deserialized_responses::SyncResponse,
|
2020-06-25 00:30:53 +00:00
|
|
|
events::{
|
|
|
|
room::message::{MessageEventContent, TextMessageEventContent},
|
2020-08-26 14:19:39 +00:00
|
|
|
AnyMessageEventContent, AnySyncMessageEvent, AnySyncRoomEvent, SyncMessageEvent,
|
2020-06-25 00:30:53 +00:00
|
|
|
},
|
2020-05-08 14:12:21 +00:00
|
|
|
identifiers::RoomId,
|
2021-01-22 17:14:08 +00:00
|
|
|
Client, LoopCtrl, 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,
|
2020-07-18 12:37:43 +00:00
|
|
|
event: SyncMessageEvent<MessageEventContent>,
|
2020-06-20 21:18:20 +00:00
|
|
|
) {
|
2020-07-18 12:37:43 +00:00
|
|
|
let msg_body = if let SyncMessageEvent {
|
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") {
|
2020-08-26 14:19:39 +00:00
|
|
|
let content = AnyMessageEventContent::RoomMessage(MessageEventContent::Text(
|
|
|
|
TextMessageEventContent::plain("🎉🎊🥳 let's PARTY with wasm!! 🥳🎊🎉".to_string()),
|
2020-05-12 12:30:33 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
self.0.room_send(&room_id, content, None).await.unwrap();
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 13:04:43 +00:00
|
|
|
async fn on_sync_response(&self, response: SyncResponse) -> LoopCtrl {
|
2020-08-26 14:19:39 +00:00
|
|
|
console::log_1(&"Synced".to_string().into());
|
2020-05-12 12:30:33 +00:00
|
|
|
|
|
|
|
for (room_id, room) in response.rooms.join {
|
|
|
|
for event in room.timeline.events {
|
2021-01-22 17:14:08 +00:00
|
|
|
if let AnySyncRoomEvent::Message(AnySyncMessageEvent::RoomMessage(ev)) = event {
|
|
|
|
self.on_room_message(&room_id, ev).await
|
2020-05-12 12:30:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 13:04:43 +00:00
|
|
|
|
|
|
|
LoopCtrl::Continue
|
2020-05-12 12:30:33 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-08 14:12:21 +00:00
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub async fn run() -> Result<JsValue, JsValue> {
|
2021-01-22 17:14:08 +00:00
|
|
|
console_error_panic_hook::set_once();
|
|
|
|
|
2020-05-08 14:12:21 +00:00
|
|
|
let homeserver_url = "http://localhost:8008";
|
2021-01-22 17:14:08 +00:00
|
|
|
let username = "example";
|
|
|
|
let password = "wordpass";
|
2020-05-08 14:12:21 +00:00
|
|
|
|
|
|
|
let homeserver_url = Url::parse(&homeserver_url).unwrap();
|
2021-01-22 17:14:08 +00:00
|
|
|
let client = Client::new(homeserver_url).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-10-06 13:04:43 +00:00
|
|
|
client.sync_once(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
|
2020-10-06 13:04:43 +00:00
|
|
|
.sync_with_callback(settings, |response| bot.on_sync_response(response))
|
2020-05-12 12:30:33 +00:00
|
|
|
.await;
|
2020-05-08 14:12:21 +00:00
|
|
|
|
|
|
|
Ok(JsValue::NULL)
|
|
|
|
}
|