BlueGit

ict rusty-teams

Files

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

post_follow.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
post_follow.rs 5.6 KiB Download Raw
1use std::collections::{HashMap, HashSet};
2
3use gpui_kit::*;
4use teams_core::PostFollowState;
5
6use crate::app_state::AppState;
7use crate::runtime;
8
9pub const TURN_OFF_NOTIFICATIONS: &str = "Turn off notifications";
10pub const TURN_ON_NOTIFICATIONS: &str = "Turn on notifications";
11
12type PostKey = (String, String);
13
14#[derive(Default)]
15pub struct PostFollows {
16 states: HashMap<PostKey, PostFollowState>,
17 requested: HashSet<PostKey>,
18}
19
20fn post_key(channel_id: &str, root_id: &str) -> PostKey {
21 (channel_id.to_owned(), root_id.to_owned())
22}
23
24/// Teams reads the explicit reply chain state first, then implicit participation.
25pub fn post_followed(state: Option<PostFollowState>, participated: bool) -> bool {
26 match state {
27 Some(state) => state.followed(),
28 None => participated,
29 }
30}
31
32pub fn follow_label(followed: bool) -> &'static str {
33 if followed {
34 TURN_OFF_NOTIFICATIONS
35 } else {
36 TURN_ON_NOTIFICATIONS
37 }
38}
39
40pub fn follow_notice(followed: bool) -> &'static str {
41 if followed {
42 "Notifications turned on"
43 } else {
44 "Notifications turned off"
45 }
46}
47
48pub fn follow_failure(followed: bool) -> &'static str {
49 if followed {
50 "Couldn't turn on notifications"
51 } else {
52 "Couldn't turn off notifications"
53 }
54}
55
56impl AppState {
57 pub fn post_follow_state(&self, channel_id: &str, root_id: &str) -> Option<PostFollowState> {
58 self.post_follows
59 .states
60 .get(&post_key(channel_id, root_id))
61 .copied()
62 }
63
64 pub fn request_post_follow_state(
65 &mut self,
66 channel_id: &str,
67 root_id: &str,
68 cx: &mut Context<Self>,
69 ) {
70 let key = post_key(channel_id, root_id);
71 if !self.post_follows.requested.insert(key) {
72 return;
73 }
74 let Some(engine) = self.engine.clone() else {
75 return;
76 };
77 let (channel, root) = (channel_id.to_owned(), root_id.to_owned());
78 let requested = vec![root.clone()];
79 let requested_channel = channel.clone();
80 let receiver = runtime::spawn(async move {
81 engine
82 .post_follow_states(&requested_channel, &requested)
83 .await
84 });
85 cx.spawn(async move |this, cx| {
86 let outcome = receiver.await;
87 this.update(cx, |state, cx| match outcome {
88 Ok(Ok(states)) => {
89 if let Some(follow_state) = states.get(&root) {
90 state
91 .post_follows
92 .states
93 .insert(post_key(&channel, &root), *follow_state);
94 cx.notify();
95 }
96 }
97 _ => {
98 state
99 .post_follows
100 .requested
101 .remove(&post_key(&channel, &root));
102 }
103 })
104 .ok();
105 })
106 .detach();
107 }
108
109 pub fn set_post_followed(
110 &mut self,
111 channel_id: &str,
112 root_id: &str,
113 followed: bool,
114 cx: &mut Context<Self>,
115 ) {
116 let Some(engine_or_demo) = self.chat_action_engine(cx) else {
117 return;
118 };
119 let key = post_key(channel_id, root_id);
120 let previous = self.post_follows.states.get(&key).copied();
121 let next = if followed {
122 PostFollowState::ExplicitFollow
123 } else {
124 PostFollowState::ExplicitUnfollow
125 };
126 self.post_follows.states.insert(key.clone(), next);
127 cx.notify();
128 let Some(engine) = engine_or_demo else {
129 self.raise_notice(follow_notice(followed).to_owned(), None, cx);
130 return;
131 };
132 let (channel, root) = (channel_id.to_owned(), root_id.to_owned());
133 self.run_channel_action(
134 follow_failure(followed),
135 async move {
136 let annotation_failure =
137 engine.set_post_followed(&channel, &root, followed).await?;
138 if let Some(error) = annotation_failure {
139 log::warn!("post follow annotation not written: {error}");
140 }
141 Ok(())
142 },
143 move |state, cx| {
144 match previous {
145 Some(previous) => state.post_follows.states.insert(key, previous),
146 None => state.post_follows.states.remove(&key),
147 };
148 cx.notify();
149 },
150 Some(follow_notice(followed)),
151 cx,
152 );
153 }
154}
155
156#[cfg(test)]
157mod tests {
158 use teams_core::PostFollowState;
159
160 use super::{follow_failure, follow_label, follow_notice, post_followed};
161
162 #[test]
163 fn explicit_state_wins_over_participation() {
164 assert!(!post_followed(
165 Some(PostFollowState::ExplicitUnfollow),
166 true
167 ));
168 assert!(post_followed(Some(PostFollowState::ExplicitFollow), false));
169 assert!(post_followed(Some(PostFollowState::ImplicitFollow), false));
170 assert!(post_followed(None, true));
171 assert!(!post_followed(None, false));
172 }
173
174 #[test]
175 fn labels_and_notices_use_teams_strings() {
176 assert_eq!(follow_label(true), "Turn off notifications");
177 assert_eq!(follow_label(false), "Turn on notifications");
178 assert_eq!(follow_notice(false), "Notifications turned off");
179 assert_eq!(follow_notice(true), "Notifications turned on");
180 assert_eq!(follow_failure(false), "Couldn't turn off notifications");
181 assert_eq!(follow_failure(true), "Couldn't turn on notifications");
182 }
183}