Merge branch 'event-handler-encryption-info'
This commit is contained in:
commit
b96624890e
2 changed files with 50 additions and 34 deletions
|
@ -927,6 +927,7 @@ impl Client {
|
||||||
/// # let homeserver = Url::parse("http://localhost:8080").unwrap();
|
/// # let homeserver = Url::parse("http://localhost:8080").unwrap();
|
||||||
/// # let client = Client::new(homeserver).unwrap();
|
/// # let client = Client::new(homeserver).unwrap();
|
||||||
/// use matrix_sdk::{
|
/// use matrix_sdk::{
|
||||||
|
/// deserialized_responses::EncryptionInfo,
|
||||||
/// room::Room,
|
/// room::Room,
|
||||||
/// ruma::{
|
/// ruma::{
|
||||||
/// events::{
|
/// events::{
|
||||||
|
@ -952,10 +953,20 @@ impl Client {
|
||||||
/// )
|
/// )
|
||||||
/// .await
|
/// .await
|
||||||
/// .register_event_handler(
|
/// .register_event_handler(
|
||||||
|
/// |ev: SyncMessageEvent<MessageEventContent>,
|
||||||
|
/// room: Room,
|
||||||
|
/// encryption_info: Option<EncryptionInfo>| async move {
|
||||||
|
/// // An `Option<EncryptionInfo>` parameter lets you distinguish between
|
||||||
|
/// // unencrypted events and events that were decrypted by the SDK.
|
||||||
|
/// },
|
||||||
|
/// )
|
||||||
|
/// .await
|
||||||
|
/// .register_event_handler(
|
||||||
/// |ev: SyncStateEvent<TopicEventContent>| async move {
|
/// |ev: SyncStateEvent<TopicEventContent>| async move {
|
||||||
/// // Also possible: Omit any or all arguments after the first.
|
/// // You can omit any or all arguments after the first.
|
||||||
/// }
|
/// }
|
||||||
/// ).await;
|
/// )
|
||||||
|
/// .await;
|
||||||
///
|
///
|
||||||
/// // Custom events work exactly the same way, you just need to declare
|
/// // Custom events work exactly the same way, you just need to declare
|
||||||
/// // the content struct and use the EventContent derive macro on it.
|
/// // the content struct and use the EventContent derive macro on it.
|
||||||
|
|
|
@ -32,7 +32,7 @@
|
||||||
|
|
||||||
use std::{borrow::Cow, future::Future, ops::Deref};
|
use std::{borrow::Cow, future::Future, ops::Deref};
|
||||||
|
|
||||||
use matrix_sdk_base::deserialized_responses::SyncRoomEvent;
|
use matrix_sdk_base::deserialized_responses::{EncryptionInfo, SyncRoomEvent};
|
||||||
use ruma::{events::AnySyncStateEvent, serde::Raw};
|
use ruma::{events::AnySyncStateEvent, serde::Raw};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde_json::value::RawValue as RawJsonValue;
|
use serde_json::value::RawValue as RawJsonValue;
|
||||||
|
@ -117,6 +117,7 @@ pub struct EventHandlerData<'a> {
|
||||||
pub client: Client,
|
pub client: Client,
|
||||||
pub room: Option<room::Room>,
|
pub room: Option<room::Room>,
|
||||||
pub raw: &'a RawJsonValue,
|
pub raw: &'a RawJsonValue,
|
||||||
|
pub encryption_info: Option<&'a EncryptionInfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Context for an event handler.
|
/// Context for an event handler.
|
||||||
|
@ -169,6 +170,12 @@ impl EventHandlerContext for RawEvent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl EventHandlerContext for Option<EncryptionInfo> {
|
||||||
|
fn from_data(data: &EventHandlerData<'_>) -> Option<Self> {
|
||||||
|
Some(data.encryption_info.cloned())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Return types supported for event handlers implement this trait.
|
/// Return types supported for event handlers implement this trait.
|
||||||
///
|
///
|
||||||
/// It is not meant to be implemented outside of matrix-sdk.
|
/// It is not meant to be implemented outside of matrix-sdk.
|
||||||
|
@ -211,7 +218,19 @@ impl Client {
|
||||||
room: &Option<room::Room>,
|
room: &Option<room::Room>,
|
||||||
events: &[Raw<T>],
|
events: &[Raw<T>],
|
||||||
) -> serde_json::Result<()> {
|
) -> serde_json::Result<()> {
|
||||||
self.handle_sync_events_wrapped(kind, room, events, |x| x).await
|
#[derive(Deserialize)]
|
||||||
|
struct ExtractType<'a> {
|
||||||
|
#[serde(borrow, rename = "type")]
|
||||||
|
event_type: Cow<'a, str>,
|
||||||
|
}
|
||||||
|
|
||||||
|
self.handle_sync_events_wrapped_with(
|
||||||
|
room,
|
||||||
|
events,
|
||||||
|
|ev| (ev, None),
|
||||||
|
|raw| Ok((kind, raw.deserialize_as::<ExtractType>()?.event_type)),
|
||||||
|
)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn handle_sync_state_events(
|
pub(crate) async fn handle_sync_state_events(
|
||||||
|
@ -226,11 +245,16 @@ impl Client {
|
||||||
unsigned: Option<UnsignedDetails>,
|
unsigned: Option<UnsignedDetails>,
|
||||||
}
|
}
|
||||||
|
|
||||||
self.handle_sync_events_wrapped_with(room, state_events, std::convert::identity, |raw| {
|
self.handle_sync_events_wrapped_with(
|
||||||
|
room,
|
||||||
|
state_events,
|
||||||
|
|ev| (ev, None),
|
||||||
|
|raw| {
|
||||||
let StateEventDetails { event_type, unsigned } = raw.deserialize_as()?;
|
let StateEventDetails { event_type, unsigned } = raw.deserialize_as()?;
|
||||||
let redacted = unsigned.and_then(|u| u.redacted_because).is_some();
|
let redacted = unsigned.and_then(|u| u.redacted_because).is_some();
|
||||||
Ok((EventKind::State { redacted }, event_type))
|
Ok((EventKind::State { redacted }, event_type))
|
||||||
})
|
},
|
||||||
|
)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -239,7 +263,6 @@ impl Client {
|
||||||
room: &Option<room::Room>,
|
room: &Option<room::Room>,
|
||||||
timeline_events: &[SyncRoomEvent],
|
timeline_events: &[SyncRoomEvent],
|
||||||
) -> serde_json::Result<()> {
|
) -> serde_json::Result<()> {
|
||||||
// FIXME: add EncryptionInfo to context
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct TimelineEventDetails<'a> {
|
struct TimelineEventDetails<'a> {
|
||||||
#[serde(borrow, rename = "type")]
|
#[serde(borrow, rename = "type")]
|
||||||
|
@ -251,7 +274,7 @@ impl Client {
|
||||||
self.handle_sync_events_wrapped_with(
|
self.handle_sync_events_wrapped_with(
|
||||||
room,
|
room,
|
||||||
timeline_events,
|
timeline_events,
|
||||||
|e| &e.event,
|
|e| (&e.event, e.encryption_info.as_ref()),
|
||||||
|raw| {
|
|raw| {
|
||||||
let TimelineEventDetails { event_type, state_key, unsigned } =
|
let TimelineEventDetails { event_type, state_key, unsigned } =
|
||||||
raw.deserialize_as()?;
|
raw.deserialize_as()?;
|
||||||
|
@ -268,35 +291,16 @@ impl Client {
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_sync_events_wrapped<'a, T: 'a, U: 'a>(
|
|
||||||
&self,
|
|
||||||
kind: EventKind,
|
|
||||||
room: &Option<room::Room>,
|
|
||||||
events: &'a [U],
|
|
||||||
get_event: impl Fn(&'a U) -> &'a Raw<T>,
|
|
||||||
) -> Result<(), serde_json::Error> {
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
struct ExtractType<'a> {
|
|
||||||
#[serde(borrow, rename = "type")]
|
|
||||||
event_type: Cow<'a, str>,
|
|
||||||
}
|
|
||||||
|
|
||||||
self.handle_sync_events_wrapped_with(room, events, get_event, |raw| {
|
|
||||||
Ok((kind, raw.deserialize_as::<ExtractType>()?.event_type))
|
|
||||||
})
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn handle_sync_events_wrapped_with<'a, T: 'a, U: 'a>(
|
async fn handle_sync_events_wrapped_with<'a, T: 'a, U: 'a>(
|
||||||
&self,
|
&self,
|
||||||
room: &Option<room::Room>,
|
room: &Option<room::Room>,
|
||||||
list: &'a [U],
|
list: &'a [U],
|
||||||
get_event: impl Fn(&'a U) -> &'a Raw<T>,
|
get_event_details: impl Fn(&'a U) -> (&'a Raw<T>, Option<&'a EncryptionInfo>),
|
||||||
get_id: impl Fn(&Raw<T>) -> serde_json::Result<(EventKind, Cow<'_, str>)>,
|
get_id: impl Fn(&Raw<T>) -> serde_json::Result<(EventKind, Cow<'_, str>)>,
|
||||||
) -> serde_json::Result<()> {
|
) -> serde_json::Result<()> {
|
||||||
for x in list {
|
for x in list {
|
||||||
let event = get_event(x);
|
let (raw_event, encryption_info) = get_event_details(x);
|
||||||
let (ev_kind, ev_type) = get_id(event)?;
|
let (ev_kind, ev_type) = get_id(raw_event)?;
|
||||||
let event_handler_id = (ev_kind, &*ev_type);
|
let event_handler_id = (ev_kind, &*ev_type);
|
||||||
|
|
||||||
if let Some(handlers) = self.event_handlers.read().await.get(&event_handler_id) {
|
if let Some(handlers) = self.event_handlers.read().await.get(&event_handler_id) {
|
||||||
|
@ -304,7 +308,8 @@ impl Client {
|
||||||
let data = EventHandlerData {
|
let data = EventHandlerData {
|
||||||
client: self.clone(),
|
client: self.clone(),
|
||||||
room: room.clone(),
|
room: room.clone(),
|
||||||
raw: event.json(),
|
raw: raw_event.json(),
|
||||||
|
encryption_info,
|
||||||
};
|
};
|
||||||
matrix_sdk_common::executor::spawn((handler)(data));
|
matrix_sdk_common::executor::spawn((handler)(data));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue