| 1 | use chrono::{DateTime, Local, Utc}; |
| 2 | use gpui_kit::*; |
| 3 | use store::MessageRecord; |
| 4 | |
| 5 | use crate::app_state::AppState; |
| 6 | use crate::notice::short_error; |
| 7 | use crate::rows::message_text; |
| 8 | use crate::runtime; |
| 9 | |
| 10 | pub const SHARE_TO_OUTLOOK_LABEL: &str = "Share to Outlook"; |
| 11 | |
| 12 | pub fn share_subject(record: &MessageRecord, conversation_title: &str) -> String { |
| 13 | record |
| 14 | .subject |
| 15 | .as_deref() |
| 16 | .map(str::trim) |
| 17 | .filter(|subject| !subject.is_empty()) |
| 18 | .unwrap_or(conversation_title) |
| 19 | .to_owned() |
| 20 | } |
| 21 | |
| 22 | pub fn share_body(record: &MessageRecord, link: Option<&str>) -> String { |
| 23 | let sender = record.sender_name.as_deref().unwrap_or_default(); |
| 24 | let header = format!("{sender} {}", sent_label(record.created_at)) |
| 25 | .trim() |
| 26 | .to_owned(); |
| 27 | let mut parts = vec![header, message_text(record)]; |
| 28 | parts.extend(link.map(str::to_owned)); |
| 29 | parts |
| 30 | .into_iter() |
| 31 | .filter(|part| !part.trim().is_empty()) |
| 32 | .collect::<Vec<_>>() |
| 33 | .join("\n\n") |
| 34 | } |
| 35 | |
| 36 | fn sent_label(sent_at: DateTime<Utc>) -> String { |
| 37 | let local = sent_at.with_timezone(&Local); |
| 38 | format!( |
| 39 | "{} {}", |
| 40 | crate::format::date_label(local.date_naive()), |
| 41 | local.format(crate::format::clock_format()) |
| 42 | ) |
| 43 | } |
| 44 | |
| 45 | impl AppState { |
| 46 | pub fn share_to_outlook( |
| 47 | &mut self, |
| 48 | conversation_id: &str, |
| 49 | message_id: &str, |
| 50 | conversation_title: &str, |
| 51 | cx: &mut Context<Self>, |
| 52 | ) { |
| 53 | let Some(record) = self.message_record(conversation_id, message_id) else { |
| 54 | return; |
| 55 | }; |
| 56 | let subject = share_subject(&record, conversation_title); |
| 57 | let Some(engine) = self.engine.clone() else { |
| 58 | if self.mode.demo { |
| 59 | let link = teams_core::chat_message_link(conversation_id, message_id, None); |
| 60 | let body = share_body(&record, Some(&link)); |
| 61 | app_base::open_link::open_url( |
| 62 | cx, |
| 63 | &teams_core::outlook_compose_link(&subject, &body), |
| 64 | ); |
| 65 | } |
| 66 | return; |
| 67 | }; |
| 68 | let (conversation_id, message_id) = (conversation_id.to_owned(), message_id.to_owned()); |
| 69 | let receiver = |
| 70 | runtime::spawn(async move { engine.message_link(&conversation_id, &message_id).await }); |
| 71 | cx.spawn(async move |this, cx| { |
| 72 | let link = match receiver.await { |
| 73 | Ok(Ok(link)) => Ok(link), |
| 74 | Ok(Err(error)) => Err(short_error(&error)), |
| 75 | Err(_) => Err("cancelled".to_owned()), |
| 76 | }; |
| 77 | this.update(cx, |state, cx| { |
| 78 | if let Err(reason) = &link { |
| 79 | state.raise_notice(format!("No link to the message: {reason}"), None, cx); |
| 80 | } |
| 81 | let body = share_body(&record, link.as_deref().ok()); |
| 82 | app_base::open_link::open_url( |
| 83 | cx, |
| 84 | &teams_core::outlook_compose_link(&subject, &body), |
| 85 | ); |
| 86 | }) |
| 87 | .ok(); |
| 88 | }) |
| 89 | .detach(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | #[cfg(test)] |
| 94 | mod tests { |
| 95 | use chrono::DateTime; |
| 96 | use store::MessageRecord; |
| 97 | |
| 98 | use super::{share_body, share_subject}; |
| 99 | |
| 100 | fn record(subject: Option<&str>, html: &str) -> MessageRecord { |
| 101 | MessageRecord { |
| 102 | conversation_id: "19:c@thread.v2".into(), |
| 103 | message_id: "1".into(), |
| 104 | reply_to_id: None, |
| 105 | sender_id: Some("u".into()), |
| 106 | sender_name: Some("Ada Example".into()), |
| 107 | sender_application_id: None, |
| 108 | links_json: "[]".to_owned(), |
| 109 | created_at: DateTime::from_timestamp(1_790_000_000, 0).unwrap(), |
| 110 | edited_at: None, |
| 111 | deleted: false, |
| 112 | body_html: html.into(), |
| 113 | attachments_json: "[]".into(), |
| 114 | reactions_json: "[]".into(), |
| 115 | mentions_json: "[]".into(), |
| 116 | subject: subject.map(str::to_owned), |
| 117 | call_event_json: None, |
| 118 | importance: Default::default(), |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | #[test] |
| 123 | fn the_subject_is_the_post_subject_or_the_conversation_title() { |
| 124 | assert_eq!( |
| 125 | share_subject(&record(Some("Launch"), "<p>x</p>"), "General"), |
| 126 | "Launch" |
| 127 | ); |
| 128 | assert_eq!( |
| 129 | share_subject(&record(Some(" "), "<p>x</p>"), "General"), |
| 130 | "General" |
| 131 | ); |
| 132 | assert_eq!( |
| 133 | share_subject(&record(None, "<p>x</p>"), "Release planning"), |
| 134 | "Release planning" |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | #[test] |
| 139 | fn the_body_has_sender_and_time_then_the_text_then_the_link() { |
| 140 | let body = share_body( |
| 141 | &record(None, "<p>Ship it <b>today</b></p>"), |
| 142 | Some("https://teams.microsoft.com/l/message/1"), |
| 143 | ); |
| 144 | let parts: Vec<&str> = body.split("\n\n").collect(); |
| 145 | assert_eq!(parts.len(), 3); |
| 146 | assert!(parts[0].starts_with("Ada Example ")); |
| 147 | assert!(parts[1].contains("Ship it")); |
| 148 | assert_eq!(parts[2], "https://teams.microsoft.com/l/message/1"); |
| 149 | assert_eq!( |
| 150 | share_body(&record(None, "<p>hi</p>"), None) |
| 151 | .split("\n\n") |
| 152 | .count(), |
| 153 | 2 |
| 154 | ); |
| 155 | } |
| 156 | } |