BlueGit

ict rusty-teams

Files

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

default_reactions.rs

default_reactions.rs 5.9 KiB Download Raw
1use gpui_kit::Context;
2
3use crate::app_state::AppState;
4use crate::emoticons;
5use crate::rows::reaction_glyph;
6
7pub fn quick_reaction_glyphs(reaction_ids: &[String], skin_tone: u8) -> Vec<String> {
8 reaction_ids
9 .iter()
10 .filter_map(|reaction_id| chatsvc::reaction_glyph_for_key(reaction_id))
11 .map(|glyph| emoticons::with_tone(&glyph, skin_tone))
12 .collect()
13}
14
15/// The most used reactions first, the account defaults fill the remaining slots.
16pub fn quick_reactions_with_usage(
17 most_used: Vec<String>,
18 defaults: Vec<String>,
19 skin_tone: u8,
20) -> Vec<String> {
21 let count = if defaults.is_empty() {
22 teams_core::TEAMS_DEFAULT_REACTIONS.len()
23 } else {
24 defaults.len()
25 };
26 let mut quick: Vec<String> = Vec::with_capacity(count);
27 let toned_used = most_used
28 .into_iter()
29 .map(|glyph| emoticons::with_tone(&glyph, skin_tone));
30 for glyph in toned_used.chain(defaults) {
31 if quick.len() == count {
32 break;
33 }
34 let base = reaction_glyph(&emoticons::without_tone(&glyph));
35 if !quick
36 .iter()
37 .any(|known| reaction_glyph(&emoticons::without_tone(known)) == base)
38 {
39 quick.push(glyph);
40 }
41 }
42 quick
43}
44
45pub fn reaction_id_for_glyph(glyph: &str) -> Option<String> {
46 chatsvc::reaction_key(&emoticons::without_tone(glyph))
47}
48
49pub fn slots_for(reaction_ids: &[String]) -> Vec<Option<String>> {
50 let length = reaction_ids.len().max(teams_core::DEFAULT_REACTION_SLOTS);
51 (0..length)
52 .map(|index| reaction_ids.get(index).cloned())
53 .collect()
54}
55
56/// Teams' replace rule: an id already in another slot leaves that slot empty.
57pub fn place_in_slot(slots: &mut [Option<String>], index: usize, reaction_id: String) {
58 for slot in slots.iter_mut() {
59 if slot.as_deref() == Some(reaction_id.as_str()) {
60 *slot = None;
61 }
62 }
63 if let Some(slot) = slots.get_mut(index) {
64 *slot = Some(reaction_id);
65 }
66}
67
68pub fn filled(slots: &[Option<String>]) -> Vec<String> {
69 slots.iter().flatten().cloned().collect()
70}
71
72impl AppState {
73 pub fn default_reactions(&self) -> Vec<String> {
74 teams_core::default_reactions(&self.store)
75 }
76
77 pub fn quick_reactions(&self, most_used: Vec<String>) -> Vec<String> {
78 let skin_tone = teams_core::skin_tone(&self.store);
79 quick_reactions_with_usage(
80 most_used,
81 quick_reaction_glyphs(&self.default_reactions(), skin_tone),
82 skin_tone,
83 )
84 }
85
86 pub fn set_default_reactions(&mut self, reaction_ids: Vec<String>, cx: &mut Context<Self>) {
87 let Some(engine_or_demo) = self.chat_action_engine(cx) else {
88 return;
89 };
90 let previous = self.default_reactions();
91 let _ = teams_core::store_account_default_reactions(&self.store, Some(&reaction_ids));
92 cx.notify();
93 let Some(engine) = engine_or_demo else {
94 return;
95 };
96 self.run_chat_action(
97 "Customising default reactions",
98 async move { engine.set_default_reactions(&reaction_ids).await },
99 move |state, cx| {
100 let _ = teams_core::store_account_default_reactions(&state.store, Some(&previous));
101 cx.notify();
102 },
103 |_, _| {},
104 cx,
105 );
106 }
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112
113 fn ids(values: &[&str]) -> Vec<String> {
114 values.iter().map(|value| (*value).to_owned()).collect()
115 }
116
117 #[test]
118 fn most_used_come_first_and_defaults_fill_the_rest() {
119 let defaults = quick_reaction_glyphs(&ids(&teams_core::TEAMS_DEFAULT_REACTIONS), 0);
120 let quick = quick_reactions_with_usage(
121 vec!["\u{1F680}".to_owned(), "\u{2764}\u{FE0F}".to_owned()],
122 defaults.clone(),
123 0,
124 );
125 assert_eq!(quick.len(), defaults.len());
126 assert_eq!(quick[0], "\u{1F680}");
127 assert_eq!(quick[1], "\u{2764}\u{FE0F}");
128 assert_eq!(quick[2], defaults[0]);
129 assert_eq!(
130 quick.iter().filter(|glyph| reaction_glyph(glyph) == "\u{2764}").count(),
131 1
132 );
133 }
134
135 #[test]
136 fn used_reactions_take_the_account_tone() {
137 let defaults = quick_reaction_glyphs(&ids(&teams_core::TEAMS_DEFAULT_REACTIONS), 2);
138 let quick = quick_reactions_with_usage(vec!["\u{1F44D}".to_owned()], defaults, 2);
139 assert_eq!(quick[0], "\u{1F44D}\u{1F3FC}");
140 assert_eq!(quick.iter().filter(|glyph| glyph.starts_with('\u{1F44D}')).count(), 1);
141 }
142
143 #[test]
144 fn teams_defaults_show_as_glyphs_and_like_takes_the_tone() {
145 let defaults = ids(&teams_core::TEAMS_DEFAULT_REACTIONS);
146 assert_eq!(
147 quick_reaction_glyphs(&defaults, 0),
148 ["\u{1F44D}", "\u{2764}", "\u{1F606}", "\u{1F62E}"]
149 );
150 assert_eq!(quick_reaction_glyphs(&defaults, 2)[0], "\u{1F44D}\u{1F3FC}");
151 assert_eq!(
152 chatsvc::reaction_key(&quick_reaction_glyphs(&defaults, 2)[0]).as_deref(),
153 Some("yes-tone2")
154 );
155 }
156
157 #[test]
158 fn unknown_ids_are_left_out() {
159 assert_eq!(
160 quick_reaction_glyphs(&ids(&["heart", "custom-gone"]), 0),
161 ["\u{2764}"]
162 );
163 }
164
165 #[test]
166 fn picked_glyphs_store_the_untoned_id() {
167 assert_eq!(
168 reaction_id_for_glyph("\u{1F44D}\u{1F3FC}").as_deref(),
169 Some("like")
170 );
171 assert_eq!(
172 reaction_id_for_glyph("\u{2764}\u{FE0F}").as_deref(),
173 Some("heart")
174 );
175 }
176
177 #[test]
178 fn there_are_always_five_slots_and_a_repeat_moves() {
179 let mut slots = slots_for(&ids(&["like", "heart"]));
180 assert_eq!(slots.len(), 5);
181 place_in_slot(&mut slots, 3, "like".to_owned());
182 assert_eq!(filled(&slots), ["heart", "like"]);
183 assert_eq!(slots[0], None);
184 assert_eq!(slots_for(&ids(&["a", "b", "c", "d", "e", "f"])).len(), 6);
185 }
186}