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()
/// .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)]
pub struct ClientConfig {
#[cfg(not(target_arch = "wasm32"))]
@ -1194,7 +1186,6 @@ impl Client {
///
/// # Example
/// ```no_run
/// # use matrix_sdk::Room;
/// # use std::sync::{Arc, RwLock};
/// # use matrix_sdk::{Client, SyncSettings};
/// # use url::Url;

View File

@ -417,7 +417,10 @@ impl BaseClient {
///
/// The methods of `EventEmitter` are called when the respective `RoomEvents` occur.
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);
}
@ -432,7 +435,8 @@ impl BaseClient {
let mut timeline = Timeline::new(ruma_timeline.limited, ruma_timeline.prev_batch.clone());
for event in ruma_timeline.events {
if let Ok(mut e) = hoist_room_event_prev_content(&event) {
match hoist_room_event_prev_content(&event) {
Ok(mut e) => {
match &mut e {
AnySyncRoomEvent::State(s) => match s {
AnySyncStateEvent::RoomMember(member) => {
@ -469,7 +473,10 @@ impl BaseClient {
match decrypted.deserialize() {
Ok(decrypted) => e = decrypted,
Err(e) => {
warn!("Error deserializing a decrypted event {:?} ", e)
warn!(
"Error deserializing a decrypted event {:?} ",
e
)
}
}
}
@ -481,6 +488,10 @@ impl BaseClient {
timeline.events.push(e);
}
Err(e) => {
warn!("Error deserializing event {:?}", e);
}
}
}
timeline

View File

@ -44,11 +44,13 @@ use crate::{
},
responses::SyncResponse,
rooms::RoomState,
Store,
};
use matrix_sdk_common_macros::async_trait;
pub(crate) struct Emitter {
pub(crate) inner: Box<dyn EventEmitter>,
pub(crate) store: Store,
}
impl Deref for Emitter {
@ -60,8 +62,8 @@ impl Deref for Emitter {
}
impl Emitter {
fn get_room(&self, _room_id: &RoomId) -> Option<RoomState> {
todo!()
fn get_room(&self, room_id: &RoomId) -> Option<RoomState> {
self.store.get_room(room_id)
}
pub(crate) async fn emit_sync(&self, response: &SyncResponse) {
@ -281,15 +283,10 @@ pub enum CustomEvent<'c> {
/// ..
/// } = event
/// {
/// let name = {
/// let room = room.read().await;
/// let member = room.joined_members.get(&sender).unwrap();
/// member
/// .display_name
/// .as_ref()
/// .map(ToString::to_string)
/// .unwrap_or(sender.to_string())
/// };
/// let member = room.get_member(&sender).await.unwrap();
/// let name = member
/// .display_name()
/// .unwrap_or_else(|| member.user_id().as_str());
/// println!("{}: {}", name, msg_body);
/// }
/// }
@ -707,6 +704,9 @@ mod test {
assert_eq!(
v.as_slice(),
[
"receipt event",
"account read",
"account ignore",
"state rules",
"state member",
"state aliases",
@ -715,10 +715,7 @@ mod test {
"state member",
"state member",
"message",
"account ignore",
"presence event",
"receipt event",
"account read",
],
)
}
@ -738,7 +735,11 @@ mod test {
let v = test_vec.lock().await;
assert_eq!(
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!(
v.as_slice(),
[
"account ignore",
"state rules",
"state member",
"state aliases",
@ -765,7 +767,8 @@ mod test {
"state canonical",
"state member",
"state member",
"message"
"message",
"presence event",
],
)
}
@ -786,15 +789,11 @@ mod test {
assert_eq!(
v.as_slice(),
[
"receipt event",
"typing event",
"message",
"message", // this is a message edit event
"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,
"sender": "@example:localhost",
"type": "m.room.redaction",
"redacts": "$151957878228ssqrj:localhost"
"redacts": "$151957878228ssqrj:localhost",
"unsigned": {
"age": 85
}
},
{
"content": {},
@ -661,12 +664,12 @@ lazy_static! {
{
"content": {
"m.relates_to": {
"event_id": "some event id",
"event_id": "$15275047031IXQRi:localhost",
"key": "👍",
"rel_type": "m.annotation"
}
},
"event_id": "event id",
"event_id": "$15275047031IXQRi:localhost",
"origin_server_ts": 159027581,
"sender": "@alice:matrix.org",
"type": "m.reaction",