BlueGit

ict rusty-teams

Files

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

share_outlook.rs

share_outlook.rs 4.8 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 sent_at
38 .with_timezone(&Local)
39 .format("%d/%m/%Y %H:%M")
40 .to_string()
41}
42
43impl AppState {
44 pub fn share_to_outlook(
45 &mut self,
46 conversation_id: &str,
47 message_id: &str,
48 conversation_title: &str,
49 cx: &mut Context<Self>,
50 ) {
51 let Some(record) = self.message_record(conversation_id, message_id) else {
52 return;
53 };
54 let subject = share_subject(&record, conversation_title);
55 let Some(engine) = self.engine.clone() else {
56 if self.mode.demo {
57 let link = teams_core::chat_message_link(conversation_id, message_id, None);
58 let body = share_body(&record, Some(&link));
59 cx.open_url(&teams_core::outlook_compose_link(&subject, &body));
60 }
61 return;
62 };
63 let (conversation_id, message_id) = (conversation_id.to_owned(), message_id.to_owned());
64 let receiver =
65 runtime::spawn(async move { engine.message_link(&conversation_id, &message_id).await });
66 cx.spawn(async move |this, cx| {
67 let link = match receiver.await {
68 Ok(Ok(link)) => Ok(link),
69 Ok(Err(error)) => Err(short_error(&error)),
70 Err(_) => Err("cancelled".to_owned()),
71 };
72 this.update(cx, |state, cx| {
73 if let Err(reason) = &link {
74 state.raise_notice(format!("No link to the message: {reason}"), None, cx);
75 }
76 let body = share_body(&record, link.as_deref().ok());
77 cx.open_url(&teams_core::outlook_compose_link(&subject, &body));
78 })
79 .ok();
80 })
81 .detach();
82 }
83}
84
85#[cfg(test)]
86mod tests {
87 use chrono::DateTime;
88 use store::MessageRecord;
89
90 use super::{share_body, share_subject};
91
92 fn record(subject: Option<&str>, html: &str) -> MessageRecord {
93 MessageRecord {
94 conversation_id: "19:c@thread.v2".into(),
95 message_id: "1".into(),
96 reply_to_id: None,
97 sender_id: Some("u".into()),
98 sender_name: Some("Ada Example".into()),
99 sender_application_id: None,
100 links_json: "[]".to_owned(),
101 created_at: DateTime::from_timestamp(1_790_000_000, 0).unwrap(),
102 edited_at: None,
103 deleted: false,
104 body_html: html.into(),
105 attachments_json: "[]".into(),
106 reactions_json: "[]".into(),
107 mentions_json: "[]".into(),
108 subject: subject.map(str::to_owned),
109 call_event_json: None,
110 importance: Default::default(),
111 }
112 }
113
114 #[test]
115 fn the_subject_is_the_post_subject_or_the_conversation_title() {
116 assert_eq!(
117 share_subject(&record(Some("Launch"), "<p>x</p>"), "General"),
118 "Launch"
119 );
120 assert_eq!(
121 share_subject(&record(Some(" "), "<p>x</p>"), "General"),
122 "General"
123 );
124 assert_eq!(
125 share_subject(&record(None, "<p>x</p>"), "Release planning"),
126 "Release planning"
127 );
128 }
129
130 #[test]
131 fn the_body_has_sender_and_time_then_the_text_then_the_link() {
132 let body = share_body(
133 &record(None, "<p>Ship it <b>today</b></p>"),
134 Some("https://teams.microsoft.com/l/message/1"),
135 );
136 let parts: Vec<&str> = body.split("\n\n").collect();
137 assert_eq!(parts.len(), 3);
138 assert!(parts[0].starts_with("Ada Example "));
139 assert!(parts[1].contains("Ship it"));
140 assert_eq!(parts[2], "https://teams.microsoft.com/l/message/1");
141 assert_eq!(
142 share_body(&record(None, "<p>hi</p>"), None)
143 .split("\n\n")
144 .count(),
145 2
146 );
147 }
148}