From 24b670204713b54bee092f39322286f7a3117b92 Mon Sep 17 00:00:00 2001 From: Devin R Date: Mon, 22 Jun 2020 07:26:09 -0400 Subject: [PATCH] Add to_*_event_stub methods to pdu, filter for correct event kind When creating some responses (sync) an AnyRoomEventStub is needed for this PduEvent will deserialize the JSON as a Stub event and a non stub event when needed. Ephemeral and account events are checked to be the correct type and filtered out if not. This requires an extra `deserialize` call which could/should be removed. TODO: Possibly get rid of EventJson in some places. --- src/client_server.rs | 222 ++++++++++++++++++++----------------- src/database/rooms/edus.rs | 2 + src/pdu.rs | 14 ++- 3 files changed, 136 insertions(+), 102 deletions(-) diff --git a/src/client_server.rs b/src/client_server.rs index 5a20eb3..15ced16 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -76,7 +76,7 @@ const TOKEN_LENGTH: usize = 256; const MXC_LENGTH: usize = 256; const SESSION_ID_LENGTH: usize = 256; -// #[get("/_matrix/client/versions")] +#[get("/_matrix/client/versions")] pub fn get_supported_versions_route() -> ConduitResult { let mut unstable_features = BTreeMap::new(); @@ -89,7 +89,7 @@ pub fn get_supported_versions_route() -> ConduitResult, body: Ruma, @@ -119,7 +119,7 @@ pub fn get_register_available_route( Ok(get_username_availability::Response { available: true }.into()) } -// #[post("/_matrix/client/r0/register", data = "")] +#[post("/_matrix/client/r0/register", data = "")] pub fn register_route( db: State<'_, Database>, body: Ruma, @@ -225,7 +225,7 @@ pub fn register_route( .into()) } -// #[get("/_matrix/client/r0/login")] +#[get("/_matrix/client/r0/login")] pub fn get_login_route() -> ConduitResult { Ok(get_login_types::Response { flows: vec![get_login_types::LoginType::Password], @@ -233,7 +233,7 @@ pub fn get_login_route() -> ConduitResult { .into()) } -// #[post("/_matrix/client/r0/login", data = "")] +#[post("/_matrix/client/r0/login", data = "")] pub fn login_route( db: State<'_, Database>, body: Ruma, @@ -291,7 +291,7 @@ pub fn login_route( .into()) } -// #[post("/_matrix/client/r0/logout", data = "")] +#[post("/_matrix/client/r0/logout", data = "")] pub fn logout_route( db: State<'_, Database>, body: Ruma, @@ -472,7 +472,7 @@ pub fn get_capabilities_route() -> ConduitResult { .into()) } -// #[get("/_matrix/client/r0/pushrules", data = "")] +#[get("/_matrix/client/r0/pushrules", data = "")] pub fn get_pushrules_all_route( db: State<'_, Database>, body: Ruma, @@ -514,7 +514,7 @@ pub fn set_pushrule_route( Ok(set_pushrule::Response.into()) } -// #[put("/_matrix/client/r0/pushrules/<_scope>/<_kind>/<_rule_id>/enabled")] +#[put("/_matrix/client/r0/pushrules/<_scope>/<_kind>/<_rule_id>/enabled")] pub fn set_pushrule_enabled_route( _scope: String, _kind: String, @@ -525,7 +525,7 @@ pub fn set_pushrule_enabled_route( Ok(set_pushrule_enabled::Response.into()) } -// #[get("/_matrix/client/r0/user/<_user_id>/filter/<_filter_id>")] +#[get("/_matrix/client/r0/user/<_user_id>/filter/<_filter_id>")] pub fn get_filter_route( _user_id: String, _filter_id: String, @@ -543,7 +543,7 @@ pub fn get_filter_route( .into()) } -// #[post("/_matrix/client/r0/user/<_user_id>/filter")] +#[post("/_matrix/client/r0/user/<_user_id>/filter")] pub fn create_filter_route(_user_id: String) -> ConduitResult { // TODO Ok(create_filter::Response { @@ -552,10 +552,10 @@ pub fn create_filter_route(_user_id: String) -> ConduitResult/account_data/<_type>", -// data = "" -// )] +#[put( + "/_matrix/client/r0/user/<_user_id>/account_data/<_type>", + data = "" +)] pub fn set_global_account_data_route( db: State<'_, Database>, body: Ruma, @@ -581,10 +581,10 @@ pub fn set_global_account_data_route( Ok(set_global_account_data::Response.into()) } -// #[get( -// "/_matrix/client/r0/user/<_user_id>/account_data/<_type>", -// data = "" -// )] +#[get( + "/_matrix/client/r0/user/<_user_id>/account_data/<_type>", + data = "" +)] pub fn get_global_account_data_route( db: State<'_, Database>, body: Ruma, @@ -614,7 +614,7 @@ pub fn get_global_account_data_route( } } -// #[put("/_matrix/client/r0/profile/<_user_id>/displayname", data = "")] +#[put("/_matrix/client/r0/profile/<_user_id>/displayname", data = "")] pub fn set_displayname_route( db: State<'_, Database>, body: Ruma, @@ -680,7 +680,7 @@ pub fn set_displayname_route( Ok(set_display_name::Response.into()) } -// #[get("/_matrix/client/r0/profile/<_user_id>/displayname", data = "")] +#[get("/_matrix/client/r0/profile/<_user_id>/displayname", data = "")] pub fn get_displayname_route( db: State<'_, Database>, body: Ruma, @@ -693,7 +693,7 @@ pub fn get_displayname_route( .into()) } -// #[put("/_matrix/client/r0/profile/<_user_id>/avatar_url", data = "")] +#[put("/_matrix/client/r0/profile/<_user_id>/avatar_url", data = "")] pub fn set_avatar_url_route( db: State<'_, Database>, body: Ruma, @@ -770,7 +770,7 @@ pub fn set_avatar_url_route( Ok(set_avatar_url::Response.into()) } -// #[get("/_matrix/client/r0/profile/<_user_id>/avatar_url", data = "")] +#[get("/_matrix/client/r0/profile/<_user_id>/avatar_url", data = "")] pub fn get_avatar_url_route( db: State<'_, Database>, body: Ruma, @@ -783,7 +783,7 @@ pub fn get_avatar_url_route( .into()) } -// #[get("/_matrix/client/r0/profile/<_user_id>", data = "")] +#[get("/_matrix/client/r0/profile/<_user_id>", data = "")] pub fn get_profile_route( db: State<'_, Database>, body: Ruma, @@ -808,7 +808,7 @@ pub fn get_profile_route( .into()) } -// #[put("/_matrix/client/r0/presence/<_user_id>/status", data = "")] +#[put("/_matrix/client/r0/presence/<_user_id>/status", data = "")] pub fn set_presence_route( db: State<'_, Database>, body: Ruma, @@ -838,7 +838,7 @@ pub fn set_presence_route( Ok(set_presence::Response.into()) } -// #[post("/_matrix/client/r0/keys/upload", data = "")] +#[post("/_matrix/client/r0/keys/upload", data = "")] pub fn upload_keys_route( db: State<'_, Database>, body: Ruma, @@ -867,7 +867,7 @@ pub fn upload_keys_route( .into()) } -// #[post("/_matrix/client/r0/keys/query", data = "")] +#[post("/_matrix/client/r0/keys/query", data = "")] pub fn get_keys_route( db: State<'_, Database>, body: Ruma, @@ -944,7 +944,7 @@ pub fn get_keys_route( .into()) } -// #[post("/_matrix/client/r0/keys/claim", data = "")] +#[post("/_matrix/client/r0/keys/claim", data = "")] pub fn claim_keys_route( db: State<'_, Database>, body: Ruma, @@ -1155,10 +1155,10 @@ pub fn set_read_marker_route( Ok(set_read_marker::Response.into()) } -// #[put( -// "/_matrix/client/r0/rooms/<_room_id>/typing/<_user_id>", -// data = "" -// )] +#[put( + "/_matrix/client/r0/rooms/<_room_id>/typing/<_user_id>", + data = "" +)] pub fn create_typing_event_route( db: State<'_, Database>, body: Ruma, @@ -1184,7 +1184,7 @@ pub fn create_typing_event_route( Ok(create_typing_event::Response.into()) } -// #[post("/_matrix/client/r0/createRoom", data = "")] +#[post("/_matrix/client/r0/createRoom", data = "")] pub fn create_room_route( db: State<'_, Database>, body: Ruma, @@ -1448,10 +1448,10 @@ pub fn create_room_route( Ok(create_room::Response { room_id }.into()) } -// #[put( -// "/_matrix/client/r0/rooms/<_room_id>/redact/<_event_id>/<_txn_id>", -// data = "" -// )] +#[put( + "/_matrix/client/r0/rooms/<_room_id>/redact/<_event_id>/<_txn_id>", + data = "" +)] pub fn redact_event_route( db: State<'_, Database>, body: Ruma, @@ -1478,7 +1478,7 @@ pub fn redact_event_route( Ok(redact_event::Response { event_id }.into()) } -// #[put("/_matrix/client/r0/directory/room/<_room_alias>", data = "")] +#[put("/_matrix/client/r0/directory/room/<_room_alias>", data = "")] pub fn create_alias_route( db: State<'_, Database>, body: Ruma, @@ -1494,7 +1494,7 @@ pub fn create_alias_route( Ok(create_alias::Response.into()) } -// #[delete("/_matrix/client/r0/directory/room/<_room_alias>", data = "")] +#[delete("/_matrix/client/r0/directory/room/<_room_alias>", data = "")] pub fn delete_alias_route( db: State<'_, Database>, body: Ruma, @@ -1505,7 +1505,7 @@ pub fn delete_alias_route( Ok(delete_alias::Response.into()) } -// #[get("/_matrix/client/r0/directory/room/<_room_alias>", data = "")] +#[get("/_matrix/client/r0/directory/room/<_room_alias>", data = "")] pub fn get_alias_route( db: State<'_, Database>, body: Ruma, @@ -1530,7 +1530,7 @@ pub fn get_alias_route( .into()) } -// #[post("/_matrix/client/r0/rooms/<_room_id>/join", data = "")] +#[post("/_matrix/client/r0/rooms/<_room_id>/join", data = "")] pub fn join_room_by_id_route( db: State<'_, Database>, body: Ruma, @@ -1565,7 +1565,7 @@ pub fn join_room_by_id_route( .into()) } -// #[post("/_matrix/client/r0/join/<_room_id_or_alias>", data = "")] +#[post("/_matrix/client/r0/join/<_room_id_or_alias>", data = "")] pub fn join_room_by_id_or_alias_route( db: State<'_, Database>, body: Ruma, @@ -1594,7 +1594,7 @@ pub fn join_room_by_id_or_alias_route( .into()) } -// #[post("/_matrix/client/r0/rooms/<_room_id>/leave", data = "")] +#[post("/_matrix/client/r0/rooms/<_room_id>/leave", data = "")] pub fn leave_room_route( db: State<'_, Database>, body: Ruma, @@ -1632,7 +1632,7 @@ pub fn leave_room_route( Ok(leave_room::Response.into()) } -// #[post("/_matrix/client/r0/rooms/<_room_id>/kick", data = "")] +#[post("/_matrix/client/r0/rooms/<_room_id>/kick", data = "")] pub fn kick_user_route( db: State<'_, Database>, body: Ruma, @@ -1672,7 +1672,7 @@ pub fn kick_user_route( Ok(kick_user::Response.into()) } -// #[post("/_matrix/client/r0/rooms/<_room_id>/ban", data = "")] +#[post("/_matrix/client/r0/rooms/<_room_id>/ban", data = "")] pub fn ban_user_route( db: State<'_, Database>, body: Ruma, @@ -1719,7 +1719,7 @@ pub fn ban_user_route( Ok(ban_user::Response.into()) } -// #[post("/_matrix/client/r0/rooms/<_room_id>/unban", data = "")] +#[post("/_matrix/client/r0/rooms/<_room_id>/unban", data = "")] pub fn unban_user_route( db: State<'_, Database>, body: Ruma, @@ -1758,7 +1758,7 @@ pub fn unban_user_route( Ok(unban_user::Response.into()) } -// #[post("/_matrix/client/r0/rooms/<_room_id>/forget", data = "")] +#[post("/_matrix/client/r0/rooms/<_room_id>/forget", data = "")] pub fn forget_room_route( db: State<'_, Database>, body: Ruma, @@ -1771,7 +1771,7 @@ pub fn forget_room_route( Ok(forget_room::Response.into()) } -// #[post("/_matrix/client/r0/rooms/<_room_id>/invite", data = "")] +#[post("/_matrix/client/r0/rooms/<_room_id>/invite", data = "")] pub fn invite_user_route( db: State<'_, Database>, body: Ruma, @@ -1802,7 +1802,7 @@ pub fn invite_user_route( } } -// #[put("/_matrix/client/r0/directory/list/room/<_room_id>", data = "")] +#[put("/_matrix/client/r0/directory/list/room/<_room_id>", data = "")] pub async fn set_room_visibility_route( db: State<'_, Database>, body: Ruma, @@ -1816,7 +1816,7 @@ pub async fn set_room_visibility_route( Ok(set_room_visibility::Response.into()) } -// #[get("/_matrix/client/r0/directory/list/room/<_room_id>", data = "")] +#[get("/_matrix/client/r0/directory/list/room/<_room_id>", data = "")] pub async fn get_room_visibility_route( db: State<'_, Database>, body: Ruma, @@ -1832,7 +1832,7 @@ pub async fn get_room_visibility_route( .into()) } -// #[get("/_matrix/client/r0/publicRooms", data = "")] +#[get("/_matrix/client/r0/publicRooms", data = "")] pub async fn get_public_rooms_route( db: State<'_, Database>, body: Ruma, @@ -1881,7 +1881,7 @@ pub async fn get_public_rooms_route( .into()) } -// #[post("/_matrix/client/r0/publicRooms", data = "")] +#[post("/_matrix/client/r0/publicRooms", data = "")] pub async fn get_public_rooms_filtered_route( db: State<'_, Database>, body: Ruma, @@ -1993,7 +1993,7 @@ pub async fn get_public_rooms_filtered_route( .into()) } -// #[post("/_matrix/client/r0/user_directory/search", data = "")] +#[post("/_matrix/client/r0/user_directory/search", data = "")] pub fn search_users_route( db: State<'_, Database>, body: Ruma, @@ -2059,7 +2059,7 @@ pub fn get_member_events_route( .into()) } -// #[get("/_matrix/client/r0/thirdparty/protocols")] +#[get("/_matrix/client/r0/thirdparty/protocols")] pub fn get_protocols_route() -> ConduitResult { warn!("TODO: get_protocols_route"); Ok(get_protocols::Response { @@ -2133,7 +2133,7 @@ pub fn create_message_event_route( } #[put( - // "/_matrix/client/r0/rooms/<_room_id>/state/<_event_type>/<_state_key>", + "/_matrix/client/r0/rooms/<_room_id>/state/<_event_type>/<_state_key>", data = "" )] pub fn create_state_event_for_key_route( @@ -2194,10 +2194,10 @@ pub fn create_state_event_for_key_route( Ok(create_state_event_for_key::Response { event_id }.into()) } -// #[put( -// "/_matrix/client/r0/rooms/<_room_id>/state/<_event_type>", -// data = "" -// )] +#[put( + "/_matrix/client/r0/rooms/<_room_id>/state/<_event_type>", + data = "" +)] pub fn create_state_event_for_empty_key_route( db: State<'_, Database>, body: Ruma, @@ -2241,7 +2241,7 @@ pub fn create_state_event_for_empty_key_route( .into()) } -// #[get("/_matrix/client/r0/rooms/<_room_id>/state", data = "")] +#[get("/_matrix/client/r0/rooms/<_room_id>/state", data = "")] pub fn get_state_events_route( db: State<'_, Database>, body: Ruma, @@ -2267,10 +2267,10 @@ pub fn get_state_events_route( .into()) } -// #[get( -// "/_matrix/client/r0/rooms/<_room_id>/state/<_event_type>/<_state_key>", -// data = "" -// )] +#[get( + "/_matrix/client/r0/rooms/<_room_id>/state/<_event_type>/<_state_key>", + data = "" +)] pub fn get_state_events_for_key_route( db: State<'_, Database>, body: Ruma, @@ -2302,10 +2302,10 @@ pub fn get_state_events_for_key_route( .into()) } -// #[get( -// "/_matrix/client/r0/rooms/<_room_id>/state/<_event_type>", -// data = "" -// )] +#[get( + "/_matrix/client/r0/rooms/<_room_id>/state/<_event_type>", + data = "" +)] pub fn get_state_events_for_empty_key_route( db: State<'_, Database>, body: Ruma, @@ -2336,7 +2336,7 @@ pub fn get_state_events_for_empty_key_route( .into()) } -// #[get("/_matrix/client/r0/sync", data = "")] +#[get("/_matrix/client/r0/sync", data = "")] pub fn sync_route( db: State<'_, Database>, body: Ruma, @@ -2513,7 +2513,7 @@ pub fn sync_route( let room_events = pdus .into_iter() - .map(|pdu| pdu.to_room_event()) + .map(|pdu| pdu.to_room_event_stub()) .collect::>(); let mut edus = db @@ -2521,6 +2521,14 @@ pub fn sync_route( .edus .roomlatests_since(&room_id, since)? .filter_map(|r| r.ok()) // Filter out buggy events + .filter_map(|r| { + if let Ok(EduEvent::Ephemeral(ev)) = r.deserialize() { + // TODO we could get rid of EventJson? + Some(EventJson::from(ev)) + } else { + None + } + }) .collect::>(); if db @@ -2579,7 +2587,7 @@ pub fn sync_route( db.rooms .room_state_full(&room_id)? .into_iter() - .map(|(_, pdu)| pdu.to_state_event()) + .map(|(_, pdu)| pdu.to_state_event_stub()) .collect() } else { Vec::new() @@ -2599,7 +2607,7 @@ pub fn sync_route( let pdus = db.rooms.pdus_since(&user_id, &room_id, since)?; let room_events = pdus .filter_map(|pdu| pdu.ok()) // Filter out buggy events - .map(|pdu| pdu.to_room_event()) + .map(|pdu| pdu.to_room_event_stub()) .collect(); // TODO: Only until leave point @@ -2608,6 +2616,14 @@ pub fn sync_route( .edus .roomlatests_since(&room_id, since)? .filter_map(|r| r.ok()) // Filter out buggy events + .filter_map(|r| { + if let Ok(EduEvent::Ephemeral(ev)) = r.deserialize() { + // TODO we could get rid of EventJson? + Some(EventJson::from(ev)) + } else { + None + } + }) .collect::>(); if db @@ -2618,9 +2634,9 @@ pub fn sync_route( { edus.push( serde_json::from_str( - &serde_json::to_string(&EduEvent::Typing( + &serde_json::to_string(&EduEvent::Ephemeral(AnyEphemeralRoomEvent::Typing( db.rooms.edus.roomactives_all(&room_id)?, - )) + ))) .expect("event is valid, we just created it"), ) .expect("event is valid, we just created it"), @@ -2694,7 +2710,13 @@ pub fn sync_route( .account_data .changes_since(None, &user_id, since)? .into_iter() - .map(|(_, v)| v) + .flat_map(|(_, v)| { + if let Some(EduEvent::Basic(account_event)) = v.deserialize().ok() { + Some(EventJson::from(account_event)) + } else { + None + } + }) .collect(), }, device_lists: sync_events::DeviceLists { @@ -2716,10 +2738,10 @@ pub fn sync_route( .into()) } -// #[get( -// "/_matrix/client/r0/rooms/<_room_id>/context/<_event_id>", -// data = "" -// )] +#[get( + "/_matrix/client/r0/rooms/<_room_id>/context/<_event_id>", + data = "" +)] pub fn get_context_route( db: State<'_, Database>, body: Ruma, @@ -2817,7 +2839,7 @@ pub fn get_context_route( .into()) } -// #[get("/_matrix/client/r0/rooms/<_room_id>/messages", data = "")] +#[get("/_matrix/client/r0/rooms/<_room_id>/messages", data = "")] pub fn get_message_events_route( db: State<'_, Database>, body: Ruma, @@ -2913,7 +2935,7 @@ pub fn get_message_events_route( } } -// #[get("/_matrix/client/r0/voip/turnServer")] +#[get("/_matrix/client/r0/voip/turnServer")] pub fn turn_server_route() -> ConduitResult { Err(Error::BadRequest( ErrorKind::NotFound, @@ -2921,7 +2943,7 @@ pub fn turn_server_route() -> ConduitResult { )) } -// #[post("/_matrix/client/r0/publicised_groups")] +#[post("/_matrix/client/r0/publicised_groups")] pub fn publicised_groups_route() -> ConduitResult { Err(Error::BadRequest( ErrorKind::NotFound, @@ -2929,10 +2951,10 @@ pub fn publicised_groups_route() -> ConduitResult/<_txn_id>", -// data = "" -// )] +#[put( + "/_matrix/client/r0/sendToDevice/<_event_type>/<_txn_id>", + data = "" +)] pub fn send_event_to_device_route( db: State<'_, Database>, body: Ruma, @@ -2978,7 +3000,7 @@ pub fn send_event_to_device_route( Ok(send_event_to_device::Response.into()) } -// #[get("/_matrix/media/r0/config")] +#[get("/_matrix/media/r0/config")] pub fn get_media_config_route() -> ConduitResult { Ok(get_media_config::Response { upload_size: (20_u32 * 1024 * 1024).into(), // 20 MB @@ -2986,7 +3008,7 @@ pub fn get_media_config_route() -> ConduitResult { .into()) } -// #[post("/_matrix/media/r0/upload", data = "")] +#[post("/_matrix/media/r0/upload", data = "")] pub fn create_content_route( db: State<'_, Database>, body: Ruma, @@ -3006,10 +3028,10 @@ pub fn create_content_route( Ok(create_content::Response { content_uri: mxc }.into()) } -// #[get( -// "/_matrix/media/r0/download/<_server_name>/<_media_id>", -// data = "" -// )] +#[get( + "/_matrix/media/r0/download/<_server_name>/<_media_id>", + data = "" +)] pub fn get_content_route( db: State<'_, Database>, body: Ruma, @@ -3031,10 +3053,10 @@ pub fn get_content_route( } } -// #[get( -// "/_matrix/media/r0/thumbnail/<_server_name>/<_media_id>", -// data = "" -// )] +#[get( + "/_matrix/media/r0/thumbnail/<_server_name>/<_media_id>", + data = "" +)] pub fn get_content_thumbnail_route( db: State<'_, Database>, body: Ruma, @@ -3056,7 +3078,7 @@ pub fn get_content_thumbnail_route( } } -// #[get("/_matrix/client/r0/devices", data = "")] +#[get("/_matrix/client/r0/devices", data = "")] pub fn get_devices_route( db: State<'_, Database>, body: Ruma, @@ -3072,7 +3094,7 @@ pub fn get_devices_route( Ok(get_devices::Response { devices }.into()) } -// #[get("/_matrix/client/r0/devices/<_device_id>", data = "")] +#[get("/_matrix/client/r0/devices/<_device_id>", data = "")] pub fn get_device_route( db: State<'_, Database>, body: Ruma, @@ -3088,7 +3110,7 @@ pub fn get_device_route( Ok(get_device::Response { device }.into()) } -// #[put("/_matrix/client/r0/devices/<_device_id>", data = "")] +#[put("/_matrix/client/r0/devices/<_device_id>", data = "")] pub fn update_device_route( db: State<'_, Database>, body: Ruma, @@ -3109,7 +3131,7 @@ pub fn update_device_route( Ok(update_device::Response.into()) } -// #[delete("/_matrix/client/r0/devices/<_device_id>", data = "")] +#[delete("/_matrix/client/r0/devices/<_device_id>", data = "")] pub fn delete_device_route( db: State<'_, Database>, body: Ruma, @@ -3153,7 +3175,7 @@ pub fn delete_device_route( Ok(delete_device::Response.into()) } -// #[post("/_matrix/client/r0/delete_devices", data = "")] +#[post("/_matrix/client/r0/delete_devices", data = "")] pub fn delete_devices_route( db: State<'_, Database>, body: Ruma, diff --git a/src/database/rooms/edus.rs b/src/database/rooms/edus.rs index de138b6..c352a01 100644 --- a/src/database/rooms/edus.rs +++ b/src/database/rooms/edus.rs @@ -62,6 +62,8 @@ impl RoomEdus { room_id: &RoomId, since: u64, ) -> Result>>> { + // TODO is this ^^^^^^^ + // only ever a read receipt could we just return EphemeralRoomEvent here? let mut prefix = room_id.to_string().as_bytes().to_vec(); prefix.push(0xff); diff --git a/src/pdu.rs b/src/pdu.rs index 4504ae6..73bb869 100644 --- a/src/pdu.rs +++ b/src/pdu.rs @@ -2,8 +2,8 @@ use crate::{Error, Result}; use js_int::UInt; use ruma::{ events::{ - pdu::EventHash, AnyRoomEvent, AnyStateEvent, AnyStrippedStateEventStub, EventJson, - EventType, + pdu::EventHash, AnyRoomEvent, AnyRoomEventStub, AnyStateEvent, AnyStateEventStub, + AnyStrippedStateEventStub, EventJson, EventType, }, identifiers::{EventId, RoomId, UserId}, }; @@ -78,6 +78,11 @@ impl PduEvent { Ok(()) } + pub fn to_room_event_stub(&self) -> EventJson { + let json = serde_json::to_string(&self).expect("PDUs are always valid"); + serde_json::from_str::>(&json) + .expect("EventJson::from_str always works") + } pub fn to_room_event(&self) -> EventJson { let json = serde_json::to_string(&self).expect("PDUs are always valid"); serde_json::from_str::>(&json) @@ -88,6 +93,11 @@ impl PduEvent { serde_json::from_str::>(&json) .expect("EventJson::from_str always works") } + pub fn to_state_event_stub(&self) -> EventJson { + let json = serde_json::to_string(&self).expect("PDUs are always valid"); + serde_json::from_str::>(&json) + .expect("EventJson::from_str always works") + } pub fn to_stripped_state_event(&self) -> EventJson { let json = serde_json::to_string(&self).expect("PDUs are always valid"); serde_json::from_str::>(&json)