BlueGit

ict rusty-teams

Files

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

delivery_options.rs

delivery_options.rs 7.8 KiB Download Raw
1use gpui_kit::Context;
2use store::MessageImportance;
3use teams_core::NOTES_CHAT_ID;
4
5use crate::app_state::AppState;
6use crate::data::is_one_on_one;
7use crate::runtime;
8
9pub const DELIVERY_BUTTON_TITLE: &str = "Set delivery options";
10pub const URGENT_MAX_MEMBERS: usize = 20;
11pub const URGENT_TOO_MANY_MEMBERS: &str = "Unavailable in chats with more than 20 people";
12const MEETING_KIND: &str = "meeting";
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub struct DeliveryOption {
16 pub importance: MessageImportance,
17 pub header: &'static str,
18 pub content: &'static str,
19 pub symbol: &'static str,
20 pub tag: &'static str,
21}
22
23pub const STANDARD: DeliveryOption = DeliveryOption {
24 importance: MessageImportance::Normal,
25 header: "Standard",
26 content: "Message will be sent as usual",
27 symbol: "chat_bubble",
28 tag: "standard-delivery-option",
29};
30
31pub const IMPORTANT: DeliveryOption = DeliveryOption {
32 importance: MessageImportance::High,
33 header: "Important",
34 content: "Message will be marked as important",
35 symbol: "priority_high",
36 tag: "important-delivery-option",
37};
38
39pub const URGENT: DeliveryOption = DeliveryOption {
40 importance: MessageImportance::Urgent,
41 header: "Urgent",
42 content: "Recipient(s) notified every 2 min for 20 min",
43 symbol: "notification_important",
44 tag: "urgent-delivery-option",
45};
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub enum UrgentAvailability {
49 Hidden,
50 Disabled,
51 Available,
52}
53
54#[derive(Debug, Clone, Copy, Default)]
55pub struct DeliveryContext<'a> {
56 pub conversation_id: &'a str,
57 pub chat_kind: &'a str,
58 pub member_count: usize,
59 pub channel: bool,
60 pub one_on_one_bot: bool,
61 pub editing: bool,
62 pub policy_allows_urgent: bool,
63}
64
65/// Teams hides Urgent while editing, in notes, bot 1:1 chats, meeting chats, channels and when the messaging policy denies priority messages; more than 20 people disable it.
66pub fn urgent_availability(context: &DeliveryContext) -> UrgentAvailability {
67 let hidden = context.editing
68 || !context.policy_allows_urgent
69 || context.conversation_id == NOTES_CHAT_ID
70 || context.one_on_one_bot
71 || context.channel
72 || context.chat_kind.eq_ignore_ascii_case(MEETING_KIND);
73 if hidden {
74 UrgentAvailability::Hidden
75 } else if context.member_count > URGENT_MAX_MEMBERS {
76 UrgentAvailability::Disabled
77 } else {
78 UrgentAvailability::Available
79 }
80}
81
82impl AppState {
83 pub fn urgent_availability(&self, conversation_id: &str, editing: bool) -> UrgentAvailability {
84 let chat = self
85 .sidebar
86 .chats
87 .iter()
88 .find(|chat| chat.id == conversation_id);
89 let one_on_one_bot = chat.is_some_and(|chat| {
90 is_one_on_one(chat)
91 && self
92 .bot_apps
93 .get(&chat.id)
94 .is_some_and(|apps| apps.iter().any(|app| !app.bot_ids.is_empty()))
95 });
96 urgent_availability(&DeliveryContext {
97 conversation_id,
98 chat_kind: chat.map_or("", |chat| chat.kind.as_str()),
99 member_count: chat.map_or(0, |chat| chat.members.len()),
100 channel: chat.is_none() && !conversation_id.is_empty(),
101 one_on_one_bot,
102 editing,
103 policy_allows_urgent: self.allow_priority_messages,
104 })
105 }
106
107 pub fn refresh_messaging_policy(&mut self, cx: &mut Context<Self>) {
108 let Some(engine) = self.engine.clone() else {
109 return;
110 };
111 let read = runtime::spawn(async move { engine.allow_priority_messages().await.ok() });
112 cx.spawn(async move |this, cx| {
113 let Some(allowed) = read.await.ok().flatten() else {
114 return;
115 };
116 this.update(cx, |state, cx| {
117 state.allow_priority_messages = allowed;
118 cx.notify();
119 })
120 .ok();
121 })
122 .detach();
123 }
124}
125
126pub fn delivery_options(urgent: UrgentAvailability) -> Vec<(DeliveryOption, bool)> {
127 let mut options = vec![(STANDARD, true), (IMPORTANT, true)];
128 match urgent {
129 UrgentAvailability::Hidden => {}
130 UrgentAvailability::Disabled => options.push((URGENT, false)),
131 UrgentAvailability::Available => options.push((URGENT, true)),
132 }
133 options
134}
135
136/// The importance that can still be sent once the chat or the policy changed.
137pub fn allowed_importance(
138 importance: MessageImportance,
139 urgent: UrgentAvailability,
140) -> MessageImportance {
141 if importance == MessageImportance::Urgent && urgent != UrgentAvailability::Available {
142 MessageImportance::Normal
143 } else {
144 importance
145 }
146}
147
148pub fn urgent_banner(member_count: usize) -> &'static str {
149 if member_count > 2 {
150 "Notify recipients every 2 minutes for 20 minutes"
151 } else {
152 "Notify recipient every 2 minutes for 20 minutes"
153 }
154}
155
156#[cfg(test)]
157mod tests {
158 use super::*;
159
160 fn group(member_count: usize) -> DeliveryContext<'static> {
161 DeliveryContext {
162 conversation_id: "19:group@thread.v2",
163 chat_kind: "group",
164 member_count,
165 policy_allows_urgent: true,
166 ..Default::default()
167 }
168 }
169
170 #[test]
171 fn urgent_is_offered_in_ordinary_chats() {
172 assert_eq!(
173 urgent_availability(&group(5)),
174 UrgentAvailability::Available
175 );
176 let options: Vec<_> = delivery_options(UrgentAvailability::Available)
177 .into_iter()
178 .map(|(option, enabled)| (option.header, enabled))
179 .collect();
180 assert_eq!(
181 options,
182 [("Standard", true), ("Important", true), ("Urgent", true)]
183 );
184 }
185
186 #[test]
187 fn urgent_is_hidden_where_teams_hides_it() {
188 let hidden = [
189 DeliveryContext {
190 conversation_id: NOTES_CHAT_ID,
191 ..group(1)
192 },
193 DeliveryContext {
194 chat_kind: "meeting",
195 ..group(4)
196 },
197 DeliveryContext {
198 one_on_one_bot: true,
199 ..group(1)
200 },
201 DeliveryContext {
202 channel: true,
203 ..group(4)
204 },
205 DeliveryContext {
206 editing: true,
207 ..group(4)
208 },
209 DeliveryContext {
210 policy_allows_urgent: false,
211 ..group(4)
212 },
213 ];
214 for context in hidden {
215 assert_eq!(
216 urgent_availability(&context),
217 UrgentAvailability::Hidden,
218 "{context:?}"
219 );
220 }
221 assert_eq!(delivery_options(UrgentAvailability::Hidden).len(), 2);
222 }
223
224 #[test]
225 fn more_than_twenty_people_disable_urgent() {
226 assert_eq!(
227 urgent_availability(&group(20)),
228 UrgentAvailability::Available
229 );
230 assert_eq!(
231 urgent_availability(&group(21)),
232 UrgentAvailability::Disabled
233 );
234 assert_eq!(
235 delivery_options(UrgentAvailability::Disabled)[2],
236 (URGENT, false)
237 );
238 }
239
240 #[test]
241 fn a_chosen_urgent_falls_back_to_standard_where_it_is_not_allowed() {
242 assert_eq!(
243 allowed_importance(MessageImportance::Urgent, UrgentAvailability::Disabled),
244 MessageImportance::Normal
245 );
246 assert_eq!(
247 allowed_importance(MessageImportance::High, UrgentAvailability::Hidden),
248 MessageImportance::High
249 );
250 }
251
252 #[test]
253 fn the_banner_uses_the_plural_for_more_than_two_members() {
254 assert_eq!(
255 urgent_banner(2),
256 "Notify recipient every 2 minutes for 20 minutes"
257 );
258 assert_eq!(
259 urgent_banner(3),
260 "Notify recipients every 2 minutes for 20 minutes"
261 );
262 }
263}