| 1 | use crate::rows::reaction_glyph; |
| 2 | |
| 3 | const LIMIT: usize = 24; |
| 4 | const META_KEY: &str = "emoji_recent"; |
| 5 | const TEAMS_QUICK_REACTIONS: [&str; 6] = ["👍", "❤️", "😆", "😮", "😢", "😡"]; |
| 6 | const COUNT_SEPARATOR: char = '\t'; |
| 7 | |
| 8 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 9 | struct Entry { |
| 10 | glyph: String, |
| 11 | uses: u32, |
| 12 | } |
| 13 | |
| 14 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 15 | pub struct Recent(Vec<Entry>); |
| 16 | |
| 17 | impl Default for Recent { |
| 18 | fn default() -> Self { |
| 19 | Recent( |
| 20 | TEAMS_QUICK_REACTIONS |
| 21 | .map(|glyph| Entry { |
| 22 | glyph: glyph.to_owned(), |
| 23 | uses: 0, |
| 24 | }) |
| 25 | .to_vec(), |
| 26 | ) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | impl Recent { |
| 31 | pub fn load(store: &store::Store) -> Self { |
| 32 | match store.meta(META_KEY) { |
| 33 | Ok(Some(stored)) => Self::parse(&stored), |
| 34 | _ => Self::default(), |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | pub fn reload(&mut self, store: &store::Store) { |
| 39 | *self = Self::load(store); |
| 40 | } |
| 41 | |
| 42 | pub fn save(&self, store: &store::Store) { |
| 43 | store.set_meta(META_KEY, &self.serialize()).ok(); |
| 44 | } |
| 45 | |
| 46 | fn serialize(&self) -> String { |
| 47 | self.0 |
| 48 | .iter() |
| 49 | .map(|entry| format!("{}{COUNT_SEPARATOR}{}", entry.glyph, entry.uses)) |
| 50 | .collect::<Vec<_>>() |
| 51 | .join("\n") |
| 52 | } |
| 53 | |
| 54 | fn parse(stored: &str) -> Self { |
| 55 | Recent( |
| 56 | stored |
| 57 | .lines() |
| 58 | .filter(|line| !line.is_empty()) |
| 59 | .take(LIMIT) |
| 60 | .map(|line| match line.split_once(COUNT_SEPARATOR) { |
| 61 | Some((glyph, uses)) => Entry { |
| 62 | glyph: glyph.to_owned(), |
| 63 | uses: uses.parse().unwrap_or(0), |
| 64 | }, |
| 65 | None => Entry { |
| 66 | glyph: line.to_owned(), |
| 67 | uses: 0, |
| 68 | }, |
| 69 | }) |
| 70 | .collect(), |
| 71 | ) |
| 72 | } |
| 73 | |
| 74 | pub fn glyphs(&self) -> Vec<String> { |
| 75 | self.0.iter().map(|entry| entry.glyph.clone()).collect() |
| 76 | } |
| 77 | |
| 78 | pub fn most_used(&self, count: usize) -> Vec<String> { |
| 79 | let mut ranked: Vec<&Entry> = self.0.iter().collect(); |
| 80 | ranked.sort_by_key(|entry| std::cmp::Reverse(entry.uses)); |
| 81 | ranked |
| 82 | .into_iter() |
| 83 | .take(count) |
| 84 | .map(|entry| entry.glyph.clone()) |
| 85 | .collect() |
| 86 | } |
| 87 | |
| 88 | pub fn push(&mut self, glyph: &str) { |
| 89 | self.bump(glyph, 0); |
| 90 | } |
| 91 | |
| 92 | pub fn record_use(&mut self, glyph: &str) { |
| 93 | self.bump(glyph, 1); |
| 94 | } |
| 95 | |
| 96 | fn bump(&mut self, glyph: &str, uses: u32) { |
| 97 | let previous_uses = self |
| 98 | .0 |
| 99 | .iter() |
| 100 | .position(|entry| reaction_glyph(&entry.glyph) == reaction_glyph(glyph)) |
| 101 | .map_or(0, |position| self.0.remove(position).uses); |
| 102 | self.0.insert( |
| 103 | 0, |
| 104 | Entry { |
| 105 | glyph: glyph.to_owned(), |
| 106 | uses: previous_uses + uses, |
| 107 | }, |
| 108 | ); |
| 109 | self.0.truncate(LIMIT); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | #[cfg(test)] |
| 114 | mod tests { |
| 115 | use super::{LIMIT, Recent}; |
| 116 | |
| 117 | #[test] |
| 118 | fn starts_with_the_teams_quick_reactions() { |
| 119 | assert_eq!(Recent::default().glyphs()[..2], ["👍", "❤️"]); |
| 120 | assert_eq!(Recent::default().most_used(4), ["👍", "❤️", "😆", "😮"]); |
| 121 | } |
| 122 | |
| 123 | #[test] |
| 124 | fn newest_first_without_duplicates() { |
| 125 | let mut recent = Recent::parse(""); |
| 126 | recent.record_use("👍"); |
| 127 | recent.record_use("❤️"); |
| 128 | recent.record_use("👍"); |
| 129 | assert_eq!(recent.glyphs(), ["👍", "❤️"]); |
| 130 | } |
| 131 | |
| 132 | #[test] |
| 133 | fn keeps_at_most_the_limit() { |
| 134 | let mut recent = Recent::parse(""); |
| 135 | for index in 0..LIMIT + 5 { |
| 136 | recent.record_use(&index.to_string()); |
| 137 | } |
| 138 | assert_eq!(recent.glyphs().len(), LIMIT); |
| 139 | assert_eq!(Recent::parse(&recent.serialize()), recent); |
| 140 | } |
| 141 | |
| 142 | #[test] |
| 143 | fn ranks_by_count_then_recency() { |
| 144 | let mut recent = Recent::parse(""); |
| 145 | for glyph in ["🎉", "🎉", "🔥", "👀", "👀", "✅", "🚀"] { |
| 146 | recent.record_use(glyph); |
| 147 | } |
| 148 | assert_eq!(recent.most_used(4), ["👀", "🎉", "🚀", "✅"]); |
| 149 | } |
| 150 | |
| 151 | #[test] |
| 152 | fn matches_glyphs_with_and_without_variation_selector() { |
| 153 | let mut recent = Recent::parse(""); |
| 154 | recent.record_use("\u{2764}\u{FE0F}"); |
| 155 | recent.record_use("\u{2764}"); |
| 156 | assert_eq!(recent.glyphs().len(), 1); |
| 157 | assert_eq!(recent.most_used(1), ["\u{2764}"]); |
| 158 | } |
| 159 | |
| 160 | #[test] |
| 161 | fn push_moves_to_front_without_counting() { |
| 162 | let mut recent = Recent::default(); |
| 163 | recent.push("😮"); |
| 164 | assert_eq!(recent.glyphs()[0], "😮"); |
| 165 | assert_eq!(recent.most_used(1), ["😮"]); |
| 166 | recent.record_use("👍"); |
| 167 | assert_eq!(recent.most_used(1), ["👍"]); |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn legacy_values_without_counts_still_load() { |
| 172 | let recent = Recent::parse("🎉\n👍\n"); |
| 173 | assert_eq!(recent.glyphs(), ["🎉", "👍"]); |
| 174 | assert_eq!(recent.most_used(2), ["🎉", "👍"]); |
| 175 | } |
| 176 | |
| 177 | #[test] |
| 178 | fn counts_survive_a_round_trip() { |
| 179 | let mut recent = Recent::parse(""); |
| 180 | recent.record_use("👍"); |
| 181 | recent.record_use("👍"); |
| 182 | recent.record_use("🔥"); |
| 183 | assert_eq!(Recent::parse(&recent.serialize()), recent); |
| 184 | assert_eq!(recent.most_used(1), ["👍"]); |
| 185 | } |
| 186 | } |