matrix_sdk/widget/machine/
from_widget.rsuse as_variant::as_variant;
use ruma::{
api::client::{
delayed_events::{delayed_message_event, delayed_state_event, update_delayed_event},
error::{ErrorBody, StandardErrorBody},
},
events::{AnyTimelineEvent, MessageLikeEventType, StateEventType},
serde::Raw,
OwnedEventId, OwnedRoomId,
};
use serde::{Deserialize, Serialize};
use super::{SendEventRequest, UpdateDelayedEventRequest};
use crate::{widget::StateKeySelector, Error, HttpError, RumaApiError};
#[derive(Deserialize, Debug)]
#[serde(tag = "action", rename_all = "snake_case", content = "data")]
pub(super) enum FromWidgetRequest {
SupportedApiVersions {},
ContentLoaded {},
#[serde(rename = "get_openid")]
GetOpenId {},
#[serde(rename = "org.matrix.msc2876.read_events")]
ReadEvent(ReadEventRequest),
SendEvent(SendEventRequest),
#[serde(rename = "org.matrix.msc4157.update_delayed_event")]
DelayedEventUpdate(UpdateDelayedEventRequest),
}
#[derive(Serialize)]
pub(super) struct FromWidgetErrorResponse {
error: FromWidgetError,
}
impl FromWidgetErrorResponse {
pub(crate) fn from_http_error(error: HttpError) -> Self {
let message = error.to_string();
let matrix_api_error = as_variant!(error, HttpError::Api(ruma::api::error::FromHttpResponseError::Server(RumaApiError::ClientApi(err))) => err);
Self {
error: FromWidgetError {
message,
matrix_api_error: matrix_api_error.and_then(|api_error| match api_error.body {
ErrorBody::Standard { kind, message } => Some(FromWidgetMatrixErrorBody {
http_status: api_error.status_code.as_u16().into(),
response: StandardErrorBody { kind, message },
}),
_ => None,
}),
},
}
}
pub(crate) fn from_error(error: Error) -> Self {
match error {
Error::Http(e) => FromWidgetErrorResponse::from_http_error(e),
Error::UnknownError(e) => FromWidgetErrorResponse::from_string(e.to_string()),
_ => FromWidgetErrorResponse::from_string(error.to_string()),
}
}
pub(crate) fn from_string<S: Into<String>>(error: S) -> Self {
Self { error: FromWidgetError { message: error.into(), matrix_api_error: None } }
}
}
#[derive(Serialize)]
struct FromWidgetError {
message: String,
matrix_api_error: Option<FromWidgetMatrixErrorBody>,
}
#[derive(Serialize)]
struct FromWidgetMatrixErrorBody {
http_status: u32,
response: StandardErrorBody,
}
#[derive(Serialize)]
pub(super) struct SupportedApiVersionsResponse {
supported_versions: Vec<ApiVersion>,
}
impl SupportedApiVersionsResponse {
pub(super) fn new() -> Self {
Self {
supported_versions: vec![
ApiVersion::V0_0_1,
ApiVersion::V0_0_2,
ApiVersion::MSC2762,
ApiVersion::MSC2871,
ApiVersion::MSC3819,
],
}
}
}
#[derive(Serialize)]
#[allow(dead_code)] pub(super) enum ApiVersion {
#[serde(rename = "0.0.1")]
V0_0_1,
#[serde(rename = "0.0.2")]
V0_0_2,
#[serde(rename = "org.matrix.msc2762")]
MSC2762,
#[serde(rename = "org.matrix.msc2871")]
MSC2871,
#[serde(rename = "org.matrix.msc2931")]
MSC2931,
#[serde(rename = "org.matrix.msc2974")]
MSC2974,
#[serde(rename = "org.matrix.msc2876")]
MSC2876,
#[serde(rename = "org.matrix.msc3819")]
MSC3819,
#[serde(rename = "town.robin.msc3846")]
MSC3846,
}
#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub(super) enum ReadEventRequest {
ReadStateEvent {
#[serde(rename = "type")]
event_type: StateEventType,
state_key: StateKeySelector,
},
ReadMessageLikeEvent {
#[serde(rename = "type")]
event_type: MessageLikeEventType,
limit: Option<u32>,
},
}
#[derive(Debug, Serialize)]
pub(super) struct ReadEventResponse {
pub(super) events: Vec<Raw<AnyTimelineEvent>>,
}
#[derive(Serialize, Debug)]
pub(crate) struct SendEventResponse {
pub(crate) room_id: Option<OwnedRoomId>,
pub(crate) event_id: Option<OwnedEventId>,
pub(crate) delay_id: Option<String>,
}
impl SendEventResponse {
pub(crate) fn from_event_id(event_id: OwnedEventId) -> Self {
SendEventResponse { room_id: None, event_id: Some(event_id), delay_id: None }
}
pub(crate) fn set_room_id(&mut self, room_id: OwnedRoomId) {
self.room_id = Some(room_id);
}
}
impl From<delayed_message_event::unstable::Response> for SendEventResponse {
fn from(val: delayed_message_event::unstable::Response) -> Self {
SendEventResponse { room_id: None, event_id: None, delay_id: Some(val.delay_id) }
}
}
impl From<delayed_state_event::unstable::Response> for SendEventResponse {
fn from(val: delayed_state_event::unstable::Response) -> Self {
SendEventResponse { room_id: None, event_id: None, delay_id: Some(val.delay_id) }
}
}
#[derive(Serialize, Debug)]
pub(crate) struct UpdateDelayedEventResponse {}
impl From<update_delayed_event::unstable::Response> for UpdateDelayedEventResponse {
fn from(_: update_delayed_event::unstable::Response) -> Self {
Self {}
}
}