| @@ -16,6 +16,8 @@ use matrix_sdk::ruma::events::room::message::{ |
| 16 | TextMessageEventContent, |
16 | TextMessageEventContent, |
| 17 | }; |
17 | }; |
| 18 | |
18 | |
| |
19 | use crate::config::MarkupFormat; |
| |
20 | |
| 19 | #[derive(Clone, Debug, Default)] |
21 | #[derive(Clone, Debug, Default)] |
| 20 | enum SlashCommand { |
22 | enum SlashCommand { |
| 21 | /// Send an emote message. |
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 | fn parse_slash_command_inner(input: &str) -> IResult<&str, SlashCommand> { |
117 | fn parse_slash_command_inner(input: &str) -> IResult<&str, SlashCommand> { |
| 106 | let (input, _) = space0(input)?; |
118 | let (input, _) = space0(input)?; |
| 107 | let (input, slash) = alt(( |
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 { |
187 | pub fn text_to_message(input: String, default_markup: MarkupFormat) -> RoomMessageEventContent { |
| 176 | let msg = parse_slash_command(input.as_str()) |
188 | let (rest, slash) = parse_slash_command(input.as_str()) |
| 177 | .and_then(|(input, slash)| slash.to_message(input)) |
189 | .unwrap_or_else(|_| (&input, SlashCommand::from(default_markup))); |
| |
190 | let msg = slash |
| |
191 | .to_message(rest) |
| 178 | .unwrap_or_else(|_| MessageType::Text(text_to_message_content(input))); |
192 | .unwrap_or_else(|_| MessageType::Text(text_to_message_content(input))); |
| 179 | |
193 | |
| 180 | RoomMessageEventContent::new(msg) |
194 | RoomMessageEventContent::new(msg) |
| 181 | } |
195 | } |
| 182 | |
196 | |
| 183 | /// Returns `None` if `input` contains a non-text slash command. |
197 | /// Returns `None` if `input` contains a non-text slash command. |
| 184 | pub fn text_to_text_message_event_content(input: String) -> Option<TextMessageEventContent> { |
198 | pub fn text_to_text_message_event_content( |
| 185 | let cmd = parse_slash_command(&input); |
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 | let content = match cmd { |
205 | let content = match cmd { |
| 188 | Ok((body, SlashCommand::Html)) => TextMessageEventContent::html(body, body), |
206 | SlashCommand::Html => TextMessageEventContent::html(body, body), |
| 189 | Ok((body, SlashCommand::Plaintext)) => TextMessageEventContent::plain(body), |
207 | SlashCommand::Plaintext => TextMessageEventContent::plain(body), |
| 190 | Ok((body, SlashCommand::Markdown)) => { |
208 | SlashCommand::Markdown => { |
| 191 | if let Some(html) = text_to_html(body) { |
209 | if let Some(html) = text_to_html(body) { |
| 192 | TextMessageEventContent::html(body, html) |
210 | TextMessageEventContent::html(body, html) |
| 193 | } else { |
211 | } else { |
| 194 | TextMessageEventContent::plain(body) |
212 | TextMessageEventContent::plain(body) |
| 195 | } |
213 | } |
| 196 | }, |
214 | }, |
| 197 | Ok(_) => return None, |
215 | _ => 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 | }, |
| |
| 206 | }; |
216 | }; |
| 207 | |
217 | |
| 208 | Some(content) |
218 | Some(content) |
| @@ -348,59 +358,103 @@ pub mod tests { |
| 348 | |
358 | |
| 349 | #[test] |
359 | #[test] |
| 350 | fn text_to_message_slash_commands() { |
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 | panic!("Expected MessageType::Text"); |
364 | panic!("Expected MessageType::Text"); |
| 353 | }; |
365 | }; |
| 354 | assert_eq!(content.body, "<b>bold</b>"); |
366 | assert_eq!(content.body, "<b>bold</b>"); |
| 355 | assert_eq!(content.formatted.unwrap().body, "<b>bold</b>"); |
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 | panic!("Expected MessageType::Text"); |
372 | panic!("Expected MessageType::Text"); |
| 359 | }; |
373 | }; |
| 360 | assert_eq!(content.body, "<b>bold</b>"); |
374 | assert_eq!(content.body, "<b>bold</b>"); |
| 361 | assert_eq!(content.formatted.unwrap().body, "<b>bold</b>"); |
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 | else { |
379 | else { |
| 365 | panic!("Expected MessageType::Text"); |
380 | panic!("Expected MessageType::Text"); |
| 366 | }; |
381 | }; |
| 367 | assert_eq!(content.body, "<b>bold</b>"); |
382 | assert_eq!(content.body, "<b>bold</b>"); |
| 368 | assert!(content.formatted.is_none(), "{:?}", content.formatted); |
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 | panic!("Expected MessageType::Text"); |
388 | panic!("Expected MessageType::Text"); |
| 372 | }; |
389 | }; |
| 373 | assert_eq!(content.body, "<b>bold</b>"); |
390 | assert_eq!(content.body, "<b>bold</b>"); |
| 374 | assert!(content.formatted.is_none(), "{:?}", content.formatted); |
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 | panic!("Expected MessageType::Emote"); |
396 | panic!("Expected MessageType::Emote"); |
| 378 | }; |
397 | }; |
| 379 | assert_eq!(content.body, "*bold*"); |
398 | assert_eq!(content.body, "*bold*"); |
| 380 | assert_eq!(content.formatted.unwrap().body, "<p><em>bold</em></p>\n"); |
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 | assert_eq!(content.msgtype(), "nic.custom.confetti"); |
402 | assert_eq!(content.msgtype(), "nic.custom.confetti"); |
| 384 | assert_eq!(content.body(), "hello"); |
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 | assert_eq!(content.msgtype(), "nic.custom.fireworks"); |
406 | assert_eq!(content.msgtype(), "nic.custom.fireworks"); |
| 388 | assert_eq!(content.body(), "hello"); |
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 | assert_eq!(content.msgtype(), "io.element.effect.hearts"); |
410 | assert_eq!(content.msgtype(), "io.element.effect.hearts"); |
| 392 | assert_eq!(content.body(), "hello"); |
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 | assert_eq!(content.msgtype(), "io.element.effect.rainfall"); |
414 | assert_eq!(content.msgtype(), "io.element.effect.rainfall"); |
| 396 | assert_eq!(content.body(), "hello"); |
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 | assert_eq!(content.msgtype(), "io.element.effect.snowfall"); |
418 | assert_eq!(content.msgtype(), "io.element.effect.snowfall"); |
| 400 | assert_eq!(content.body(), "hello"); |
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 | assert_eq!(content.msgtype(), "io.element.effects.space_invaders"); |
422 | assert_eq!(content.msgtype(), "io.element.effects.space_invaders"); |
| 404 | assert_eq!(content.body(), "hello"); |
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 | } |