From 1f53f38b4f2ce8f10a0d7ec62cf14e819d0f6421 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Wed, 16 Mar 2022 12:13:50 +0100 Subject: [PATCH] common: Add support for translatable text content. According to MSC3554 --- crates/ruma-common/Cargo.toml | 1 + crates/ruma-common/src/events/message.rs | 27 ++++++++++++-- crates/ruma-common/tests/events/message.rs | 42 ++++++++++++++++++++++ crates/ruma/Cargo.toml | 2 ++ 4 files changed, 70 insertions(+), 2 deletions(-) diff --git a/crates/ruma-common/Cargo.toml b/crates/ruma-common/Cargo.toml index 65987ce5..2617e3a6 100644 --- a/crates/ruma-common/Cargo.toml +++ b/crates/ruma-common/Cargo.toml @@ -39,6 +39,7 @@ unstable-msc3488 = ["unstable-msc1767"] unstable-msc3551 = ["unstable-msc1767"] unstable-msc3552 = ["unstable-msc1767", "unstable-msc3551"] unstable-msc3553 = ["unstable-msc3552"] +unstable-msc3554 = ["unstable-msc1767"] [dependencies] base64 = "0.13.0" diff --git a/crates/ruma-common/src/events/message.rs b/crates/ruma-common/src/events/message.rs index 36dbdd03..84572223 100644 --- a/crates/ruma-common/src/events/message.rs +++ b/crates/ruma-common/src/events/message.rs @@ -100,6 +100,12 @@ impl MessageContent { } } +impl From> for MessageContent { + fn from(variants: Vec) -> Self { + Self(variants) + } +} + /// Text message content. #[derive(Clone, Debug, Serialize, Deserialize)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] @@ -110,17 +116,34 @@ pub struct Text { /// The text content. pub body: String, + + /// The language of the text. + /// + /// This must be a valid language code according to [BCP 47](https://www.rfc-editor.org/rfc/bcp/bcp47.txt). + #[cfg(feature = "unstable-msc3554")] + #[serde(skip_serializing_if = "Option::is_none")] + pub lang: Option, } impl Text { /// Creates a new plain text message body. pub fn plain(body: impl Into) -> Self { - Self { mimetype: "text/plain".to_owned(), body: body.into() } + Self { + mimetype: "text/plain".to_owned(), + body: body.into(), + #[cfg(feature = "unstable-msc3554")] + lang: None, + } } /// Creates a new HTML-formatted message body. pub fn html(body: impl Into) -> Self { - Self { mimetype: "text/html".to_owned(), body: body.into() } + Self { + mimetype: "text/html".to_owned(), + body: body.into(), + #[cfg(feature = "unstable-msc3554")] + lang: None, + } } /// Creates a new HTML-formatted message body by parsing the Markdown in `body`. diff --git a/crates/ruma-common/tests/events/message.rs b/crates/ruma-common/tests/events/message.rs index e6c95f31..484b8095 100644 --- a/crates/ruma-common/tests/events/message.rs +++ b/crates/ruma-common/tests/events/message.rs @@ -354,3 +354,45 @@ fn emote_event_deserialization() { && unsigned.is_empty() ); } + +#[test] +#[cfg(feature = "unstable-msc3554")] +fn lang_serialization() { + use ruma_common::events::message::{MessageContent, Text}; + + let content = MessageContent::from(vec![ + assign!(Text::plain("Bonjour le monde !"), { lang: Some("fr".into()) }), + assign!(Text::plain("Hallo Welt!"), { lang: Some("de".into()) }), + assign!(Text::plain("Hello World!"), { lang: Some("en".into()) }), + ]); + + assert_eq!( + to_json_value(&content).unwrap(), + json!({ + "org.matrix.msc1767.message": [ + { "body": "Bonjour le monde !", "mimetype": "text/plain", "lang": "fr"}, + { "body": "Hallo Welt!", "mimetype": "text/plain", "lang": "de"}, + { "body": "Hello World!", "mimetype": "text/plain", "lang": "en"}, + ] + }) + ); +} + +#[test] +#[cfg(feature = "unstable-msc3554")] +fn lang_deserialization() { + use ruma_common::events::message::MessageContent; + + let json_data = json!({ + "org.matrix.msc1767.message": [ + { "body": "Bonjour le monde !", "mimetype": "text/plain", "lang": "fr"}, + { "body": "Hallo Welt!", "mimetype": "text/plain", "lang": "de"}, + { "body": "Hello World!", "mimetype": "text/plain", "lang": "en"}, + ] + }); + + let content = from_json_value::(json_data).unwrap(); + assert_eq!(content.variants()[0].lang.as_deref(), Some("fr")); + assert_eq!(content.variants()[1].lang.as_deref(), Some("de")); + assert_eq!(content.variants()[2].lang.as_deref(), Some("en")); +} diff --git a/crates/ruma/Cargo.toml b/crates/ruma/Cargo.toml index 1f53acc2..09d1df06 100644 --- a/crates/ruma/Cargo.toml +++ b/crates/ruma/Cargo.toml @@ -129,6 +129,7 @@ unstable-msc3488 = [ unstable-msc3551 = ["ruma-common/unstable-msc3551"] unstable-msc3552 = ["ruma-common/unstable-msc3552"] unstable-msc3553 = ["ruma-common/unstable-msc3553"] +unstable-msc3554 = ["ruma-common/unstable-msc3554"] unstable-msc3618 = ["ruma-federation-api/unstable-msc3618"] unstable-msc3723 = ["ruma-federation-api/unstable-msc3723"] @@ -147,6 +148,7 @@ __ci = [ "unstable-msc3551", "unstable-msc3552", "unstable-msc3553", + "unstable-msc3554", "unstable-msc3618", "unstable-msc3723", "ruma-state-res/__ci",