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

This commit is contained in:
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,54 +435,62 @@ 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 &mut e {
AnySyncRoomEvent::State(s) => match s {
AnySyncStateEvent::RoomMember(member) => {
if let Ok(member) = MemberEvent::try_from(member.clone()) {
match member.content.membership {
MembershipState::Join | MembershipState::Invite => {
user_ids.insert(member.state_key.clone());
}
_ => {
user_ids.remove(&member.state_key);
match hoist_room_event_prev_content(&event) {
Ok(mut e) => {
match &mut e {
AnySyncRoomEvent::State(s) => match s {
AnySyncStateEvent::RoomMember(member) => {
if let Ok(member) = MemberEvent::try_from(member.clone()) {
match member.content.membership {
MembershipState::Join | MembershipState::Invite => {
user_ids.insert(member.state_key.clone());
}
_ => {
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);
changes.add_state_event(room_id, s.clone());
}
},
_ => {
room_info.handle_state_event(&s);
changes.add_state_event(room_id, s.clone());
}
},
#[cfg(feature = "encryption")]
AnySyncRoomEvent::Message(message) => {
if let AnySyncMessageEvent::RoomEncrypted(encrypted) = message {
if let Some(olm) = self.olm_machine().await {
if let Ok(decrypted) =
olm.decrypt_room_event(encrypted, room_id).await
{
match decrypted.deserialize() {
Ok(decrypted) => e = decrypted,
Err(e) => {
warn!("Error deserializing a decrypted event {:?} ", e)
#[cfg(feature = "encryption")]
AnySyncRoomEvent::Message(message) => {
if let AnySyncMessageEvent::RoomEncrypted(encrypted) = message {
if let Some(olm) = self.olm_machine().await {
if let Ok(decrypted) =
olm.decrypt_room_event(encrypted, room_id).await
{
match decrypted.deserialize() {
Ok(decrypted) => e = decrypted,
Err(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,
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",