BlueGit

ict rusty-teams

Files

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

post_follow.rs

post_follow.rs 5.5 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.post_follows.requested.remove(&post_key(&channel, &root));
99 }
100 })
101 .ok();
102 })
103 .detach();
104 }
105
106 pub fn set_post_followed(
107 &mut self,
108 channel_id: &str,
109 root_id: &str,
110 followed: bool,
111 cx: &mut Context<Self>,
112 ) {
113 let Some(engine_or_demo) = self.chat_action_engine(cx) else {
114 return;
115 };
116 let key = post_key(channel_id, root_id);
117 let previous = self.post_follows.states.get(&key).copied();
118 let next = if followed {
119 PostFollowState::ExplicitFollow
120 } else {
121 PostFollowState::ExplicitUnfollow
122 };
123 self.post_follows.states.insert(key.clone(), next);
124 cx.notify();
125 let Some(engine) = engine_or_demo else {
126 self.raise_notice(follow_notice(followed).to_owned(), None, cx);
127 return;
128 };
129 let (channel, root) = (channel_id.to_owned(), root_id.to_owned());
130 self.run_channel_action(
131 follow_failure(followed),
132 async move {
133 let annotation_failure = engine.set_post_followed(&channel, &root, followed).await?;
134 if let Some(error) = annotation_failure {
135 log::warn!("post follow annotation not written: {error}");
136 }
137 Ok(())
138 },
139 move |state, cx| {
140 match previous {
141 Some(previous) => state.post_follows.states.insert(key, previous),
142 None => state.post_follows.states.remove(&key),
143 };
144 cx.notify();
145 },
146 Some(follow_notice(followed)),
147 cx,
148 );
149 }
150}
151
152#[cfg(test)]
153mod tests {
154 use teams_core::PostFollowState;
155
156 use super::{follow_failure, follow_label, follow_notice, post_followed};
157
158 #[test]
159 fn explicit_state_wins_over_participation() {
160 assert!(!post_followed(Some(PostFollowState::ExplicitUnfollow), true));
161 assert!(post_followed(Some(PostFollowState::ExplicitFollow), false));
162 assert!(post_followed(Some(PostFollowState::ImplicitFollow), false));
163 assert!(post_followed(None, true));
164 assert!(!post_followed(None, false));
165 }
166
167 #[test]
168 fn labels_and_notices_use_teams_strings() {
169 assert_eq!(follow_label(true), "Turn off notifications");
170 assert_eq!(follow_label(false), "Turn on notifications");
171 assert_eq!(follow_notice(false), "Notifications turned off");
172 assert_eq!(follow_notice(true), "Notifications turned on");
173 assert_eq!(follow_failure(false), "Couldn't turn off notifications");
174 assert_eq!(follow_failure(true), "Couldn't turn on notifications");
175 }
176}