event_emitter: use enum to represent custom events and raw json
parent
9cb86596d8
commit
db38bf1276
|
@ -38,7 +38,7 @@
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
pub use matrix_sdk_base::JsonStore;
|
pub use matrix_sdk_base::JsonStore;
|
||||||
pub use matrix_sdk_base::{EventEmitter, Room, Session, SyncRoom};
|
pub use matrix_sdk_base::{CustomOrRawEvent, EventEmitter, Room, Session, SyncRoom};
|
||||||
pub use matrix_sdk_base::{RoomState, StateStore};
|
pub use matrix_sdk_base::{RoomState, StateStore};
|
||||||
pub use matrix_sdk_common::*;
|
pub use matrix_sdk_common::*;
|
||||||
pub use reqwest::header::InvalidHeaderValue;
|
pub use reqwest::header::InvalidHeaderValue;
|
||||||
|
|
|
@ -28,6 +28,7 @@ use crate::error::Result;
|
||||||
use crate::events::collections::all::{RoomEvent, StateEvent};
|
use crate::events::collections::all::{RoomEvent, StateEvent};
|
||||||
use crate::events::presence::PresenceEvent;
|
use crate::events::presence::PresenceEvent;
|
||||||
// `NonRoomEvent` is what it is aliased as
|
// `NonRoomEvent` is what it is aliased as
|
||||||
|
use crate::event_emitter::CustomOrRawEvent;
|
||||||
use crate::events::collections::only::Event as NonRoomEvent;
|
use crate::events::collections::only::Event as NonRoomEvent;
|
||||||
use crate::events::ignored_user_list::IgnoredUserListEvent;
|
use crate::events::ignored_user_list::IgnoredUserListEvent;
|
||||||
use crate::events::push_rules::{PushRulesEvent, Ruleset};
|
use crate::events::push_rules::{PushRulesEvent, Ruleset};
|
||||||
|
@ -1428,9 +1429,9 @@ impl BaseClient {
|
||||||
}
|
}
|
||||||
RoomEvent::RoomTombstone(tomb) => event_emitter.on_room_tombstone(room, &tomb).await,
|
RoomEvent::RoomTombstone(tomb) => event_emitter.on_room_tombstone(room, &tomb).await,
|
||||||
RoomEvent::CustomRoom(custom) => {
|
RoomEvent::CustomRoom(custom) => {
|
||||||
if let Ok(raw) = serde_json::value::to_raw_value(custom) {
|
event_emitter
|
||||||
event_emitter.on_unrecognized_event(room, &raw).await
|
.on_unrecognized_event(room, &CustomOrRawEvent::CustomRoom(custom))
|
||||||
}
|
.await
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
@ -1493,9 +1494,9 @@ impl BaseClient {
|
||||||
}
|
}
|
||||||
StateEvent::RoomTombstone(tomb) => event_emitter.on_room_tombstone(room, &tomb).await,
|
StateEvent::RoomTombstone(tomb) => event_emitter.on_room_tombstone(room, &tomb).await,
|
||||||
StateEvent::CustomState(custom) => {
|
StateEvent::CustomState(custom) => {
|
||||||
if let Ok(raw) = serde_json::value::to_raw_value(custom) {
|
event_emitter
|
||||||
event_emitter.on_unrecognized_event(room, &raw).await
|
.on_unrecognized_event(room, &CustomOrRawEvent::CustomState(custom))
|
||||||
}
|
.await
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
@ -1751,7 +1752,8 @@ impl BaseClient {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if let Some(ee) = &self.event_emitter.read().await.as_ref() {
|
if let Some(ee) = &self.event_emitter.read().await.as_ref() {
|
||||||
ee.on_unrecognized_event(room, event.json()).await;
|
ee.on_unrecognized_event(room, &CustomOrRawEvent::RawJson(event.json()))
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2019,7 +2021,6 @@ mod test {
|
||||||
|
|
||||||
use crate::{EventEmitter, SyncRoom};
|
use crate::{EventEmitter, SyncRoom};
|
||||||
use matrix_sdk_common::locks::RwLock;
|
use matrix_sdk_common::locks::RwLock;
|
||||||
use serde_json::value::RawValue;
|
|
||||||
use std::sync::{
|
use std::sync::{
|
||||||
atomic::{AtomicBool, Ordering},
|
atomic::{AtomicBool, Ordering},
|
||||||
Arc,
|
Arc,
|
||||||
|
@ -2028,13 +2029,15 @@ mod test {
|
||||||
struct EE(Arc<AtomicBool>);
|
struct EE(Arc<AtomicBool>);
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
impl EventEmitter for EE {
|
impl EventEmitter for EE {
|
||||||
async fn on_unrecognized_event(&self, room: SyncRoom, event: &RawValue) {
|
async fn on_unrecognized_event(&self, room: SyncRoom, event: &CustomOrRawEvent<'_>) {
|
||||||
if let SyncRoom::Joined(_) = room {
|
if let SyncRoom::Joined(_) = room {
|
||||||
let val = serde_json::to_value(event).unwrap();
|
if let CustomOrRawEvent::RawJson(raw) = event {
|
||||||
if val.get("type").unwrap() == &json! { "m.room.message" }
|
let val = serde_json::to_value(raw).unwrap();
|
||||||
&& val.get("content").unwrap().get("m.relates_to").is_some()
|
if val.get("type").unwrap() == &json! { "m.room.message" }
|
||||||
{
|
&& val.get("content").unwrap().get("m.relates_to").is_some()
|
||||||
self.0.swap(true, Ordering::SeqCst);
|
{
|
||||||
|
self.0.swap(true, Ordering::SeqCst);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2114,7 +2117,6 @@ mod test {
|
||||||
|
|
||||||
use crate::{EventEmitter, SyncRoom};
|
use crate::{EventEmitter, SyncRoom};
|
||||||
use matrix_sdk_common::locks::RwLock;
|
use matrix_sdk_common::locks::RwLock;
|
||||||
use serde_json::value::RawValue;
|
|
||||||
use std::sync::{
|
use std::sync::{
|
||||||
atomic::{AtomicBool, Ordering},
|
atomic::{AtomicBool, Ordering},
|
||||||
Arc,
|
Arc,
|
||||||
|
@ -2123,13 +2125,14 @@ mod test {
|
||||||
struct EE(Arc<AtomicBool>);
|
struct EE(Arc<AtomicBool>);
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
impl EventEmitter for EE {
|
impl EventEmitter for EE {
|
||||||
async fn on_unrecognized_event(&self, room: SyncRoom, event: &RawValue) {
|
async fn on_unrecognized_event(&self, room: SyncRoom, event: &CustomOrRawEvent<'_>) {
|
||||||
if let SyncRoom::Joined(_) = room {
|
if let SyncRoom::Joined(_) = room {
|
||||||
let val = serde_json::to_value(event).unwrap();
|
if let CustomOrRawEvent::CustomRoom(custom) = event {
|
||||||
if val.get("type").unwrap() == &json! { "m.reaction" }
|
if custom.event_type == "m.reaction"
|
||||||
&& val.get("content").unwrap().get("m.relates_to").is_some()
|
&& custom.content.get("m.relates_to").is_some()
|
||||||
{
|
{
|
||||||
self.0.swap(true, Ordering::SeqCst);
|
self.0.swap(true, Ordering::SeqCst);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,12 +40,28 @@ use crate::events::{
|
||||||
StrippedRoomMember, StrippedRoomName, StrippedRoomPowerLevels,
|
StrippedRoomMember, StrippedRoomName, StrippedRoomPowerLevels,
|
||||||
},
|
},
|
||||||
typing::TypingEvent,
|
typing::TypingEvent,
|
||||||
|
CustomEvent, CustomRoomEvent, CustomStateEvent,
|
||||||
};
|
};
|
||||||
use crate::{Room, RoomState};
|
use crate::{Room, RoomState};
|
||||||
|
|
||||||
/// Type alias for `RoomState` enum when passed to `EventEmitter` methods.
|
/// Type alias for `RoomState` enum when passed to `EventEmitter` methods.
|
||||||
pub type SyncRoom = RoomState<Arc<RwLock<Room>>>;
|
pub type SyncRoom = RoomState<Arc<RwLock<Room>>>;
|
||||||
|
|
||||||
|
/// This represents the various "unrecognized" events.
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub enum CustomOrRawEvent<'c> {
|
||||||
|
/// When an event can not be deserialized by ruma.
|
||||||
|
///
|
||||||
|
/// This will be mostly obsolete when ruma-events is updated.
|
||||||
|
RawJson(&'c RawJsonValue),
|
||||||
|
/// A custom event.
|
||||||
|
Custom(&'c CustomEvent),
|
||||||
|
/// A custom room event.
|
||||||
|
CustomRoom(&'c CustomRoomEvent),
|
||||||
|
/// A custom state event.
|
||||||
|
CustomState(&'c CustomStateEvent),
|
||||||
|
}
|
||||||
|
|
||||||
/// This trait allows any type implementing `EventEmitter` to specify event callbacks for each event.
|
/// This trait allows any type implementing `EventEmitter` to specify event callbacks for each event.
|
||||||
/// The `Client` calls each method when the corresponding event is received.
|
/// The `Client` calls each method when the corresponding event is received.
|
||||||
///
|
///
|
||||||
|
@ -177,7 +193,7 @@ pub trait EventEmitter: Send + Sync {
|
||||||
/// because the event was unknown to ruma.
|
/// because the event was unknown to ruma.
|
||||||
///
|
///
|
||||||
/// The only guarantee this method can give about the event is that it is valid JSON.
|
/// The only guarantee this method can give about the event is that it is valid JSON.
|
||||||
async fn on_unrecognized_event(&self, _: SyncRoom, _: &RawJsonValue) {}
|
async fn on_unrecognized_event(&self, _: SyncRoom, _: &CustomOrRawEvent<'_>) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -46,7 +46,7 @@ mod session;
|
||||||
mod state;
|
mod state;
|
||||||
|
|
||||||
pub use client::{BaseClient, BaseClientConfig, RoomState, RoomStateType};
|
pub use client::{BaseClient, BaseClientConfig, RoomState, RoomStateType};
|
||||||
pub use event_emitter::{EventEmitter, SyncRoom};
|
pub use event_emitter::{CustomOrRawEvent, EventEmitter, SyncRoom};
|
||||||
#[cfg(feature = "encryption")]
|
#[cfg(feature = "encryption")]
|
||||||
pub use matrix_sdk_crypto::{Device, TrustState};
|
pub use matrix_sdk_crypto::{Device, TrustState};
|
||||||
pub use models::Room;
|
pub use models::Room;
|
||||||
|
|
Loading…
Reference in New Issue