From 668e8b2239b1600573e993fa2f0a9138997d60d8 Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Thu, 13 Jun 2019 01:34:05 -0700 Subject: [PATCH] Add m.room.tombstone. --- src/collections/all.rs | 37 +++++++++++++++++++++++++++++++++++++ src/collections/only.rs | 2 ++ src/lib.rs | 4 ++++ src/room.rs | 1 + src/room/tombstone.rs | 19 +++++++++++++++++++ 5 files changed, 63 insertions(+) create mode 100644 src/room/tombstone.rs diff --git a/src/collections/all.rs b/src/collections/all.rs index 62d4494f..f49cd8ff 100644 --- a/src/collections/all.rs +++ b/src/collections/all.rs @@ -26,6 +26,7 @@ use crate::{ redaction::RedactionEvent, server_acl::ServerAclEvent, third_party_invite::ThirdPartyInviteEvent, + tombstone::TombstoneEvent, topic::TopicEvent, }, sticker::StickerEvent, @@ -91,6 +92,8 @@ pub enum Event { RoomServerAcl(ServerAclEvent), /// m.room.third_party_invite RoomThirdPartyInvite(ThirdPartyInviteEvent), + /// m.room.tombstone + RoomTombstone(TombstoneEvent), /// m.room.topic RoomTopic(TopicEvent), /// m.sticker @@ -151,6 +154,8 @@ pub enum RoomEvent { RoomServerAcl(ServerAclEvent), /// m.room.third_party_invite RoomThirdPartyInvite(ThirdPartyInviteEvent), + /// m.room.tombstone + RoomTombstone(TombstoneEvent), /// m.room.topic RoomTopic(TopicEvent), /// m.sticker @@ -191,6 +196,8 @@ pub enum StateEvent { RoomServerAcl(ServerAclEvent), /// m.room.third_party_invite RoomThirdPartyInvite(ThirdPartyInviteEvent), + /// m.room.tombstone + RoomTombstone(TombstoneEvent), /// m.room.topic RoomTopic(TopicEvent), /// Any state event that is not part of the specification. @@ -228,6 +235,7 @@ impl Serialize for Event { Event::RoomRedaction(ref event) => event.serialize(serializer), Event::RoomServerAcl(ref event) => event.serialize(serializer), Event::RoomThirdPartyInvite(ref event) => event.serialize(serializer), + Event::RoomTombstone(ref event) => event.serialize(serializer), Event::RoomTopic(ref event) => event.serialize(serializer), Event::Sticker(ref event) => event.serialize(serializer), Event::Tag(ref event) => event.serialize(serializer), @@ -457,6 +465,14 @@ impl<'de> Deserialize<'de> for Event { Ok(Event::RoomThirdPartyInvite(event)) } + EventType::RoomTombstone => { + let event = match from_value::(value) { + Ok(event) => event, + Err(error) => return Err(D::Error::custom(error.to_string())), + }; + + Ok(Event::RoomTombstone(event)) + } EventType::RoomTopic => { let event = match from_value::(value) { Ok(event) => event, @@ -546,6 +562,7 @@ impl Serialize for RoomEvent { RoomEvent::RoomRedaction(ref event) => event.serialize(serializer), RoomEvent::RoomServerAcl(ref event) => event.serialize(serializer), RoomEvent::RoomThirdPartyInvite(ref event) => event.serialize(serializer), + RoomEvent::RoomTombstone(ref event) => event.serialize(serializer), RoomEvent::RoomTopic(ref event) => event.serialize(serializer), RoomEvent::Sticker(ref event) => event.serialize(serializer), RoomEvent::CustomRoom(ref event) => event.serialize(serializer), @@ -732,6 +749,14 @@ impl<'de> Deserialize<'de> for RoomEvent { Ok(RoomEvent::RoomThirdPartyInvite(event)) } + EventType::RoomTombstone => { + let event = match from_value::(value) { + Ok(event) => event, + Err(error) => return Err(D::Error::custom(error.to_string())), + }; + + Ok(RoomEvent::RoomTombstone(event)) + } EventType::RoomTopic => { let event = match from_value::(value) { Ok(event) => event, @@ -795,6 +820,7 @@ impl Serialize for StateEvent { StateEvent::RoomPowerLevels(ref event) => event.serialize(serializer), StateEvent::RoomServerAcl(ref event) => event.serialize(serializer), StateEvent::RoomThirdPartyInvite(ref event) => event.serialize(serializer), + StateEvent::RoomTombstone(ref event) => event.serialize(serializer), StateEvent::RoomTopic(ref event) => event.serialize(serializer), StateEvent::CustomState(ref event) => event.serialize(serializer), } @@ -923,6 +949,14 @@ impl<'de> Deserialize<'de> for StateEvent { Ok(StateEvent::RoomThirdPartyInvite(event)) } + EventType::RoomTombstone => { + let event = match from_value::(value) { + Ok(event) => event, + Err(error) => return Err(D::Error::custom(error.to_string())), + }; + + Ok(StateEvent::RoomTombstone(event)) + } EventType::RoomTopic => { let event = match from_value::(value) { Ok(event) => event, @@ -993,6 +1027,7 @@ impl_from_t_for_event!(PowerLevelsEvent, RoomPowerLevels); impl_from_t_for_event!(RedactionEvent, RoomRedaction); impl_from_t_for_event!(ServerAclEvent, RoomServerAcl); impl_from_t_for_event!(ThirdPartyInviteEvent, RoomThirdPartyInvite); +impl_from_t_for_event!(TombstoneEvent, RoomTombstone); impl_from_t_for_event!(TopicEvent, RoomTopic); impl_from_t_for_event!(StickerEvent, Sticker); impl_from_t_for_event!(TagEvent, Tag); @@ -1032,6 +1067,7 @@ impl_from_t_for_room_event!(RedactionEvent, RoomRedaction); impl_from_t_for_room_event!(ServerAclEvent, RoomServerAcl); impl_from_t_for_room_event!(StickerEvent, Sticker); impl_from_t_for_room_event!(ThirdPartyInviteEvent, RoomThirdPartyInvite); +impl_from_t_for_room_event!(TombstoneEvent, RoomTombstone); impl_from_t_for_room_event!(TopicEvent, RoomTopic); impl_from_t_for_room_event!(CustomRoomEvent, CustomRoom); impl_from_t_for_room_event!(CustomStateEvent, CustomState); @@ -1059,5 +1095,6 @@ impl_from_t_for_state_event!(PinnedEventsEvent, RoomPinnedEvents); impl_from_t_for_state_event!(PowerLevelsEvent, RoomPowerLevels); impl_from_t_for_state_event!(ServerAclEvent, RoomServerAcl); impl_from_t_for_state_event!(ThirdPartyInviteEvent, RoomThirdPartyInvite); +impl_from_t_for_state_event!(TombstoneEvent, RoomTombstone); impl_from_t_for_state_event!(TopicEvent, RoomTopic); impl_from_t_for_state_event!(CustomStateEvent, CustomState); diff --git a/src/collections/only.rs b/src/collections/only.rs index c958b0d9..dae1d8f5 100644 --- a/src/collections/only.rs +++ b/src/collections/only.rs @@ -189,6 +189,7 @@ impl<'de> Deserialize<'de> for Event { | EventType::RoomRedaction | EventType::RoomServerAcl | EventType::RoomThirdPartyInvite + | EventType::RoomTombstone | EventType::RoomTopic | EventType::Sticker => Err(D::Error::custom( "not exclusively a basic event".to_string(), @@ -324,6 +325,7 @@ impl<'de> Deserialize<'de> for RoomEvent { | EventType::RoomPowerLevels | EventType::RoomServerAcl | EventType::RoomThirdPartyInvite + | EventType::RoomTombstone | EventType::RoomTopic | EventType::Tag | EventType::Typing => { diff --git a/src/lib.rs b/src/lib.rs index 9a16654e..49244353 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -185,6 +185,8 @@ pub enum EventType { RoomServerAcl, /// m.room.third_party_invite RoomThirdPartyInvite, + /// m.room.tombstone + RoomTombstone, /// m.room.topic RoomTopic, /// m.sticker @@ -286,6 +288,7 @@ impl Display for EventType { EventType::RoomRedaction => "m.room.redaction", EventType::RoomServerAcl => "m.room.server_acl", EventType::RoomThirdPartyInvite => "m.room.third_party_invite", + EventType::RoomTombstone => "m.room.tombstone", EventType::RoomTopic => "m.room.topic", EventType::Sticker => "m.sticker", EventType::Tag => "m.tag", @@ -325,6 +328,7 @@ impl<'a> From<&'a str> for EventType { "m.room.redaction" => EventType::RoomRedaction, "m.room.server_acl" => EventType::RoomServerAcl, "m.room.third_party_invite" => EventType::RoomThirdPartyInvite, + "m.room.tombstone" => EventType::RoomTombstone, "m.room.topic" => EventType::RoomTopic, "m.sticker" => EventType::Sticker, "m.tag" => EventType::Tag, diff --git a/src/room.rs b/src/room.rs index 95ae0aca..9eb8c80e 100644 --- a/src/room.rs +++ b/src/room.rs @@ -21,6 +21,7 @@ pub mod power_levels; pub mod redaction; pub mod server_acl; pub mod third_party_invite; +pub mod tombstone; pub mod topic; /// Metadata about an image. diff --git a/src/room/tombstone.rs b/src/room/tombstone.rs new file mode 100644 index 00000000..cca37288 --- /dev/null +++ b/src/room/tombstone.rs @@ -0,0 +1,19 @@ +//! Types for the *m.room.tombstone* event. + +use ruma_identifiers::RoomId; +use serde::{Deserialize, Serialize}; + +state_event! { + /// A state event signifying that a room has been upgraded to a different room version, and that + /// clients should go there. + pub struct TombstoneEvent(TombstoneEventContent) {} +} + +/// The payload of an *m.room.tombstone* event. +#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] +pub struct TombstoneEventContent { + /// A server-defined message. + pub body: String, + /// The new room the client should be visiting. + pub replacement_room: RoomId, +}