nio: Allow callbacks to borrow the room, sadly back to non async callbacks.
parent
bec253efd8
commit
b61dc18060
|
@ -9,9 +9,13 @@ use matrix_nio::{
|
||||||
room::message::{MessageEvent, MessageEventContent, TextMessageEventContent},
|
room::message::{MessageEvent, MessageEventContent, TextMessageEventContent},
|
||||||
EventType,
|
EventType,
|
||||||
},
|
},
|
||||||
AsyncClient, AsyncClientConfig, SyncSettings,
|
AsyncClient, AsyncClientConfig, SyncSettings, Room
|
||||||
};
|
};
|
||||||
|
|
||||||
|
fn callback(room: &Room, event: &RoomEvent) {
|
||||||
|
println!("{:?}", event);
|
||||||
|
}
|
||||||
|
|
||||||
async fn login(
|
async fn login(
|
||||||
homeserver_url: String,
|
homeserver_url: String,
|
||||||
username: String,
|
username: String,
|
||||||
|
@ -22,18 +26,20 @@ async fn login(
|
||||||
.disable_ssl_verification();
|
.disable_ssl_verification();
|
||||||
let mut client = AsyncClient::new_with_config(&homeserver_url, None, client_config).unwrap();
|
let mut client = AsyncClient::new_with_config(&homeserver_url, None, client_config).unwrap();
|
||||||
|
|
||||||
client.add_event_callback(EventType::RoomMessage, |event| {
|
client.add_event_callback(EventType::RoomMessage, Box::new(callback));
|
||||||
Box::pin(async {
|
|
||||||
if let RoomEvent::RoomMessage(MessageEvent {
|
// client.add_event_callback(EventType::RoomMessage, Box::new(|event| {
|
||||||
content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }),
|
// Box::pin(async {
|
||||||
sender,
|
// if let RoomEvent::RoomMessage(MessageEvent {
|
||||||
..
|
// content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }),
|
||||||
}) = event
|
// sender,
|
||||||
{
|
// ..
|
||||||
println!("{}: {}", sender, msg_body);
|
// }) = event
|
||||||
}
|
// {
|
||||||
})
|
// println!("{}: {}", sender, msg_body);
|
||||||
});
|
// }
|
||||||
|
// })
|
||||||
|
// }));
|
||||||
|
|
||||||
client.login(username, password, None).await?;
|
client.login(username, password, None).await?;
|
||||||
let response = client.sync(SyncSettings::new()).await?;
|
let response = client.sync(SyncSettings::new()).await?;
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::convert::{TryFrom, TryInto};
|
use std::convert::{TryFrom, TryInto};
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
@ -16,9 +15,12 @@ pub use ruma_events::EventType;
|
||||||
|
|
||||||
use crate::api;
|
use crate::api;
|
||||||
use crate::base_client::Client as BaseClient;
|
use crate::base_client::Client as BaseClient;
|
||||||
|
use crate::base_client::Room;
|
||||||
use crate::error::{Error, InnerError};
|
use crate::error::{Error, InnerError};
|
||||||
use crate::session::Session;
|
use crate::session::Session;
|
||||||
|
|
||||||
|
type RoomEventCallback = Box::<dyn FnMut(&Room, &RoomEvent)>;
|
||||||
|
|
||||||
pub struct AsyncClient {
|
pub struct AsyncClient {
|
||||||
/// The URL of the homeserver to connect to.
|
/// The URL of the homeserver to connect to.
|
||||||
homeserver: Url,
|
homeserver: Url,
|
||||||
|
@ -27,8 +29,7 @@ pub struct AsyncClient {
|
||||||
/// User session data.
|
/// User session data.
|
||||||
base_client: BaseClient,
|
base_client: BaseClient,
|
||||||
/// Event callbacks
|
/// Event callbacks
|
||||||
event_callbacks:
|
event_callbacks: Vec<RoomEventCallback>,
|
||||||
HashMap<EventType, Box<dyn FnMut(RoomEvent) -> Pin<Box<dyn Future<Output = ()>>>>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Debug)]
|
||||||
|
@ -160,16 +161,16 @@ impl AsyncClient {
|
||||||
homeserver,
|
homeserver,
|
||||||
http_client,
|
http_client,
|
||||||
base_client: BaseClient::new(session),
|
base_client: BaseClient::new(session),
|
||||||
event_callbacks: HashMap::new(),
|
event_callbacks: Vec::new(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_event_callback(
|
pub fn add_event_callback(
|
||||||
&mut self,
|
&mut self,
|
||||||
event_type: EventType,
|
event_type: EventType,
|
||||||
callback: impl FnMut(RoomEvent) -> Pin<Box<dyn Future<Output = ()>>> + 'static,
|
callback: RoomEventCallback,
|
||||||
) {
|
) {
|
||||||
self.event_callbacks.insert(event_type, Box::new(callback));
|
self.event_callbacks.push(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn login<S: Into<String>>(
|
pub async fn login<S: Into<String>>(
|
||||||
|
@ -187,7 +188,7 @@ impl AsyncClient {
|
||||||
user: user.into(),
|
user: user.into(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let response = self.send(request).await.unwrap();
|
let response = self.send(request).await?;
|
||||||
self.base_client.receive_login_response(&response);
|
self.base_client.receive_login_response(&response);
|
||||||
|
|
||||||
Ok(response)
|
Ok(response)
|
||||||
|
@ -205,47 +206,39 @@ impl AsyncClient {
|
||||||
timeout: sync_settings.timeout,
|
timeout: sync_settings.timeout,
|
||||||
};
|
};
|
||||||
|
|
||||||
let response = self.send(request).await.unwrap();
|
let response = self.send(request).await?;
|
||||||
|
|
||||||
for (room_id, room) in &response.rooms.join {
|
for (room_id, room) in &response.rooms.join {
|
||||||
let room_id = room_id.to_string();
|
let room_id = room_id.to_string();
|
||||||
|
|
||||||
for event in &room.state.events {
|
for event in &room.state.events {
|
||||||
self.base_client.receive_joined_state_event(&room_id, event);
|
let event = match event.clone().into_result() {
|
||||||
|
Ok(e) => e,
|
||||||
|
Err(e) => continue
|
||||||
|
};
|
||||||
|
|
||||||
|
self.base_client.receive_joined_state_event(&room_id, &event);
|
||||||
}
|
}
|
||||||
|
|
||||||
for event in &room.timeline.events {
|
for event in &room.timeline.events {
|
||||||
self.base_client
|
let event = match event.clone().into_result() {
|
||||||
.receive_joined_timeline_event(&room_id, event);
|
Ok(e) => e,
|
||||||
|
Err(e) => continue
|
||||||
let event_type = match &event {
|
|
||||||
RoomEvent::CallAnswer(e) => e.event_type(),
|
|
||||||
RoomEvent::CallCandidates(e) => e.event_type(),
|
|
||||||
RoomEvent::CallHangup(e) => e.event_type(),
|
|
||||||
RoomEvent::CallInvite(e) => e.event_type(),
|
|
||||||
RoomEvent::RoomAliases(e) => e.event_type(),
|
|
||||||
RoomEvent::RoomAvatar(e) => e.event_type(),
|
|
||||||
RoomEvent::RoomCanonicalAlias(e) => e.event_type(),
|
|
||||||
RoomEvent::RoomCreate(e) => e.event_type(),
|
|
||||||
RoomEvent::RoomGuestAccess(e) => e.event_type(),
|
|
||||||
RoomEvent::RoomHistoryVisibility(e) => e.event_type(),
|
|
||||||
RoomEvent::RoomJoinRules(e) => e.event_type(),
|
|
||||||
RoomEvent::RoomMember(e) => e.event_type(),
|
|
||||||
RoomEvent::RoomMessage(e) => e.event_type(),
|
|
||||||
RoomEvent::RoomName(e) => e.event_type(),
|
|
||||||
RoomEvent::RoomPinnedEvents(e) => e.event_type(),
|
|
||||||
RoomEvent::RoomPowerLevels(e) => e.event_type(),
|
|
||||||
RoomEvent::RoomRedaction(e) => e.event_type(),
|
|
||||||
RoomEvent::RoomThirdPartyInvite(e) => e.event_type(),
|
|
||||||
RoomEvent::RoomTopic(e) => e.event_type(),
|
|
||||||
RoomEvent::CustomRoom(e) => e.event_type(),
|
|
||||||
RoomEvent::CustomState(e) => e.event_type(),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if self.event_callbacks.contains_key(&event_type) {
|
self.base_client
|
||||||
let cb = self.event_callbacks.get_mut(&event_type).unwrap();
|
.receive_joined_timeline_event(&room_id, &event);
|
||||||
cb(event.clone()).await;
|
|
||||||
|
let room = self.base_client.joined_rooms.get(&room_id).unwrap();
|
||||||
|
|
||||||
|
for mut cb in &mut self.event_callbacks {
|
||||||
|
cb(&room, &event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if self.event_callbacks.contains_key(&event_type) {
|
||||||
|
// let cb = self.event_callbacks.get_mut(&event_type).unwrap();
|
||||||
|
// cb(&event).await;
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -105,6 +105,7 @@ impl Room {
|
||||||
MembershipState::Ban => self.handle_leave(event),
|
MembershipState::Ban => self.handle_leave(event),
|
||||||
MembershipState::Invite => false,
|
MembershipState::Invite => false,
|
||||||
MembershipState::Knock => false,
|
MembershipState::Knock => false,
|
||||||
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,4 +13,6 @@ mod error;
|
||||||
mod session;
|
mod session;
|
||||||
|
|
||||||
pub use async_client::{AsyncClient, AsyncClientConfig, SyncSettings};
|
pub use async_client::{AsyncClient, AsyncClientConfig, SyncSettings};
|
||||||
pub use base_client::Client;
|
pub use base_client::{Client, Room};
|
||||||
|
|
||||||
|
pub const VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
Loading…
Reference in New Issue