async_client: Properly handle the EventResult for our timeline.

master
Damir Jelić 2019-11-26 20:47:02 +01:00
parent afcb68cc0e
commit 38ddad1874
3 changed files with 23 additions and 12 deletions

View File

@ -7,17 +7,23 @@ use matrix_nio::{
events::{ events::{
collections::all::RoomEvent, collections::all::RoomEvent,
room::message::{MessageEvent, MessageEventContent, TextMessageEventContent}, room::message::{MessageEvent, MessageEventContent, TextMessageEventContent},
EventResult,
}, },
AsyncClient, AsyncClientConfig, Room, SyncSettings, AsyncClient, AsyncClientConfig, Room, SyncSettings,
}; };
async fn async_cb(room: Arc<RwLock<Room>>, event: Arc<RoomEvent>) { async fn async_cb(room: Arc<RwLock<Room>>, event: Arc<EventResult<RoomEvent>>) {
let room = room.read().unwrap(); let room = room.read().unwrap();
let event = if let EventResult::Ok(event) = &*event {
event
} else {
return;
};
if let RoomEvent::RoomMessage(MessageEvent { if let RoomEvent::RoomMessage(MessageEvent {
content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }), content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }),
sender, sender,
.. ..
}) = &*event }) = event
{ {
let user = room.members.get(&sender.to_string()).unwrap(); let user = room.members.get(&sender.to_string()).unwrap();
println!( println!(

View File

@ -24,7 +24,7 @@ use crate::session::Session;
use crate::VERSION; use crate::VERSION;
type RoomEventCallbackF = type RoomEventCallbackF =
Box<dyn FnMut(Arc<RwLock<Room>>, Arc<RoomEvent>) -> BoxFuture<'static, ()> + Send>; Box<dyn FnMut(Arc<RwLock<Room>>, Arc<EventResult<RoomEvent>>) -> BoxFuture<'static, ()> + Send>;
#[derive(Clone)] #[derive(Clone)]
pub struct AsyncClient { pub struct AsyncClient {
@ -163,7 +163,7 @@ impl AsyncClient {
pub fn add_event_future<C: 'static>( pub fn add_event_future<C: 'static>(
&mut self, &mut self,
mut callback: impl FnMut(Arc<RwLock<Room>>, Arc<RoomEvent>) -> C + 'static + Send, mut callback: impl FnMut(Arc<RwLock<Room>>, Arc<EventResult<RoomEvent>>) -> C + 'static + Send,
) where ) where
C: Future<Output = ()> + Send, C: Future<Output = ()> + Send,
{ {
@ -226,11 +226,6 @@ impl AsyncClient {
}; };
for event in &room.timeline.events { 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(); let mut client = self.base_client.write().unwrap();
client.receive_joined_timeline_event(&room_id, &event); client.receive_joined_timeline_event(&room_id, &event);

View File

@ -3,6 +3,7 @@ use std::collections::HashMap;
use crate::api::r0 as api; use crate::api::r0 as api;
use crate::events::collections::all::{RoomEvent, StateEvent}; use crate::events::collections::all::{RoomEvent, StateEvent};
use crate::events::room::member::{MemberEvent, MembershipState}; use crate::events::room::member::{MemberEvent, MembershipState};
use crate::events::EventResult;
use crate::session::Session; use crate::session::Session;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
@ -215,9 +216,18 @@ impl Client {
/// ///
/// Returns true if the membership list of the room changed, false /// Returns true if the membership list of the room changed, false
/// otherwise. /// otherwise.
pub fn receive_joined_timeline_event(&mut self, room_id: &RoomId, event: &RoomEvent) -> bool { pub fn receive_joined_timeline_event(
&mut self,
room_id: &RoomId,
event: &EventResult<RoomEvent>,
) -> bool {
match event {
EventResult::Ok(e) => {
let mut room = self.get_or_create_room(room_id).write().unwrap(); let mut room = self.get_or_create_room(room_id).write().unwrap();
room.receive_timeline_event(event) room.receive_timeline_event(e)
}
_ => false,
}
} }
/// Receive a state event for a joined room and update the client state. /// Receive a state event for a joined room and update the client state.