diff --git a/examples/login.rs b/examples/login.rs index 4948b3ef..3c9b0012 100644 --- a/examples/login.rs +++ b/examples/login.rs @@ -7,17 +7,23 @@ use matrix_nio::{ events::{ collections::all::RoomEvent, room::message::{MessageEvent, MessageEventContent, TextMessageEventContent}, + EventResult, }, AsyncClient, AsyncClientConfig, Room, SyncSettings, }; -async fn async_cb(room: Arc>, event: Arc) { +async fn async_cb(room: Arc>, event: Arc>) { let room = room.read().unwrap(); + let event = if let EventResult::Ok(event) = &*event { + event + } else { + return; + }; if let RoomEvent::RoomMessage(MessageEvent { content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }), sender, .. - }) = &*event + }) = event { let user = room.members.get(&sender.to_string()).unwrap(); println!( diff --git a/src/async_client.rs b/src/async_client.rs index ff1808b6..3f61b833 100644 --- a/src/async_client.rs +++ b/src/async_client.rs @@ -24,7 +24,7 @@ use crate::session::Session; use crate::VERSION; type RoomEventCallbackF = - Box>, Arc) -> BoxFuture<'static, ()> + Send>; + Box>, Arc>) -> BoxFuture<'static, ()> + Send>; #[derive(Clone)] pub struct AsyncClient { @@ -163,7 +163,7 @@ impl AsyncClient { pub fn add_event_future( &mut self, - mut callback: impl FnMut(Arc>, Arc) -> C + 'static + Send, + mut callback: impl FnMut(Arc>, Arc>) -> C + 'static + Send, ) where C: Future + Send, { @@ -226,11 +226,6 @@ impl AsyncClient { }; for event in &room.timeline.events { - let event = match event.clone().into_result() { - Ok(e) => e, - Err(e) => continue, - }; - { let mut client = self.base_client.write().unwrap(); client.receive_joined_timeline_event(&room_id, &event); diff --git a/src/base_client.rs b/src/base_client.rs index f875eb70..af461377 100644 --- a/src/base_client.rs +++ b/src/base_client.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use crate::api::r0 as api; use crate::events::collections::all::{RoomEvent, StateEvent}; use crate::events::room::member::{MemberEvent, MembershipState}; +use crate::events::EventResult; use crate::session::Session; use std::sync::{Arc, RwLock}; @@ -215,9 +216,18 @@ impl Client { /// /// Returns true if the membership list of the room changed, false /// otherwise. - pub fn receive_joined_timeline_event(&mut self, room_id: &RoomId, event: &RoomEvent) -> bool { - let mut room = self.get_or_create_room(room_id).write().unwrap(); - room.receive_timeline_event(event) + pub fn receive_joined_timeline_event( + &mut self, + room_id: &RoomId, + event: &EventResult, + ) -> bool { + match event { + EventResult::Ok(e) => { + let mut room = self.get_or_create_room(room_id).write().unwrap(); + room.receive_timeline_event(e) + } + _ => false, + } } /// Receive a state event for a joined room and update the client state.