| 1 | use std::collections::{HashMap, HashSet}; |
| 2 | |
| 3 | use chrono::{DateTime, Utc}; |
| 4 | use mcp::ReactionProposal; |
| 5 | use store::{Sidebar, Store}; |
| 6 | |
| 7 | use super::{ProposalEntry, ProposalStatus, agent_label}; |
| 8 | use crate::activity::preview_label; |
| 9 | use crate::app_state::chat_title; |
| 10 | use crate::notify::preview_of; |
| 11 | use crate::rows::reaction_glyph; |
| 12 | |
| 13 | const EXCERPT_CHARACTERS: usize = 120; |
| 14 | |
| 15 | #[derive(Debug, Clone, PartialEq)] |
| 16 | pub enum PendingKind { |
| 17 | Reply { |
| 18 | text: String, |
| 19 | }, |
| 20 | Reaction { |
| 21 | glyph: String, |
| 22 | message_id: String, |
| 23 | excerpt: Option<String>, |
| 24 | }, |
| 25 | } |
| 26 | |
| 27 | #[derive(Debug, Clone, PartialEq)] |
| 28 | pub struct PendingProposal { |
| 29 | pub id: String, |
| 30 | pub conversation_id: String, |
| 31 | pub agent: &'static str, |
| 32 | pub kind: PendingKind, |
| 33 | pub created_at: DateTime<Utc>, |
| 34 | pub seen: bool, |
| 35 | } |
| 36 | |
| 37 | impl PendingProposal { |
| 38 | pub fn title(&self) -> String { |
| 39 | let what = match self.kind { |
| 40 | PendingKind::Reply { .. } => "a reply", |
| 41 | PendingKind::Reaction { .. } => "a reaction", |
| 42 | }; |
| 43 | format!("{} proposes {what}", self.agent) |
| 44 | } |
| 45 | |
| 46 | pub fn headline(&self, conversation: Option<&str>) -> String { |
| 47 | match conversation { |
| 48 | Some(conversation) => format!("{} in {conversation}", self.title()), |
| 49 | None => self.title(), |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | pub fn detail(&self) -> String { |
| 54 | match &self.kind { |
| 55 | PendingKind::Reply { text } => single_line(text), |
| 56 | PendingKind::Reaction { |
| 57 | glyph, |
| 58 | excerpt: Some(excerpt), |
| 59 | .. |
| 60 | } => format!("{glyph} to: {excerpt}"), |
| 61 | PendingKind::Reaction { glyph, .. } => glyph.clone(), |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | pub fn jump_message_id(&self) -> &str { |
| 66 | match &self.kind { |
| 67 | PendingKind::Reply { .. } => "", |
| 68 | PendingKind::Reaction { message_id, .. } => message_id, |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | fn single_line(text: &str) -> String { |
| 74 | text.split_whitespace().collect::<Vec<_>>().join(" ") |
| 75 | } |
| 76 | |
| 77 | pub fn collect( |
| 78 | proposals: &HashMap<String, ProposalEntry>, |
| 79 | reactions: &HashMap<(String, String), ReactionProposal>, |
| 80 | excerpts: &HashMap<String, String>, |
| 81 | seen: &HashSet<String>, |
| 82 | ) -> Vec<PendingProposal> { |
| 83 | let replies = proposals |
| 84 | .values() |
| 85 | .filter(|entry| !matches!(entry.status, ProposalStatus::Stale { .. })) |
| 86 | .map(|entry| PendingProposal { |
| 87 | id: entry.proposal.id.clone(), |
| 88 | conversation_id: entry.proposal.conversation_id.clone(), |
| 89 | agent: agent_label(entry.proposal.agent.as_deref()), |
| 90 | kind: PendingKind::Reply { |
| 91 | text: entry.proposal.text.clone(), |
| 92 | }, |
| 93 | created_at: entry.proposal.created_at, |
| 94 | seen: seen.contains(&entry.proposal.id), |
| 95 | }); |
| 96 | let reacts = reactions.values().map(|reaction| PendingProposal { |
| 97 | id: reaction.id.clone(), |
| 98 | conversation_id: reaction.conversation_id.clone(), |
| 99 | agent: agent_label(reaction.agent.as_deref()), |
| 100 | kind: PendingKind::Reaction { |
| 101 | glyph: reaction_glyph(&reaction.emoji), |
| 102 | message_id: reaction.message_id.clone(), |
| 103 | excerpt: excerpts.get(&reaction.id).cloned(), |
| 104 | }, |
| 105 | created_at: reaction.created_at, |
| 106 | seen: seen.contains(&reaction.id), |
| 107 | }); |
| 108 | let mut pending: Vec<PendingProposal> = replies.chain(reacts).collect(); |
| 109 | pending.sort_by(|left, right| { |
| 110 | right |
| 111 | .created_at |
| 112 | .cmp(&left.created_at) |
| 113 | .then_with(|| left.id.cmp(&right.id)) |
| 114 | }); |
| 115 | pending |
| 116 | } |
| 117 | |
| 118 | pub fn conversation_label(sidebar: &Sidebar, conversation_id: &str) -> Option<String> { |
| 119 | if let Some(chat) = sidebar.chats.iter().find(|chat| chat.id == conversation_id) { |
| 120 | return Some(chat_title(chat)); |
| 121 | } |
| 122 | sidebar.teams.iter().find_map(|team| { |
| 123 | team.channels |
| 124 | .iter() |
| 125 | .find(|channel| channel.id == conversation_id) |
| 126 | .map(|channel| format!("{} > {}", team.team.name, channel.name)) |
| 127 | }) |
| 128 | } |
| 129 | |
| 130 | pub fn reacted_excerpt(store: &Store, conversation_id: &str, message_id: &str) -> Option<String> { |
| 131 | let records = store |
| 132 | .messages_by_id(conversation_id, &[message_id.to_owned()]) |
| 133 | .ok()?; |
| 134 | let record = records.get(message_id)?; |
| 135 | let text = single_line(&preview_label(&preview_of(record))); |
| 136 | Some(text.chars().take(EXCERPT_CHARACTERS).collect()) |
| 137 | } |
| 138 | |
| 139 | pub fn decode_seen(saved: Option<String>) -> HashSet<String> { |
| 140 | saved |
| 141 | .unwrap_or_default() |
| 142 | .lines() |
| 143 | .filter(|line| !line.is_empty()) |
| 144 | .map(str::to_owned) |
| 145 | .collect() |
| 146 | } |
| 147 | |
| 148 | pub fn encode_seen(seen: &HashSet<String>) -> String { |
| 149 | let mut ids: Vec<&str> = seen.iter().map(String::as_str).collect(); |
| 150 | ids.sort_unstable(); |
| 151 | ids.join("\n") |
| 152 | } |
| 153 | |
| 154 | #[cfg(test)] |
| 155 | mod tests { |
| 156 | use std::collections::{HashMap, HashSet}; |
| 157 | use std::sync::Arc; |
| 158 | |
| 159 | use chrono::{Local, TimeZone, Utc}; |
| 160 | use mcp::{Proposal, ReactionProposal}; |
| 161 | |
| 162 | use super::*; |
| 163 | |
| 164 | fn reply( |
| 165 | conversation_id: &str, |
| 166 | id: &str, |
| 167 | minute: u32, |
| 168 | status: ProposalStatus, |
| 169 | ) -> ProposalEntry { |
| 170 | ProposalEntry { |
| 171 | proposal: Proposal { |
| 172 | id: id.into(), |
| 173 | conversation_id: conversation_id.into(), |
| 174 | reply_to_message_id: None, |
| 175 | text: "Looks\n good to me".into(), |
| 176 | mentions: Vec::new(), |
| 177 | images: Vec::new(), |
| 178 | agent: Some("claude".into()), |
| 179 | session_id: None, |
| 180 | created_at: Utc.with_ymd_and_hms(2026, 10, 18, 9, minute, 0).unwrap(), |
| 181 | }, |
| 182 | body: Arc::new(Vec::new()), |
| 183 | mention_labels: Vec::new(), |
| 184 | images: Vec::new(), |
| 185 | status, |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | fn reaction(id: &str, minute: u32) -> ReactionProposal { |
| 190 | ReactionProposal { |
| 191 | id: id.into(), |
| 192 | conversation_id: "chat-b".into(), |
| 193 | message_id: "m7".into(), |
| 194 | emoji: "like".into(), |
| 195 | agent: Some("copilot".into()), |
| 196 | session_id: None, |
| 197 | created_at: Utc.with_ymd_and_hms(2026, 10, 18, 9, minute, 0).unwrap(), |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | #[test] |
| 202 | fn collect_lists_pending_replies_and_reactions_newest_first() { |
| 203 | let proposals = HashMap::from([ |
| 204 | ( |
| 205 | "chat-a".to_owned(), |
| 206 | reply("chat-a", "p1", 5, ProposalStatus::Pending), |
| 207 | ), |
| 208 | ( |
| 209 | "chat-c".to_owned(), |
| 210 | reply( |
| 211 | "chat-c", |
| 212 | "p2", |
| 213 | 9, |
| 214 | ProposalStatus::Stale { |
| 215 | answered_at: Local::now(), |
| 216 | }, |
| 217 | ), |
| 218 | ), |
| 219 | ]); |
| 220 | let reactions = |
| 221 | HashMap::from([(("chat-b".to_owned(), "m7".to_owned()), reaction("r1", 7))]); |
| 222 | let excerpts = HashMap::from([("r1".to_owned(), "Ship it?".to_owned())]); |
| 223 | let seen = HashSet::from(["p1".to_owned()]); |
| 224 | let pending = collect(&proposals, &reactions, &excerpts, &seen); |
| 225 | let ids: Vec<&str> = pending.iter().map(|item| item.id.as_str()).collect(); |
| 226 | assert_eq!(ids, ["r1", "p1"]); |
| 227 | assert!(!pending[0].seen); |
| 228 | assert!(pending[1].seen); |
| 229 | assert_eq!(pending[0].title(), "Copilot proposes a reaction"); |
| 230 | assert_eq!(pending[0].jump_message_id(), "m7"); |
| 231 | assert_eq!(pending[1].jump_message_id(), ""); |
| 232 | assert_eq!( |
| 233 | pending[1].headline(Some("Release planning")), |
| 234 | "Claude proposes a reply in Release planning" |
| 235 | ); |
| 236 | assert_eq!(pending[1].detail(), "Looks good to me"); |
| 237 | assert!(pending[0].detail().ends_with(" to: Ship it?")); |
| 238 | } |
| 239 | |
| 240 | #[test] |
| 241 | fn seen_ids_round_trip() { |
| 242 | let seen = HashSet::from(["b".to_owned(), "a".to_owned()]); |
| 243 | assert_eq!(encode_seen(&seen), "a\nb"); |
| 244 | assert_eq!(decode_seen(Some(encode_seen(&seen))), seen); |
| 245 | assert!(decode_seen(None).is_empty()); |
| 246 | } |
| 247 | } |