Skip to content

Commit

Permalink
refactor(sdk): move formatted_caption_from to the SDK, rename it
Browse files Browse the repository at this point in the history
Add the `markdown` feature to the SDK crate, otherwise we can't use `FormattedBody::markdown`.

Refactor the pattern matching into an if, add tests to check its behaviour.
  • Loading branch information
jmartinesp committed Nov 14, 2024
1 parent 6e75bdd commit f6c6fc1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 30 deletions.
35 changes: 11 additions & 24 deletions bindings/matrix-sdk-ffi/src/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ use ruma::{
},
receipt::ReceiptThread,
room::message::{
FormattedBody as RumaFormattedBody, ForwardThread, LocationMessageEventContent,
MessageType, RoomMessageEventContentWithoutRelation,
ForwardThread, LocationMessageEventContent, MessageType,
RoomMessageEventContentWithoutRelation,
},
AnyMessageLikeEventContent,
},
Expand Down Expand Up @@ -81,6 +81,7 @@ use crate::{
mod content;

pub use content::MessageContent;
use matrix_sdk::utils::formatted_body_from;

use crate::error::QueueWedgeError;

Expand Down Expand Up @@ -289,7 +290,8 @@ impl Timeline {
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Arc<SendAttachmentJoinHandle> {
let formatted_caption = formatted_caption_from(&caption, &formatted_caption);
let formatted_caption =
formatted_body_from(caption.as_deref(), formatted_caption.map(Into::into));
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let base_image_info = BaseImageInfo::try_from(&image_info)
.map_err(|_| RoomError::InvalidAttachmentData)?;
Expand Down Expand Up @@ -322,7 +324,8 @@ impl Timeline {
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Arc<SendAttachmentJoinHandle> {
let formatted_caption = formatted_caption_from(&caption, &formatted_caption);
let formatted_caption =
formatted_body_from(caption.as_deref(), formatted_caption.map(Into::into));
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let base_video_info: BaseVideoInfo = BaseVideoInfo::try_from(&video_info)
.map_err(|_| RoomError::InvalidAttachmentData)?;
Expand Down Expand Up @@ -353,7 +356,8 @@ impl Timeline {
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Arc<SendAttachmentJoinHandle> {
let formatted_caption = formatted_caption_from(&caption, &formatted_caption);
let formatted_caption =
formatted_body_from(caption.as_deref(), formatted_caption.map(Into::into));
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let base_audio_info: BaseAudioInfo = BaseAudioInfo::try_from(&audio_info)
.map_err(|_| RoomError::InvalidAttachmentData)?;
Expand Down Expand Up @@ -386,7 +390,8 @@ impl Timeline {
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Arc<SendAttachmentJoinHandle> {
let formatted_caption = formatted_caption_from(&caption, &formatted_caption);
let formatted_caption =
formatted_body_from(caption.as_deref(), formatted_caption.map(Into::into));
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let base_audio_info: BaseAudioInfo = BaseAudioInfo::try_from(&audio_info)
.map_err(|_| RoomError::InvalidAttachmentData)?;
Expand Down Expand Up @@ -714,24 +719,6 @@ impl Timeline {
}
}

/// Given a pair of optional `caption` and `formatted_caption` parameters,
/// return a formatted caption:
///
/// - If a `formatted_caption` exists, return it.
/// - If it doesn't exist but there is a `caption`, parse it as markdown and
/// return the result.
/// - Return `None` if there are no `caption` or `formatted_caption` parameters.
fn formatted_caption_from(
caption: &Option<String>,
formatted_caption: &Option<FormattedBody>,
) -> Option<RumaFormattedBody> {
match (&caption, formatted_caption) {
(None, None) => None,
(Some(body), None) => RumaFormattedBody::markdown(body),
(_, Some(formatted_body)) => Some(formatted_body.clone().into()),
}
}

/// A handle to perform actions onto a local echo.
#[derive(uniffi::Object)]
pub struct SendHandle {
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ features = ["docsrs"]
rustdoc-args = ["--cfg", "docsrs"]

[features]
default = ["e2e-encryption", "automatic-room-key-forwarding", "sqlite", "native-tls"]
default = ["e2e-encryption", "automatic-room-key-forwarding", "sqlite", "native-tls", "markdown"]
testing = ["matrix-sdk-sqlite?/testing", "matrix-sdk-indexeddb?/testing", "matrix-sdk-base/testing", "wiremock", "matrix-sdk-test", "assert_matches2"]

e2e-encryption = [
Expand Down
65 changes: 63 additions & 2 deletions crates/matrix-sdk/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use futures_core::Stream;
#[cfg(feature = "e2e-encryption")]
use futures_util::StreamExt;
use ruma::{
events::{AnyMessageLikeEventContent, AnyStateEventContent},
events::{room::message::FormattedBody, AnyMessageLikeEventContent, AnyStateEventContent},
serde::Raw,
RoomAliasId,
};
Expand Down Expand Up @@ -218,9 +218,28 @@ pub fn is_room_alias_format_valid(alias: String) -> bool {
has_valid_format && is_lowercase && RoomAliasId::parse(alias).is_ok()
}

/// Given a pair of optional `body` and `formatted_body` parameters,
/// returns a formatted body.
///
/// Return the formatted body if available, or interpret the `body` parameter as
/// markdown, if provided.
pub fn formatted_body_from(
body: Option<&str>,
formatted_body: Option<FormattedBody>,
) -> Option<FormattedBody> {
if formatted_body.is_some() {
formatted_body
} else {
body.and_then(FormattedBody::markdown)
}
}

#[cfg(test)]
mod test {
use crate::utils::is_room_alias_format_valid;
use assert_matches2::{assert_let, assert_matches};
use ruma::events::room::message::FormattedBody;

use crate::utils::{formatted_body_from, is_room_alias_format_valid};

#[cfg(feature = "e2e-encryption")]
#[test]
Expand Down Expand Up @@ -282,4 +301,46 @@ mod test {
fn test_is_room_alias_format_valid_when_has_valid_format() {
assert!(is_room_alias_format_valid("#alias.test:domain.org".to_owned()))
}

#[test]
fn test_formatted_body_from_nothing_returns_none() {
assert_matches!(formatted_body_from(None, None), None);
}

#[test]
fn test_formatted_body_from_only_formatted_body_returns_the_formatted_body() {
let formatted_body = FormattedBody::html(r"<h1>Hello!</h1>");

assert_let!(
Some(result_formatted_body) = formatted_body_from(None, Some(formatted_body.clone()))
);

assert_eq!(formatted_body.body, result_formatted_body.body);
assert_eq!(result_formatted_body.format, result_formatted_body.format);
}

#[test]
fn test_formatted_body_from_markdown_body_returns_a_processed_formatted_body() {
let markdown_body = Some(r"# Parsed");

assert_let!(Some(result_formatted_body) = formatted_body_from(markdown_body, None));

let expected_formatted_body = FormattedBody::html("<h1>Parsed</h1>\n".to_owned());
assert_eq!(expected_formatted_body.body, result_formatted_body.body);
assert_eq!(expected_formatted_body.format, result_formatted_body.format);
}

#[test]
fn test_formatted_body_from_body_and_formatted_body_returns_the_formatted_body() {
let markdown_body = Some(r"# Markdown");
let formatted_body = FormattedBody::html(r"<h1>HTML</h1>");

assert_let!(
Some(result_formatted_body) =
formatted_body_from(markdown_body, Some(formatted_body.clone()))
);

assert_eq!(formatted_body.body, result_formatted_body.body);
assert_eq!(formatted_body.format, result_formatted_body.format);
}
}
6 changes: 3 additions & 3 deletions xtask/src/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,17 +285,17 @@ fn run_wasm_checks(cmd: Option<WasmFeatureSet>) -> Result<()> {
(WasmFeatureSet::MatrixSdkQrcode, "-p matrix-sdk-qrcode --features js"),
(
WasmFeatureSet::MatrixSdkNoDefault,
"-p matrix-sdk --no-default-features --features js,rustls-tls",
"-p matrix-sdk --no-default-features --features js,rustls-tls,markdown",
),
(WasmFeatureSet::MatrixSdkBase, "-p matrix-sdk-base --features js,test-send-sync"),
(WasmFeatureSet::MatrixSdkCommon, "-p matrix-sdk-common --features js"),
(
WasmFeatureSet::MatrixSdkIndexeddbStoresNoCrypto,
"-p matrix-sdk --no-default-features --features js,indexeddb,rustls-tls",
"-p matrix-sdk --no-default-features --features js,indexeddb,rustls-tls,markdown",
),
(
WasmFeatureSet::MatrixSdkIndexeddbStores,
"-p matrix-sdk --no-default-features --features js,indexeddb,e2e-encryption,rustls-tls",
"-p matrix-sdk --no-default-features --features js,indexeddb,e2e-encryption,rustls-tls,markdown",
),
(WasmFeatureSet::IndexeddbAllFeatures, "-p matrix-sdk-indexeddb"),
(
Expand Down

0 comments on commit f6c6fc1

Please sign in to comment.