matrix_sdk/widget/machine/
driver_req.rsuse std::marker::PhantomData;
use ruma::{
api::client::{account::request_openid_token, delayed_events::update_delayed_event},
events::{AnyTimelineEvent, MessageLikeEventType, StateEventType, TimelineEventType},
serde::Raw,
};
use serde::Deserialize;
use serde_json::value::RawValue as RawJsonValue;
use tracing::error;
use super::{
from_widget::SendEventResponse, incoming::MatrixDriverResponse, Action,
MatrixDriverRequestMeta, WidgetMachine,
};
use crate::widget::{Capabilities, StateKeySelector};
#[derive(Clone, Debug)]
pub(crate) enum MatrixDriverRequestData {
AcquireCapabilities(AcquireCapabilities),
GetOpenId,
ReadMessageLikeEvent(ReadMessageLikeEventRequest),
ReadStateEvent(ReadStateEventRequest),
SendMatrixEvent(SendEventRequest),
UpdateDelayedEvent(UpdateDelayedEventRequest),
}
pub(crate) struct MatrixDriverRequestHandle<'m, T> {
request_meta: Option<&'m mut MatrixDriverRequestMeta>,
_phantom: PhantomData<fn() -> T>,
}
impl<'m, T> MatrixDriverRequestHandle<'m, T>
where
T: FromMatrixDriverResponse,
{
pub(crate) fn new(request_meta: &'m mut MatrixDriverRequestMeta) -> Self {
Self { request_meta: Some(request_meta), _phantom: PhantomData }
}
pub(crate) fn null() -> Self {
Self { request_meta: None, _phantom: PhantomData }
}
pub(crate) fn then(
self,
response_handler: impl FnOnce(Result<T, crate::Error>, &mut WidgetMachine) -> Vec<Action>
+ Send
+ 'static,
) {
if let Some(request_meta) = self.request_meta {
request_meta.response_fn = Some(Box::new(move |response, machine| {
if let Some(response_data) = response.map(T::from_response).transpose() {
response_handler(response_data, machine)
} else {
Vec::new()
}
}));
}
}
}
pub(crate) trait MatrixDriverRequest: Into<MatrixDriverRequestData> {
type Response: FromMatrixDriverResponse;
}
pub(crate) trait FromMatrixDriverResponse: Sized {
fn from_response(_: MatrixDriverResponse) -> Option<Self>;
}
#[derive(Clone, Debug)]
pub(crate) struct AcquireCapabilities {
pub(crate) desired_capabilities: Capabilities,
}
impl From<AcquireCapabilities> for MatrixDriverRequestData {
fn from(value: AcquireCapabilities) -> Self {
MatrixDriverRequestData::AcquireCapabilities(value)
}
}
impl MatrixDriverRequest for AcquireCapabilities {
type Response = Capabilities;
}
impl FromMatrixDriverResponse for Capabilities {
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
match ev {
MatrixDriverResponse::CapabilitiesAcquired(response) => Some(response),
_ => {
error!("bug in MatrixDriver, received wrong event response");
None
}
}
}
}
#[derive(Debug)]
pub(crate) struct RequestOpenId;
impl From<RequestOpenId> for MatrixDriverRequestData {
fn from(_: RequestOpenId) -> Self {
MatrixDriverRequestData::GetOpenId
}
}
impl MatrixDriverRequest for RequestOpenId {
type Response = request_openid_token::v3::Response;
}
impl FromMatrixDriverResponse for request_openid_token::v3::Response {
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
match ev {
MatrixDriverResponse::OpenIdReceived(response) => Some(response),
_ => {
error!("bug in MatrixDriver, received wrong event response");
None
}
}
}
}
#[derive(Clone, Debug)]
pub(crate) struct ReadMessageLikeEventRequest {
pub(crate) event_type: MessageLikeEventType,
pub(crate) limit: u32,
}
impl From<ReadMessageLikeEventRequest> for MatrixDriverRequestData {
fn from(value: ReadMessageLikeEventRequest) -> Self {
MatrixDriverRequestData::ReadMessageLikeEvent(value)
}
}
impl MatrixDriverRequest for ReadMessageLikeEventRequest {
type Response = Vec<Raw<AnyTimelineEvent>>;
}
impl FromMatrixDriverResponse for Vec<Raw<AnyTimelineEvent>> {
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
match ev {
MatrixDriverResponse::MatrixEventRead(response) => Some(response),
_ => {
error!("bug in MatrixDriver, received wrong event response");
None
}
}
}
}
#[derive(Clone, Debug)]
pub(crate) struct ReadStateEventRequest {
pub(crate) event_type: StateEventType,
pub(crate) state_key: StateKeySelector,
}
impl From<ReadStateEventRequest> for MatrixDriverRequestData {
fn from(value: ReadStateEventRequest) -> Self {
MatrixDriverRequestData::ReadStateEvent(value)
}
}
impl MatrixDriverRequest for ReadStateEventRequest {
type Response = Vec<Raw<AnyTimelineEvent>>;
}
#[derive(Clone, Debug, Deserialize)]
pub(crate) struct SendEventRequest {
#[serde(rename = "type")]
pub(crate) event_type: TimelineEventType,
pub(crate) state_key: Option<String>,
pub(crate) content: Box<RawJsonValue>,
pub(crate) delay: Option<u64>,
}
impl From<SendEventRequest> for MatrixDriverRequestData {
fn from(value: SendEventRequest) -> Self {
MatrixDriverRequestData::SendMatrixEvent(value)
}
}
impl MatrixDriverRequest for SendEventRequest {
type Response = SendEventResponse;
}
impl FromMatrixDriverResponse for SendEventResponse {
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
match ev {
MatrixDriverResponse::MatrixEventSent(response) => Some(response),
_ => {
error!("bug in MatrixDriver, received wrong event response");
None
}
}
}
}
#[derive(Deserialize, Debug, Clone)]
pub(crate) struct UpdateDelayedEventRequest {
pub(crate) action: update_delayed_event::unstable::UpdateAction,
pub(crate) delay_id: String,
}
impl From<UpdateDelayedEventRequest> for MatrixDriverRequestData {
fn from(value: UpdateDelayedEventRequest) -> Self {
MatrixDriverRequestData::UpdateDelayedEvent(value)
}
}
impl MatrixDriverRequest for UpdateDelayedEventRequest {
type Response = update_delayed_event::unstable::Response;
}
impl FromMatrixDriverResponse for update_delayed_event::unstable::Response {
fn from_response(ev: MatrixDriverResponse) -> Option<Self> {
match ev {
MatrixDriverResponse::MatrixDelayedEventUpdate(response) => Some(response),
_ => {
error!("bug in MatrixDriver, received wrong event response");
None
}
}
}
}