common: Reorder events::message types

This commit is contained in:
Kévin Commaille 2022-03-16 11:34:39 +01:00 committed by Kévin Commaille
parent 44f8192e3c
commit af58063c7b

View File

@ -11,44 +11,40 @@ use content_serde::MessageContentSerDeHelper;
use super::room::message::Relation; use super::room::message::Relation;
/// Text message content. /// The payload for an extensible text message.
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Text { #[ruma_event(type = "m.message", kind = MessageLike)]
/// The mime type of the `body`. pub struct MessageEventContent {
#[serde(default = "Text::default_mimetype")] /// The message's text content.
pub mimetype: String, #[serde(flatten)]
pub message: MessageContent,
/// The text content. /// Information about related messages for [rich replies].
pub body: String, ///
/// [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 Text { impl MessageEventContent {
/// Creates a new plain text message body. /// A convenience constructor to create a plain text message.
pub fn plain(body: impl Into<String>) -> Self { pub fn plain(body: impl Into<String>) -> Self {
Self { mimetype: "text/plain".to_owned(), body: body.into() } Self { message: MessageContent::plain(body), relates_to: None }
} }
/// Creates a new HTML-formatted message body. /// A convenience constructor to create an HTML message.
pub fn html(body: impl Into<String>) -> Self { pub fn html(body: impl Into<String>, html_body: impl Into<String>) -> Self {
Self { mimetype: "text/html".to_owned(), body: body.into() } Self { message: MessageContent::html(body, html_body), relates_to: None }
} }
/// Creates a new HTML-formatted message body by parsing the Markdown in `body`. /// A convenience constructor to create a Markdown message.
/// ///
/// Returns `None` if no Markdown formatting was found. /// Returns an HTML message if some Markdown formatting was detected, otherwise returns a plain
/// text message.
#[cfg(feature = "markdown")] #[cfg(feature = "markdown")]
pub fn markdown(body: impl AsRef<str>) -> Option<Self> { pub fn markdown(body: impl AsRef<str> + Into<String>) -> Self {
let body = body.as_ref(); Self { message: MessageContent::markdown(body), relates_to: None }
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()
} }
} }
@ -104,39 +100,43 @@ impl MessageContent {
} }
} }
/// The payload for an extensible text message. /// Text message content.
#[derive(Clone, Debug, Serialize, Deserialize, EventContent)] #[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.message", kind = MessageLike)] pub struct Text {
pub struct MessageEventContent { /// The mime type of the `body`.
/// The message's text content. #[serde(default = "Text::default_mimetype")]
#[serde(flatten)] pub mimetype: String,
pub message: MessageContent,
/// Information about related messages for [rich replies]. /// The text content.
/// pub body: String,
/// [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 { impl Text {
/// A convenience constructor to create a plain text message. /// Creates a new plain text message body.
pub fn plain(body: impl Into<String>) -> Self { pub fn plain(body: impl Into<String>) -> Self {
Self { message: MessageContent::plain(body), relates_to: None } Self { mimetype: "text/plain".to_owned(), body: body.into() }
} }
/// A convenience constructor to create an HTML message. /// Creates a new HTML-formatted message body.
pub fn html(body: impl Into<String>, html_body: impl Into<String>) -> Self { pub fn html(body: impl Into<String>) -> Self {
Self { message: MessageContent::html(body, html_body), relates_to: None } Self { mimetype: "text/html".to_owned(), body: body.into() }
} }
/// A convenience constructor to create a Markdown message. /// Creates a new HTML-formatted message body by parsing the Markdown in `body`.
/// ///
/// Returns an HTML message if some Markdown formatting was detected, otherwise returns a plain /// Returns `None` if no Markdown formatting was found.
/// text message.
#[cfg(feature = "markdown")] #[cfg(feature = "markdown")]
pub fn markdown(body: impl AsRef<str> + Into<String>) -> Self { pub fn markdown(body: impl AsRef<str>) -> Option<Self> {
Self { message: MessageContent::markdown(body), relates_to: None } 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()
} }
} }