ruma_events/room/message/
emote.rs

1use serde::{Deserialize, Serialize};
2
3use super::FormattedBody;
4
5/// The payload for an emote message.
6#[derive(Clone, Debug, Deserialize, Serialize)]
7#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
8#[serde(tag = "msgtype", rename = "m.emote")]
9pub struct EmoteMessageEventContent {
10    /// The emote action to perform.
11    pub body: String,
12
13    /// Formatted form of the message `body`.
14    #[serde(flatten)]
15    pub formatted: Option<FormattedBody>,
16}
17
18impl EmoteMessageEventContent {
19    /// A convenience constructor to create a plain-text emote.
20    pub fn plain(body: impl Into<String>) -> Self {
21        let body = body.into();
22        Self { body, formatted: None }
23    }
24
25    /// A convenience constructor to create an html emote message.
26    pub fn html(body: impl Into<String>, html_body: impl Into<String>) -> Self {
27        let body = body.into();
28        Self { body, formatted: Some(FormattedBody::html(html_body)) }
29    }
30
31    /// A convenience constructor to create a markdown emote.
32    ///
33    /// Returns an html emote message if some markdown formatting was detected, otherwise returns a
34    /// plain-text emote.
35    #[cfg(feature = "markdown")]
36    pub fn markdown(body: impl AsRef<str> + Into<String>) -> Self {
37        if let Some(formatted) = FormattedBody::markdown(&body) {
38            Self::html(body, formatted.body)
39        } else {
40            Self::plain(body)
41        }
42    }
43}