| @@ -16,6 +16,8 @@ use matrix_sdk::ruma::events::room::message::{ |
| 16 | 16 | TextMessageEventContent, |
| 17 | 17 | }; |
| 18 | 18 | |
| 19 | use crate::config::MarkupFormat; |
| 20 | |
| 19 | 21 | #[derive(Clone, Debug, Default)] |
| 20 | 22 | enum SlashCommand { |
| 21 | 23 | /// Send an emote message. |
| @@ -102,6 +104,16 @@ impl SlashCommand { |
| 102 | 104 | } |
| 103 | 105 | } |
| 104 | 106 | |
| 107 | impl From<MarkupFormat> for SlashCommand { |
| 108 | fn from(markup: MarkupFormat) -> Self { |
| 109 | match markup { |
| 110 | MarkupFormat::Html => Self::Html, |
| 111 | MarkupFormat::Markdown => Self::Markdown, |
| 112 | MarkupFormat::Plaintext => Self::Plaintext, |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 105 | 117 | fn parse_slash_command_inner(input: &str) -> IResult<&str, SlashCommand> { |
| 106 | 118 | let (input, _) = space0(input)?; |
| 107 | 119 | let (input, slash) = alt(( |
| @@ -172,37 +184,35 @@ fn text_to_message_content(input: String) -> TextMessageEventContent { |
| 172 | 184 | } |
| 173 | 185 | } |
| 174 | 186 | |
| 175 | | pub fn text_to_message(input: String) -> RoomMessageEventContent { |
| 176 | | let msg = parse_slash_command(input.as_str()) |
| 177 | | .and_then(|(input, slash)| slash.to_message(input)) |
| 187 | pub fn text_to_message(input: String, default_markup: MarkupFormat) -> RoomMessageEventContent { |
| 188 | let (rest, slash) = parse_slash_command(input.as_str()) |
| 189 | .unwrap_or_else(|_| (&input, SlashCommand::from(default_markup))); |
| 190 | let msg = slash |
| 191 | .to_message(rest) |
| 178 | 192 | .unwrap_or_else(|_| MessageType::Text(text_to_message_content(input))); |
| 179 | 193 | |
| 180 | 194 | RoomMessageEventContent::new(msg) |
| 181 | 195 | } |
| 182 | 196 | |
| 183 | 197 | /// Returns `None` if `input` contains a non-text slash command. |
| 184 | | pub fn text_to_text_message_event_content(input: String) -> Option<TextMessageEventContent> { |
| 185 | | let cmd = parse_slash_command(&input); |
| 198 | pub fn text_to_text_message_event_content( |
| 199 | input: String, |
| 200 | default_markup: MarkupFormat, |
| 201 | ) -> Option<TextMessageEventContent> { |
| 202 | let (body, cmd) = parse_slash_command(&input) |
| 203 | .unwrap_or_else(|_| (&input, SlashCommand::from(default_markup))); |
| 186 | 204 | |
| 187 | 205 | let content = match cmd { |
| 188 | | Ok((body, SlashCommand::Html)) => TextMessageEventContent::html(body, body), |
| 189 | | Ok((body, SlashCommand::Plaintext)) => TextMessageEventContent::plain(body), |
| 190 | | Ok((body, SlashCommand::Markdown)) => { |
| 206 | SlashCommand::Html => TextMessageEventContent::html(body, body), |
| 207 | SlashCommand::Plaintext => TextMessageEventContent::plain(body), |
| 208 | SlashCommand::Markdown => { |
| 191 | 209 | if let Some(html) = text_to_html(body) { |
| 192 | 210 | TextMessageEventContent::html(body, html) |
| 193 | 211 | } else { |
| 194 | 212 | TextMessageEventContent::plain(body) |
| 195 | 213 | } |
| 196 | 214 | }, |
| 197 | | Ok(_) => return None, |
| 198 | | |
| 199 | | _ => { |
| 200 | | if let Some(html) = text_to_html(&input) { |
| 201 | | TextMessageEventContent::html(input, html) |
| 202 | | } else { |
| 203 | | TextMessageEventContent::plain(input) |
| 204 | | } |
| 205 | | }, |
| 215 | _ => return None, |
| 206 | 216 | }; |
| 207 | 217 | |
| 208 | 218 | Some(content) |
| @@ -348,59 +358,103 @@ pub mod tests { |
| 348 | 358 | |
| 349 | 359 | #[test] |
| 350 | 360 | fn text_to_message_slash_commands() { |
| 351 | | let MessageType::Text(content) = text_to_message("/html <b>bold</b>".into()).msgtype else { |
| 361 | let MessageType::Text(content) = |
| 362 | text_to_message("/html <b>bold</b>".into(), Default::default()).msgtype |
| 363 | else { |
| 352 | 364 | panic!("Expected MessageType::Text"); |
| 353 | 365 | }; |
| 354 | 366 | assert_eq!(content.body, "<b>bold</b>"); |
| 355 | 367 | assert_eq!(content.formatted.unwrap().body, "<b>bold</b>"); |
| 356 | 368 | |
| 357 | | let MessageType::Text(content) = text_to_message("/h <b>bold</b>".into()).msgtype else { |
| 369 | let MessageType::Text(content) = |
| 370 | text_to_message("/h <b>bold</b>".into(), Default::default()).msgtype |
| 371 | else { |
| 358 | 372 | panic!("Expected MessageType::Text"); |
| 359 | 373 | }; |
| 360 | 374 | assert_eq!(content.body, "<b>bold</b>"); |
| 361 | 375 | assert_eq!(content.formatted.unwrap().body, "<b>bold</b>"); |
| 362 | 376 | |
| 363 | | let MessageType::Text(content) = text_to_message("/plain <b>bold</b>".into()).msgtype |
| 377 | let MessageType::Text(content) = |
| 378 | text_to_message("/plain <b>bold</b>".into(), Default::default()).msgtype |
| 364 | 379 | else { |
| 365 | 380 | panic!("Expected MessageType::Text"); |
| 366 | 381 | }; |
| 367 | 382 | assert_eq!(content.body, "<b>bold</b>"); |
| 368 | 383 | assert!(content.formatted.is_none(), "{:?}", content.formatted); |
| 369 | 384 | |
| 370 | | let MessageType::Text(content) = text_to_message("/p <b>bold</b>".into()).msgtype else { |
| 385 | let MessageType::Text(content) = |
| 386 | text_to_message("/p <b>bold</b>".into(), Default::default()).msgtype |
| 387 | else { |
| 371 | 388 | panic!("Expected MessageType::Text"); |
| 372 | 389 | }; |
| 373 | 390 | assert_eq!(content.body, "<b>bold</b>"); |
| 374 | 391 | assert!(content.formatted.is_none(), "{:?}", content.formatted); |
| 375 | 392 | |
| 376 | | let MessageType::Emote(content) = text_to_message("/me *bold*".into()).msgtype else { |
| 393 | let MessageType::Emote(content) = |
| 394 | text_to_message("/me *bold*".into(), Default::default()).msgtype |
| 395 | else { |
| 377 | 396 | panic!("Expected MessageType::Emote"); |
| 378 | 397 | }; |
| 379 | 398 | assert_eq!(content.body, "*bold*"); |
| 380 | 399 | assert_eq!(content.formatted.unwrap().body, "<p><em>bold</em></p>\n"); |
| 381 | 400 | |
| 382 | | let content = text_to_message("/confetti hello".into()).msgtype; |
| 401 | let content = text_to_message("/confetti hello".into(), Default::default()).msgtype; |
| 383 | 402 | assert_eq!(content.msgtype(), "nic.custom.confetti"); |
| 384 | 403 | assert_eq!(content.body(), "hello"); |
| 385 | 404 | |
| 386 | | let content = text_to_message("/fireworks hello".into()).msgtype; |
| 405 | let content = text_to_message("/fireworks hello".into(), Default::default()).msgtype; |
| 387 | 406 | assert_eq!(content.msgtype(), "nic.custom.fireworks"); |
| 388 | 407 | assert_eq!(content.body(), "hello"); |
| 389 | 408 | |
| 390 | | let content = text_to_message("/hearts hello".into()).msgtype; |
| 409 | let content = text_to_message("/hearts hello".into(), Default::default()).msgtype; |
| 391 | 410 | assert_eq!(content.msgtype(), "io.element.effect.hearts"); |
| 392 | 411 | assert_eq!(content.body(), "hello"); |
| 393 | 412 | |
| 394 | | let content = text_to_message("/rainfall hello".into()).msgtype; |
| 413 | let content = text_to_message("/rainfall hello".into(), Default::default()).msgtype; |
| 395 | 414 | assert_eq!(content.msgtype(), "io.element.effect.rainfall"); |
| 396 | 415 | assert_eq!(content.body(), "hello"); |
| 397 | 416 | |
| 398 | | let content = text_to_message("/snowfall hello".into()).msgtype; |
| 417 | let content = text_to_message("/snowfall hello".into(), Default::default()).msgtype; |
| 399 | 418 | assert_eq!(content.msgtype(), "io.element.effect.snowfall"); |
| 400 | 419 | assert_eq!(content.body(), "hello"); |
| 401 | 420 | |
| 402 | | let content = text_to_message("/spaceinvaders hello".into()).msgtype; |
| 421 | let content = text_to_message("/spaceinvaders hello".into(), Default::default()).msgtype; |
| 403 | 422 | assert_eq!(content.msgtype(), "io.element.effects.space_invaders"); |
| 404 | 423 | assert_eq!(content.body(), "hello"); |
| 405 | 424 | } |
| 425 | |
| 426 | #[test] |
| 427 | fn text_to_message_slash_default() { |
| 428 | let MessageType::Text(content) = |
| 429 | text_to_message("*hello*".into(), MarkupFormat::Html).msgtype |
| 430 | else { |
| 431 | panic!("Expected MessageType::Text"); |
| 432 | }; |
| 433 | assert_eq!(content.body, "*hello*"); |
| 434 | assert_eq!(content.formatted.unwrap().body, "*hello*"); |
| 435 | |
| 436 | let MessageType::Text(content) = |
| 437 | text_to_message("/markdown *hello*".into(), MarkupFormat::Html).msgtype |
| 438 | else { |
| 439 | panic!("Expected MessageType::Text"); |
| 440 | }; |
| 441 | assert_eq!(content.body, "*hello*"); |
| 442 | assert_eq!(content.formatted.unwrap().body, "<p><em>hello</em></p>\n"); |
| 443 | |
| 444 | let MessageType::Text(content) = |
| 445 | text_to_message("<b>hello</b>".into(), MarkupFormat::Plaintext).msgtype |
| 446 | else { |
| 447 | panic!("Expected MessageType::Text"); |
| 448 | }; |
| 449 | assert_eq!(content.body, "<b>hello</b>"); |
| 450 | assert!(content.formatted.is_none(), "{:?}", content.formatted); |
| 451 | |
| 452 | let MessageType::Text(content) = |
| 453 | text_to_message("/html <b>hello</b>".into(), MarkupFormat::Plaintext).msgtype |
| 454 | else { |
| 455 | panic!("Expected MessageType::Text"); |
| 456 | }; |
| 457 | assert_eq!(content.body, "<b>hello</b>"); |
| 458 | assert_eq!(content.formatted.unwrap().body, "<b>hello</b>"); |
| 459 | } |
| 406 | 460 | } |