base: Implement the last missing thing to get the emitter working again.

master
Damir Jelić 2020-12-22 10:09:59 +01:00
parent e25441babc
commit c5709d23a5
4 changed files with 76 additions and 72 deletions

View File

@ -176,14 +176,6 @@ impl Debug for Client {
/// .unwrap() /// .unwrap()
/// .disable_ssl_verification(); /// .disable_ssl_verification();
/// ``` /// ```
/// An example of adding a default `JsonStore` to the `Client`.
/// ```no_run
/// # use matrix_sdk::{ClientConfig, JsonStore};
///
/// let store = JsonStore::open("path/to/json").unwrap();
/// let client_config = ClientConfig::new()
/// .state_store(Box::new(store));
/// ```
#[derive(Default)] #[derive(Default)]
pub struct ClientConfig { pub struct ClientConfig {
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
@ -1194,7 +1186,6 @@ impl Client {
/// ///
/// # Example /// # Example
/// ```no_run /// ```no_run
/// # use matrix_sdk::Room;
/// # use std::sync::{Arc, RwLock}; /// # use std::sync::{Arc, RwLock};
/// # use matrix_sdk::{Client, SyncSettings}; /// # use matrix_sdk::{Client, SyncSettings};
/// # use url::Url; /// # use url::Url;

View File

@ -417,7 +417,10 @@ impl BaseClient {
/// ///
/// The methods of `EventEmitter` are called when the respective `RoomEvents` occur. /// The methods of `EventEmitter` are called when the respective `RoomEvents` occur.
pub async fn add_event_emitter(&self, emitter: Box<dyn EventEmitter>) { pub async fn add_event_emitter(&self, emitter: Box<dyn EventEmitter>) {
let emitter = Emitter { inner: emitter }; let emitter = Emitter {
inner: emitter,
store: self.store.clone(),
};
*self.event_emitter.write().await = Some(emitter); *self.event_emitter.write().await = Some(emitter);
} }
@ -432,54 +435,62 @@ impl BaseClient {
let mut timeline = Timeline::new(ruma_timeline.limited, ruma_timeline.prev_batch.clone()); let mut timeline = Timeline::new(ruma_timeline.limited, ruma_timeline.prev_batch.clone());
for event in ruma_timeline.events { for event in ruma_timeline.events {
if let Ok(mut e) = hoist_room_event_prev_content(&event) { match hoist_room_event_prev_content(&event) {
match &mut e { Ok(mut e) => {
AnySyncRoomEvent::State(s) => match s { match &mut e {
AnySyncStateEvent::RoomMember(member) => { AnySyncRoomEvent::State(s) => match s {
if let Ok(member) = MemberEvent::try_from(member.clone()) { AnySyncStateEvent::RoomMember(member) => {
match member.content.membership { if let Ok(member) = MemberEvent::try_from(member.clone()) {
MembershipState::Join | MembershipState::Invite => { match member.content.membership {
user_ids.insert(member.state_key.clone()); MembershipState::Join | MembershipState::Invite => {
} user_ids.insert(member.state_key.clone());
_ => { }
user_ids.remove(&member.state_key); _ => {
user_ids.remove(&member.state_key);
}
} }
changes
.members
.entry(room_id.clone())
.or_insert_with(BTreeMap::new)
.insert(member.state_key.clone(), member);
} }
changes
.members
.entry(room_id.clone())
.or_insert_with(BTreeMap::new)
.insert(member.state_key.clone(), member);
} }
} _ => {
_ => { room_info.handle_state_event(&s);
room_info.handle_state_event(&s); changes.add_state_event(room_id, s.clone());
changes.add_state_event(room_id, s.clone()); }
} },
},
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
AnySyncRoomEvent::Message(message) => { AnySyncRoomEvent::Message(message) => {
if let AnySyncMessageEvent::RoomEncrypted(encrypted) = message { if let AnySyncMessageEvent::RoomEncrypted(encrypted) = message {
if let Some(olm) = self.olm_machine().await { if let Some(olm) = self.olm_machine().await {
if let Ok(decrypted) = if let Ok(decrypted) =
olm.decrypt_room_event(encrypted, room_id).await olm.decrypt_room_event(encrypted, room_id).await
{ {
match decrypted.deserialize() { match decrypted.deserialize() {
Ok(decrypted) => e = decrypted, Ok(decrypted) => e = decrypted,
Err(e) => { Err(e) => {
warn!("Error deserializing a decrypted event {:?} ", e) warn!(
"Error deserializing a decrypted event {:?} ",
e
)
}
} }
} }
} }
} }
} }
_ => (),
} }
_ => (),
}
timeline.events.push(e); timeline.events.push(e);
}
Err(e) => {
warn!("Error deserializing event {:?}", e);
}
} }
} }

View File

@ -44,11 +44,13 @@ use crate::{
}, },
responses::SyncResponse, responses::SyncResponse,
rooms::RoomState, rooms::RoomState,
Store,
}; };
use matrix_sdk_common_macros::async_trait; use matrix_sdk_common_macros::async_trait;
pub(crate) struct Emitter { pub(crate) struct Emitter {
pub(crate) inner: Box<dyn EventEmitter>, pub(crate) inner: Box<dyn EventEmitter>,
pub(crate) store: Store,
} }
impl Deref for Emitter { impl Deref for Emitter {
@ -60,8 +62,8 @@ impl Deref for Emitter {
} }
impl Emitter { impl Emitter {
fn get_room(&self, _room_id: &RoomId) -> Option<RoomState> { fn get_room(&self, room_id: &RoomId) -> Option<RoomState> {
todo!() self.store.get_room(room_id)
} }
pub(crate) async fn emit_sync(&self, response: &SyncResponse) { pub(crate) async fn emit_sync(&self, response: &SyncResponse) {
@ -281,15 +283,10 @@ pub enum CustomEvent<'c> {
/// .. /// ..
/// } = event /// } = event
/// { /// {
/// let name = { /// let member = room.get_member(&sender).await.unwrap();
/// let room = room.read().await; /// let name = member
/// let member = room.joined_members.get(&sender).unwrap(); /// .display_name()
/// member /// .unwrap_or_else(|| member.user_id().as_str());
/// .display_name
/// .as_ref()
/// .map(ToString::to_string)
/// .unwrap_or(sender.to_string())
/// };
/// println!("{}: {}", name, msg_body); /// println!("{}: {}", name, msg_body);
/// } /// }
/// } /// }
@ -707,6 +704,9 @@ mod test {
assert_eq!( assert_eq!(
v.as_slice(), v.as_slice(),
[ [
"receipt event",
"account read",
"account ignore",
"state rules", "state rules",
"state member", "state member",
"state aliases", "state aliases",
@ -715,10 +715,7 @@ mod test {
"state member", "state member",
"state member", "state member",
"message", "message",
"account ignore",
"presence event", "presence event",
"receipt event",
"account read",
], ],
) )
} }
@ -738,7 +735,11 @@ mod test {
let v = test_vec.lock().await; let v = test_vec.lock().await;
assert_eq!( assert_eq!(
v.as_slice(), v.as_slice(),
["stripped state name", "stripped state member"], [
"stripped state name",
"stripped state member",
"presence event"
],
) )
} }
@ -758,6 +759,7 @@ mod test {
assert_eq!( assert_eq!(
v.as_slice(), v.as_slice(),
[ [
"account ignore",
"state rules", "state rules",
"state member", "state member",
"state aliases", "state aliases",
@ -765,7 +767,8 @@ mod test {
"state canonical", "state canonical",
"state member", "state member",
"state member", "state member",
"message" "message",
"presence event",
], ],
) )
} }
@ -786,15 +789,11 @@ mod test {
assert_eq!( assert_eq!(
v.as_slice(), v.as_slice(),
[ [
"receipt event",
"typing event",
"message", "message",
"message", // this is a message edit event "message", // this is a message edit event
"redaction", "redaction",
"unrecognized event",
// "unrecognized event", this is actually a redacted "m.room.messages" event
// the ephemeral room events are looped over after the room events
"receipt event",
"typing event"
], ],
) )
} }

View File

@ -634,7 +634,10 @@ lazy_static! {
"origin_server_ts": 151957878, "origin_server_ts": 151957878,
"sender": "@example:localhost", "sender": "@example:localhost",
"type": "m.room.redaction", "type": "m.room.redaction",
"redacts": "$151957878228ssqrj:localhost" "redacts": "$151957878228ssqrj:localhost",
"unsigned": {
"age": 85
}
}, },
{ {
"content": {}, "content": {},
@ -661,12 +664,12 @@ lazy_static! {
{ {
"content": { "content": {
"m.relates_to": { "m.relates_to": {
"event_id": "some event id", "event_id": "$15275047031IXQRi:localhost",
"key": "👍", "key": "👍",
"rel_type": "m.annotation" "rel_type": "m.annotation"
} }
}, },
"event_id": "event id", "event_id": "$15275047031IXQRi:localhost",
"origin_server_ts": 159027581, "origin_server_ts": 159027581,
"sender": "@alice:matrix.org", "sender": "@alice:matrix.org",
"type": "m.reaction", "type": "m.reaction",