BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / share_outlook.rs

share_outlook.rs

share_outlook.rs 4.9 KiB Download Raw
1use chrono::{DateTime, Local, Utc};
2use gpui_kit::*;
3use store::MessageRecord;
4
5use crate::app_state::AppState;
6use crate::notice::short_error;
7use crate::rows::message_text;
8use crate::runtime;
9
10pub const SHARE_TO_OUTLOOK_LABEL: &str = "Share to Outlook";
11
12pub 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
22pub 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
36fn 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
45impl 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 cx.open_url(&teams_core::outlook_compose_link(&subject, &body));
62 }
63 return;
64 };
65 let (conversation_id, message_id) = (conversation_id.to_owned(), message_id.to_owned());
66 let receiver =
67 runtime::spawn(async move { engine.message_link(&conversation_id, &message_id).await });
68 cx.spawn(async move |this, cx| {
69 let link = match receiver.await {
70 Ok(Ok(link)) => Ok(link),
71 Ok(Err(error)) => Err(short_error(&error)),
72 Err(_) => Err("cancelled".to_owned()),
73 };
74 this.update(cx, |state, cx| {
75 if let Err(reason) = &link {
76 state.raise_notice(format!("No link to the message: {reason}"), None, cx);
77 }
78 let body = share_body(&record, link.as_deref().ok());
79 cx.open_url(&teams_core::outlook_compose_link(&subject, &body));
80 })
81 .ok();
82 })
83 .detach();
84 }
85}
86
87#[cfg(test)]
88mod tests {
89 use chrono::DateTime;
90 use store::MessageRecord;
91
92 use super::{share_body, share_subject};
93
94 fn record(subject: Option<&str>, html: &str) -> MessageRecord {
95 MessageRecord {
96 conversation_id: "19:c@thread.v2".into(),
97 message_id: "1".into(),
98 reply_to_id: None,
99 sender_id: Some("u".into()),
100 sender_name: Some("Ada Example".into()),
101 sender_application_id: None,
102 links_json: "[]".to_owned(),
103 created_at: DateTime::from_timestamp(1_790_000_000, 0).unwrap(),
104 edited_at: None,
105 deleted: false,
106 body_html: html.into(),
107 attachments_json: "[]".into(),
108 reactions_json: "[]".into(),
109 mentions_json: "[]".into(),
110 subject: subject.map(str::to_owned),
111 call_event_json: None,
112 importance: Default::default(),
113 }
114 }
115
116 #[test]
117 fn the_subject_is_the_post_subject_or_the_conversation_title() {
118 assert_eq!(
119 share_subject(&record(Some("Launch"), "<p>x</p>"), "General"),
120 "Launch"
121 );
122 assert_eq!(
123 share_subject(&record(Some(" "), "<p>x</p>"), "General"),
124 "General"
125 );
126 assert_eq!(
127 share_subject(&record(None, "<p>x</p>"), "Release planning"),
128 "Release planning"
129 );
130 }
131
132 #[test]
133 fn the_body_has_sender_and_time_then_the_text_then_the_link() {
134 let body = share_body(
135 &record(None, "<p>Ship it <b>today</b></p>"),
136 Some("https://teams.microsoft.com/l/message/1"),
137 );
138 let parts: Vec<&str> = body.split("\n\n").collect();
139 assert_eq!(parts.len(), 3);
140 assert!(parts[0].starts_with("Ada Example "));
141 assert!(parts[1].contains("Ship it"));
142 assert_eq!(parts[2], "https://teams.microsoft.com/l/message/1");
143 assert_eq!(
144 share_body(&record(None, "<p>hi</p>"), None)
145 .split("\n\n")
146 .count(),
147 2
148 );
149 }
150}