events: Add support for extensible text event
As defined in MSC1767
This commit is contained in:
		
							parent
							
								
									0d49715f29
								
							
						
					
					
						commit
						f78d3480a5
					
				@ -4,6 +4,10 @@ Breaking changes:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
* Rename `MessageEvent` and the associated types and traits to `MessageLikeEvent`
 | 
					* Rename `MessageEvent` and the associated types and traits to `MessageLikeEvent`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Improvements:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					* Add unstable support for extensible text message events ([MSC1767](https://github.com/matrix-org/matrix-spec-proposals/pull/1767))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 0.26.0
 | 
					# 0.26.0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Breaking changes:
 | 
					Breaking changes:
 | 
				
			||||||
 | 
				
			|||||||
@ -21,6 +21,7 @@ markdown = ["pulldown-cmark"]
 | 
				
			|||||||
unstable-exhaustive-types = []
 | 
					unstable-exhaustive-types = []
 | 
				
			||||||
unstable-pdu = []
 | 
					unstable-pdu = []
 | 
				
			||||||
unstable-pre-spec = []
 | 
					unstable-pre-spec = []
 | 
				
			||||||
 | 
					unstable-msc1767 = []
 | 
				
			||||||
unstable-msc2448 = []
 | 
					unstable-msc2448 = []
 | 
				
			||||||
unstable-msc2675 = []
 | 
					unstable-msc2675 = []
 | 
				
			||||||
unstable-msc2676 = []
 | 
					unstable-msc2676 = []
 | 
				
			||||||
 | 
				
			|||||||
@ -7,7 +7,7 @@ use serde_json::value::RawValue as RawJsonValue;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
use crate::{
 | 
					use crate::{
 | 
				
			||||||
    key,
 | 
					    key,
 | 
				
			||||||
    room::{encrypted, message, redaction::SyncRoomRedactionEvent},
 | 
					    room::{encrypted, redaction::SyncRoomRedactionEvent},
 | 
				
			||||||
    Redact, UnsignedDeHelper,
 | 
					    Redact, UnsignedDeHelper,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -44,6 +44,8 @@ event_enum! {
 | 
				
			|||||||
        "m.key.verification.key",
 | 
					        "m.key.verification.key",
 | 
				
			||||||
        "m.key.verification.mac",
 | 
					        "m.key.verification.mac",
 | 
				
			||||||
        "m.key.verification.done",
 | 
					        "m.key.verification.done",
 | 
				
			||||||
 | 
					        #[cfg(feature = "unstable-msc1767")]
 | 
				
			||||||
 | 
					        "m.message",
 | 
				
			||||||
        #[cfg(feature = "unstable-msc2677")]
 | 
					        #[cfg(feature = "unstable-msc2677")]
 | 
				
			||||||
        "m.reaction",
 | 
					        "m.reaction",
 | 
				
			||||||
        "m.room.encrypted",
 | 
					        "m.room.encrypted",
 | 
				
			||||||
@ -351,18 +353,9 @@ impl AnyMessageLikeEventContent {
 | 
				
			|||||||
                }))
 | 
					                }))
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            Self::RoomEncrypted(ev) => ev.relates_to.clone(),
 | 
					            Self::RoomEncrypted(ev) => ev.relates_to.clone(),
 | 
				
			||||||
            Self::RoomMessage(ev) => ev.relates_to.clone().map(|rel| match rel {
 | 
					            Self::RoomMessage(ev) => ev.relates_to.clone().map(Into::into),
 | 
				
			||||||
                message::Relation::Reply { in_reply_to } => {
 | 
					            #[cfg(feature = "unstable-msc1767")]
 | 
				
			||||||
                    encrypted::Relation::Reply { in_reply_to }
 | 
					            Self::Message(ev) => ev.relates_to.clone().map(Into::into),
 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                #[cfg(feature = "unstable-msc2676")]
 | 
					 | 
				
			||||||
                message::Relation::Replacement(re) => {
 | 
					 | 
				
			||||||
                    encrypted::Relation::Replacement(encrypted::Replacement {
 | 
					 | 
				
			||||||
                        event_id: re.event_id,
 | 
					 | 
				
			||||||
                    })
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                message::Relation::_Custom => encrypted::Relation::_Custom,
 | 
					 | 
				
			||||||
            }),
 | 
					 | 
				
			||||||
            Self::CallAnswer(_)
 | 
					            Self::CallAnswer(_)
 | 
				
			||||||
            | Self::CallInvite(_)
 | 
					            | Self::CallInvite(_)
 | 
				
			||||||
            | Self::CallHangup(_)
 | 
					            | Self::CallHangup(_)
 | 
				
			||||||
 | 
				
			|||||||
@ -176,6 +176,8 @@ pub mod forwarded_room_key;
 | 
				
			|||||||
pub mod fully_read;
 | 
					pub mod fully_read;
 | 
				
			||||||
pub mod ignored_user_list;
 | 
					pub mod ignored_user_list;
 | 
				
			||||||
pub mod key;
 | 
					pub mod key;
 | 
				
			||||||
 | 
					#[cfg(feature = "unstable-msc1767")]
 | 
				
			||||||
 | 
					pub mod message;
 | 
				
			||||||
#[cfg(feature = "unstable-pdu")]
 | 
					#[cfg(feature = "unstable-pdu")]
 | 
				
			||||||
pub mod pdu;
 | 
					pub mod pdu;
 | 
				
			||||||
pub mod policy;
 | 
					pub mod policy;
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										154
									
								
								crates/ruma-events/src/message.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										154
									
								
								crates/ruma-events/src/message.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,154 @@
 | 
				
			|||||||
 | 
					//! Types for extensible text message events ([MSC1767]).
 | 
				
			||||||
 | 
					//!
 | 
				
			||||||
 | 
					//! [MSC1767]: https://github.com/matrix-org/matrix-spec-proposals/pull/1767
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use ruma_macros::EventContent;
 | 
				
			||||||
 | 
					use serde::{Deserialize, Serialize};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					mod content_serde;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use content_serde::MessageContentSerDeHelper;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use crate::room::message::Relation;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Text message content.
 | 
				
			||||||
 | 
					#[derive(Clone, Debug, Serialize, Deserialize)]
 | 
				
			||||||
 | 
					#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
 | 
				
			||||||
 | 
					pub struct Text {
 | 
				
			||||||
 | 
					    /// The mime type of the `body`.
 | 
				
			||||||
 | 
					    #[serde(default = "Text::default_mimetype")]
 | 
				
			||||||
 | 
					    pub mimetype: String,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// The text content.
 | 
				
			||||||
 | 
					    pub body: String,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Text {
 | 
				
			||||||
 | 
					    /// Creates a new plain text message body.
 | 
				
			||||||
 | 
					    pub fn plain(body: impl Into<String>) -> Self {
 | 
				
			||||||
 | 
					        Self { mimetype: "text/plain".to_owned(), body: body.into() }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// Creates a new HTML-formatted message body.
 | 
				
			||||||
 | 
					    pub fn html(body: impl Into<String>) -> Self {
 | 
				
			||||||
 | 
					        Self { mimetype: "text/html".to_owned(), body: body.into() }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// Creates a new HTML-formatted message body by parsing the Markdown in `body`.
 | 
				
			||||||
 | 
					    ///
 | 
				
			||||||
 | 
					    /// Returns `None` if no Markdown formatting was found.
 | 
				
			||||||
 | 
					    #[cfg(feature = "markdown")]
 | 
				
			||||||
 | 
					    pub fn markdown(body: impl AsRef<str>) -> Option<Self> {
 | 
				
			||||||
 | 
					        let body = body.as_ref();
 | 
				
			||||||
 | 
					        let mut html_body = String::new();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pulldown_cmark::html::push_html(&mut html_body, pulldown_cmark::Parser::new(body));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        (html_body != format!("<p>{}</p>\n", body)).then(|| Self::html(html_body))
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn default_mimetype() -> String {
 | 
				
			||||||
 | 
					        "text/plain".to_owned()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Text message content.
 | 
				
			||||||
 | 
					#[derive(Clone, Debug, Deserialize)]
 | 
				
			||||||
 | 
					#[serde(try_from = "MessageContentSerDeHelper")]
 | 
				
			||||||
 | 
					pub struct MessageContent(pub(crate) Vec<Text>);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl MessageContent {
 | 
				
			||||||
 | 
					    /// A convenience constructor to create a plain text message.
 | 
				
			||||||
 | 
					    pub fn plain(body: impl Into<String>) -> Self {
 | 
				
			||||||
 | 
					        Self(vec![Text::plain(body)])
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// A convenience constructor to create an HTML message.
 | 
				
			||||||
 | 
					    pub fn html(body: impl Into<String>, html_body: impl Into<String>) -> Self {
 | 
				
			||||||
 | 
					        Self(vec![Text::html(html_body), Text::plain(body)])
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// A convenience constructor to create a Markdown message.
 | 
				
			||||||
 | 
					    ///
 | 
				
			||||||
 | 
					    /// Returns an HTML message if some Markdown formatting was detected, otherwise returns a plain
 | 
				
			||||||
 | 
					    /// text message.
 | 
				
			||||||
 | 
					    #[cfg(feature = "markdown")]
 | 
				
			||||||
 | 
					    pub fn markdown(body: impl AsRef<str> + Into<String>) -> Self {
 | 
				
			||||||
 | 
					        let mut message = Vec::with_capacity(2);
 | 
				
			||||||
 | 
					        if let Some(html_body) = Text::markdown(&body) {
 | 
				
			||||||
 | 
					            message.push(html_body);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        message.push(Text::plain(body));
 | 
				
			||||||
 | 
					        Self(message)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// Get the plain text representation of this message.
 | 
				
			||||||
 | 
					    pub fn find_plain(&self) -> Option<&str> {
 | 
				
			||||||
 | 
					        self.variants()
 | 
				
			||||||
 | 
					            .iter()
 | 
				
			||||||
 | 
					            .find(|content| content.mimetype == "text/plain")
 | 
				
			||||||
 | 
					            .map(|content| content.body.as_ref())
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// Get the HTML representation of this message.
 | 
				
			||||||
 | 
					    pub fn find_html(&self) -> Option<&str> {
 | 
				
			||||||
 | 
					        self.variants()
 | 
				
			||||||
 | 
					            .iter()
 | 
				
			||||||
 | 
					            .find(|content| content.mimetype == "text/html")
 | 
				
			||||||
 | 
					            .map(|content| content.body.as_ref())
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// Get all the text representations of this message.
 | 
				
			||||||
 | 
					    pub fn variants(&self) -> &[Text] {
 | 
				
			||||||
 | 
					        &self.0
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Trait for messages containing a text representation.
 | 
				
			||||||
 | 
					pub trait TextMessage {
 | 
				
			||||||
 | 
					    /// Get the text representation of this message.
 | 
				
			||||||
 | 
					    fn message(&self) -> &MessageContent;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// The payload for an extensible text message.
 | 
				
			||||||
 | 
					#[derive(Clone, Debug, Serialize, Deserialize, EventContent)]
 | 
				
			||||||
 | 
					#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
 | 
				
			||||||
 | 
					#[ruma_event(type = "m.message", kind = MessageLike)]
 | 
				
			||||||
 | 
					pub struct MessageEventContent {
 | 
				
			||||||
 | 
					    /// The message's text content.
 | 
				
			||||||
 | 
					    #[serde(flatten)]
 | 
				
			||||||
 | 
					    pub message: MessageContent,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// Information about related messages for [rich replies].
 | 
				
			||||||
 | 
					    ///
 | 
				
			||||||
 | 
					    /// [rich replies]: https://spec.matrix.org/v1.2/client-server-api/#rich-replies
 | 
				
			||||||
 | 
					    #[serde(flatten, skip_serializing_if = "Option::is_none")]
 | 
				
			||||||
 | 
					    pub relates_to: Option<Relation>,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl MessageEventContent {
 | 
				
			||||||
 | 
					    /// A convenience constructor to create a plain text message.
 | 
				
			||||||
 | 
					    pub fn plain(body: impl Into<String>) -> Self {
 | 
				
			||||||
 | 
					        Self { message: MessageContent::plain(body), relates_to: None }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// A convenience constructor to create an HTML message.
 | 
				
			||||||
 | 
					    pub fn html(body: impl Into<String>, html_body: impl Into<String>) -> Self {
 | 
				
			||||||
 | 
					        Self { message: MessageContent::html(body, html_body), relates_to: None }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// A convenience constructor to create a Markdown message.
 | 
				
			||||||
 | 
					    ///
 | 
				
			||||||
 | 
					    /// Returns an HTML message if some Markdown formatting was detected, otherwise returns a plain
 | 
				
			||||||
 | 
					    /// text message.
 | 
				
			||||||
 | 
					    #[cfg(feature = "markdown")]
 | 
				
			||||||
 | 
					    pub fn markdown(body: impl AsRef<str> + Into<String>) -> Self {
 | 
				
			||||||
 | 
					        Self { message: MessageContent::markdown(body), relates_to: None }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl TextMessage for MessageEventContent {
 | 
				
			||||||
 | 
					    fn message(&self) -> &MessageContent {
 | 
				
			||||||
 | 
					        &self.message
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										55
									
								
								crates/ruma-events/src/message/content_serde.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								crates/ruma-events/src/message/content_serde.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,55 @@
 | 
				
			|||||||
 | 
					//! `Serialize` and `Deserialize` implementations for extensible events (MSC1767).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use std::convert::TryFrom;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use serde::{ser::SerializeStruct, Deserialize, Serialize};
 | 
				
			||||||
 | 
					use thiserror::Error;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use super::{MessageContent, Text};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Error, Debug)]
 | 
				
			||||||
 | 
					pub enum MessageContentSerdeError {
 | 
				
			||||||
 | 
					    #[error("missing field `{0}`")]
 | 
				
			||||||
 | 
					    MissingField(String),
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Debug, Default, Deserialize)]
 | 
				
			||||||
 | 
					pub(crate) struct MessageContentSerDeHelper {
 | 
				
			||||||
 | 
					    /// Plain text short form.
 | 
				
			||||||
 | 
					    #[serde(rename = "org.matrix.msc1767.text", skip_serializing_if = "Option::is_none")]
 | 
				
			||||||
 | 
					    text: Option<String>,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// Long form.
 | 
				
			||||||
 | 
					    #[serde(rename = "org.matrix.msc1767.message", default, skip_serializing_if = "Vec::is_empty")]
 | 
				
			||||||
 | 
					    message: Vec<Text>,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl TryFrom<MessageContentSerDeHelper> for MessageContent {
 | 
				
			||||||
 | 
					    type Error = MessageContentSerdeError;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn try_from(helper: MessageContentSerDeHelper) -> Result<Self, Self::Error> {
 | 
				
			||||||
 | 
					        if !helper.message.is_empty() {
 | 
				
			||||||
 | 
					            Ok(Self(helper.message))
 | 
				
			||||||
 | 
					        } else if let Some(text) = helper.text {
 | 
				
			||||||
 | 
					            Ok(Self::plain(text))
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            Err(MessageContentSerdeError::MissingField("m.message".into()))
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Serialize for MessageContent {
 | 
				
			||||||
 | 
					    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
 | 
				
			||||||
 | 
					    where
 | 
				
			||||||
 | 
					        S: serde::Serializer,
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        let mut st = serializer.serialize_struct("MessageContent", 1)?;
 | 
				
			||||||
 | 
					        let variants = self.variants();
 | 
				
			||||||
 | 
					        if variants.len() == 1 && variants[0].mimetype == "text/plain" {
 | 
				
			||||||
 | 
					            st.serialize_field("org.matrix.msc1767.text", &variants[0].body)?;
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            st.serialize_field("org.matrix.msc1767.message", variants)?;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        st.end()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -9,7 +9,7 @@ use ruma_identifiers::{DeviceId, EventId};
 | 
				
			|||||||
use ruma_macros::EventContent;
 | 
					use ruma_macros::EventContent;
 | 
				
			||||||
use serde::{Deserialize, Serialize};
 | 
					use serde::{Deserialize, Serialize};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::room::message::InReplyTo;
 | 
					use crate::room::message::{self, InReplyTo};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
mod relation_serde;
 | 
					mod relation_serde;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -107,6 +107,19 @@ pub enum Relation {
 | 
				
			|||||||
    _Custom,
 | 
					    _Custom,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl From<message::Relation> for Relation {
 | 
				
			||||||
 | 
					    fn from(rel: message::Relation) -> Self {
 | 
				
			||||||
 | 
					        match rel {
 | 
				
			||||||
 | 
					            message::Relation::Reply { in_reply_to } => Self::Reply { in_reply_to },
 | 
				
			||||||
 | 
					            #[cfg(feature = "unstable-msc2676")]
 | 
				
			||||||
 | 
					            message::Relation::Replacement(re) => {
 | 
				
			||||||
 | 
					                Self::Replacement(Replacement { event_id: re.event_id })
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            message::Relation::_Custom => Self::_Custom,
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// The event this relation belongs to replaces another event.
 | 
					/// The event this relation belongs to replaces another event.
 | 
				
			||||||
///
 | 
					///
 | 
				
			||||||
/// In contrast to [`message::Replacement`](crate::room::message::Replacement), this struct doesn't
 | 
					/// In contrast to [`message::Replacement`](crate::room::message::Replacement), this struct doesn't
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										221
									
								
								crates/ruma-events/tests/message.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										221
									
								
								crates/ruma-events/tests/message.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,221 @@
 | 
				
			|||||||
 | 
					#![cfg(feature = "unstable-msc1767")]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use assign::assign;
 | 
				
			||||||
 | 
					use js_int::uint;
 | 
				
			||||||
 | 
					use matches::assert_matches;
 | 
				
			||||||
 | 
					use ruma_common::MilliSecondsSinceUnixEpoch;
 | 
				
			||||||
 | 
					use ruma_events::{
 | 
				
			||||||
 | 
					    message::MessageEventContent,
 | 
				
			||||||
 | 
					    room::message::{InReplyTo, Relation},
 | 
				
			||||||
 | 
					    AnyMessageLikeEvent, MessageLikeEvent, Unsigned,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					use ruma_identifiers::{event_id, room_id, user_id};
 | 
				
			||||||
 | 
					use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[test]
 | 
				
			||||||
 | 
					fn html_content_serialization() {
 | 
				
			||||||
 | 
					    let message_event_content =
 | 
				
			||||||
 | 
					        MessageEventContent::html("Hello, World!", "Hello, <em>World</em>!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert_eq!(
 | 
				
			||||||
 | 
					        to_json_value(&message_event_content).unwrap(),
 | 
				
			||||||
 | 
					        json!({
 | 
				
			||||||
 | 
					            "org.matrix.msc1767.message": [
 | 
				
			||||||
 | 
					                { "body": "Hello, <em>World</em>!", "mimetype": "text/html"},
 | 
				
			||||||
 | 
					                { "body": "Hello, World!", "mimetype": "text/plain"},
 | 
				
			||||||
 | 
					            ]
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[test]
 | 
				
			||||||
 | 
					fn plain_text_content_serialization() {
 | 
				
			||||||
 | 
					    let message_event_content =
 | 
				
			||||||
 | 
					        MessageEventContent::plain("> <@test:example.com> test\n\ntest reply");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert_eq!(
 | 
				
			||||||
 | 
					        to_json_value(&message_event_content).unwrap(),
 | 
				
			||||||
 | 
					        json!({
 | 
				
			||||||
 | 
					            "org.matrix.msc1767.text": "> <@test:example.com> test\n\ntest reply",
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[test]
 | 
				
			||||||
 | 
					#[cfg(feature = "markdown")]
 | 
				
			||||||
 | 
					fn markdown_content_serialization() {
 | 
				
			||||||
 | 
					    let formatted_message = MessageEventContent::markdown("Testing **bold** and _italic_!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert_eq!(
 | 
				
			||||||
 | 
					        to_json_value(&formatted_message).unwrap(),
 | 
				
			||||||
 | 
					        json!({
 | 
				
			||||||
 | 
					            "org.matrix.msc1767.message": [
 | 
				
			||||||
 | 
					                { "body": "<p>Testing <strong>bold</strong> and <em>italic</em>!</p>\n", "mimetype": "text/html"},
 | 
				
			||||||
 | 
					                { "body": "Testing **bold** and _italic_!", "mimetype": "text/plain"},
 | 
				
			||||||
 | 
					            ]
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let plain_message_simple = MessageEventContent::markdown("Testing a simple phrase…");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert_eq!(
 | 
				
			||||||
 | 
					        to_json_value(&plain_message_simple).unwrap(),
 | 
				
			||||||
 | 
					        json!({
 | 
				
			||||||
 | 
					            "org.matrix.msc1767.text": "Testing a simple phrase…",
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let plain_message_paragraphs =
 | 
				
			||||||
 | 
					        MessageEventContent::markdown("Testing\n\nSeveral\n\nParagraphs.");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert_eq!(
 | 
				
			||||||
 | 
					        to_json_value(&plain_message_paragraphs).unwrap(),
 | 
				
			||||||
 | 
					        json!({
 | 
				
			||||||
 | 
					            "org.matrix.msc1767.message": [
 | 
				
			||||||
 | 
					                { "body": "<p>Testing</p>\n<p>Several</p>\n<p>Paragraphs.</p>\n", "mimetype": "text/html"},
 | 
				
			||||||
 | 
					                { "body": "Testing\n\nSeveral\n\nParagraphs.", "mimetype": "text/plain"},
 | 
				
			||||||
 | 
					            ]
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[test]
 | 
				
			||||||
 | 
					fn relates_to_content_serialization() {
 | 
				
			||||||
 | 
					    let message_event_content =
 | 
				
			||||||
 | 
					        assign!(MessageEventContent::plain("> <@test:example.com> test\n\ntest reply"), {
 | 
				
			||||||
 | 
					            relates_to: Some(Relation::Reply {
 | 
				
			||||||
 | 
					                in_reply_to: InReplyTo::new(
 | 
				
			||||||
 | 
					                    event_id!("$15827405538098VGFWH:example.com").to_owned(),
 | 
				
			||||||
 | 
					                ),
 | 
				
			||||||
 | 
					            }),
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let json_data = json!({
 | 
				
			||||||
 | 
					        "org.matrix.msc1767.text": "> <@test:example.com> test\n\ntest reply",
 | 
				
			||||||
 | 
					        "m.relates_to": {
 | 
				
			||||||
 | 
					            "m.in_reply_to": {
 | 
				
			||||||
 | 
					                "event_id": "$15827405538098VGFWH:example.com"
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert_eq!(to_json_value(&message_event_content).unwrap(), json_data);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[test]
 | 
				
			||||||
 | 
					fn message_event_serialization() {
 | 
				
			||||||
 | 
					    let event = MessageLikeEvent {
 | 
				
			||||||
 | 
					        content: MessageEventContent::plain("Hello, World!"),
 | 
				
			||||||
 | 
					        event_id: event_id!("$event:notareal.hs").to_owned(),
 | 
				
			||||||
 | 
					        sender: user_id!("@user:notareal.hs").to_owned(),
 | 
				
			||||||
 | 
					        origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(134_829_848)),
 | 
				
			||||||
 | 
					        room_id: room_id!("!roomid:notareal.hs").to_owned(),
 | 
				
			||||||
 | 
					        unsigned: Unsigned::default(),
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert_eq!(
 | 
				
			||||||
 | 
					        to_json_value(&event).unwrap(),
 | 
				
			||||||
 | 
					        json!({
 | 
				
			||||||
 | 
					            "content": {
 | 
				
			||||||
 | 
					                "org.matrix.msc1767.text": "Hello, World!",
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            "event_id": "$event:notareal.hs",
 | 
				
			||||||
 | 
					            "origin_server_ts": 134_829_848,
 | 
				
			||||||
 | 
					            "room_id": "!roomid:notareal.hs",
 | 
				
			||||||
 | 
					            "sender": "@user:notareal.hs",
 | 
				
			||||||
 | 
					            "type": "m.message",
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[test]
 | 
				
			||||||
 | 
					fn plain_text_content_deserialization() {
 | 
				
			||||||
 | 
					    let json_data = json!({
 | 
				
			||||||
 | 
					        "org.matrix.msc1767.text": "This is my body",
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert_matches!(
 | 
				
			||||||
 | 
					        from_json_value::<MessageEventContent>(json_data)
 | 
				
			||||||
 | 
					            .unwrap(),
 | 
				
			||||||
 | 
					        MessageEventContent { message, .. }
 | 
				
			||||||
 | 
					        if message.find_plain().unwrap() == "This is my body"
 | 
				
			||||||
 | 
					            && message.find_html().is_none()
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[test]
 | 
				
			||||||
 | 
					fn html_text_content_deserialization() {
 | 
				
			||||||
 | 
					    let json_data = json!({
 | 
				
			||||||
 | 
					        "org.matrix.msc1767.message": [
 | 
				
			||||||
 | 
					            { "body": "Hello, <em>New World</em>!", "mimetype": "text/html"},
 | 
				
			||||||
 | 
					            { "body": "Hello, New World!" },
 | 
				
			||||||
 | 
					        ]
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert_matches!(
 | 
				
			||||||
 | 
					        from_json_value::<MessageEventContent>(json_data)
 | 
				
			||||||
 | 
					            .unwrap(),
 | 
				
			||||||
 | 
					        MessageEventContent { message, .. }
 | 
				
			||||||
 | 
					        if message.find_plain().unwrap() == "Hello, New World!"
 | 
				
			||||||
 | 
					            && message.find_html().unwrap() == "Hello, <em>New World</em>!"
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[test]
 | 
				
			||||||
 | 
					fn relates_to_content_deserialization() {
 | 
				
			||||||
 | 
					    let json_data = json!({
 | 
				
			||||||
 | 
					        "org.matrix.msc1767.text": "> <@test:example.com> test\n\ntest reply",
 | 
				
			||||||
 | 
					        "m.relates_to": {
 | 
				
			||||||
 | 
					            "m.in_reply_to": {
 | 
				
			||||||
 | 
					                "event_id": "$15827405538098VGFWH:example.com"
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert_matches!(
 | 
				
			||||||
 | 
					        from_json_value::<MessageEventContent>(json_data)
 | 
				
			||||||
 | 
					            .unwrap(),
 | 
				
			||||||
 | 
					        MessageEventContent {
 | 
				
			||||||
 | 
					            message,
 | 
				
			||||||
 | 
					            relates_to: Some(Relation::Reply { in_reply_to: InReplyTo { event_id, .. } }),
 | 
				
			||||||
 | 
					            ..
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if message.find_plain().unwrap() == "> <@test:example.com> test\n\ntest reply"
 | 
				
			||||||
 | 
					            && message.find_html().is_none()
 | 
				
			||||||
 | 
					            && event_id == event_id!("$15827405538098VGFWH:example.com")
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[test]
 | 
				
			||||||
 | 
					fn message_event_deserialization() {
 | 
				
			||||||
 | 
					    let json_data = json!({
 | 
				
			||||||
 | 
					        "content": {
 | 
				
			||||||
 | 
					            "org.matrix.msc1767.text": "Hello, World!",
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        "event_id": "$event:notareal.hs",
 | 
				
			||||||
 | 
					        "origin_server_ts": 134_829_848,
 | 
				
			||||||
 | 
					        "room_id": "!roomid:notareal.hs",
 | 
				
			||||||
 | 
					        "sender": "@user:notareal.hs",
 | 
				
			||||||
 | 
					        "type": "m.message",
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert_matches!(
 | 
				
			||||||
 | 
					        from_json_value::<AnyMessageLikeEvent>(json_data).unwrap(),
 | 
				
			||||||
 | 
					        AnyMessageLikeEvent::Message(MessageLikeEvent {
 | 
				
			||||||
 | 
					            content: MessageEventContent {
 | 
				
			||||||
 | 
					                message,
 | 
				
			||||||
 | 
					                ..
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            event_id,
 | 
				
			||||||
 | 
					            origin_server_ts,
 | 
				
			||||||
 | 
					            room_id,
 | 
				
			||||||
 | 
					            sender,
 | 
				
			||||||
 | 
					            unsigned
 | 
				
			||||||
 | 
					        }) if event_id == event_id!("$event:notareal.hs")
 | 
				
			||||||
 | 
					            && message.find_plain().unwrap() == "Hello, World!"
 | 
				
			||||||
 | 
					            && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(134_829_848))
 | 
				
			||||||
 | 
					            && room_id == room_id!("!roomid:notareal.hs")
 | 
				
			||||||
 | 
					            && sender == user_id!("@user:notareal.hs")
 | 
				
			||||||
 | 
					            && unsigned.is_empty()
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -112,6 +112,7 @@ unstable-pre-spec = [
 | 
				
			|||||||
    "ruma-events/unstable-pre-spec",
 | 
					    "ruma-events/unstable-pre-spec",
 | 
				
			||||||
    "ruma-federation-api/unstable-pre-spec",
 | 
					    "ruma-federation-api/unstable-pre-spec",
 | 
				
			||||||
]
 | 
					]
 | 
				
			||||||
 | 
					unstable-msc1767 = ["ruma-events/unstable-msc1767"]
 | 
				
			||||||
unstable-msc2448 = [
 | 
					unstable-msc2448 = [
 | 
				
			||||||
    "ruma-client-api/unstable-msc2448",
 | 
					    "ruma-client-api/unstable-msc2448",
 | 
				
			||||||
    "ruma-events/unstable-msc2448",
 | 
					    "ruma-events/unstable-msc2448",
 | 
				
			||||||
@ -129,6 +130,7 @@ unstable-msc3618 = ["ruma-federation-api/unstable-msc3618"]
 | 
				
			|||||||
__ci = [
 | 
					__ci = [
 | 
				
			||||||
    "full",
 | 
					    "full",
 | 
				
			||||||
    "unstable-pre-spec",
 | 
					    "unstable-pre-spec",
 | 
				
			||||||
 | 
					    "unstable-msc1767",
 | 
				
			||||||
    "unstable-msc2448",
 | 
					    "unstable-msc2448",
 | 
				
			||||||
    "unstable-msc2675",
 | 
					    "unstable-msc2675",
 | 
				
			||||||
    "unstable-msc2676",
 | 
					    "unstable-msc2676",
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user