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;
/// Text message content.
#[derive(Clone, Debug, Serialize, Deserialize)]
/// The payload for an extensible text message.
#[derive(Clone, Debug, Serialize, Deserialize, EventContent)]
#[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,
#[ruma_event(type = "m.message", kind = MessageLike)]
pub struct MessageEventContent {
/// The message's text content.
#[serde(flatten)]
pub message: MessageContent,
/// The text content.
pub body: String,
/// 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 Text {
/// Creates a new plain text message body.
impl MessageEventContent {
/// A convenience constructor to create a plain text message.
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.
pub fn html(body: impl Into<String>) -> Self {
Self { mimetype: "text/html".to_owned(), body: body.into() }
/// 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 }
}
/// 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")]
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()
pub fn markdown(body: impl AsRef<str> + Into<String>) -> Self {
Self { message: MessageContent::markdown(body), relates_to: None }
}
}
@ -104,39 +100,43 @@ impl MessageContent {
}
}
/// The payload for an extensible text message.
#[derive(Clone, Debug, Serialize, Deserialize, EventContent)]
/// Text message content.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[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,
pub struct Text {
/// The mime type of the `body`.
#[serde(default = "Text::default_mimetype")]
pub mimetype: String,
/// 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>,
/// The text content.
pub body: String,
}
impl MessageEventContent {
/// A convenience constructor to create a plain text message.
impl Text {
/// Creates a new plain text message body.
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.
pub fn html(body: impl Into<String>, html_body: impl Into<String>) -> Self {
Self { message: MessageContent::html(body, html_body), relates_to: None }
/// Creates a new HTML-formatted message body.
pub fn html(body: impl Into<String>) -> Self {
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
/// text message.
/// Returns `None` if no Markdown formatting was found.
#[cfg(feature = "markdown")]
pub fn markdown(body: impl AsRef<str> + Into<String>) -> Self {
Self { message: MessageContent::markdown(body), relates_to: None }
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()
}
}