Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(channels): make reaction ordering stable across refreshes (#4907)
Jacob Beckermanad3f2c23b3db
diff --git a/Cargo.lock b/Cargo.lockindex 553ea8f8f3..d36552feee 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -2587,6 +2587,7 @@ dependencies = ["frecency","futures","http-body-util",+ "indexmap 2.14.0","item_filters","macro_db_migrator","macro_env",diff --git a/crates/channels/Cargo.toml b/crates/channels/Cargo.tomlindex 36756f20fb..b333b4de35 100644--- a/crates/channels/Cargo.toml+++ b/crates/channels/Cargo.toml@@ -48,6 +48,7 @@ outbound = ["dep:contacts","dep:entity_access","dep:entity_access_db_utils",+ "dep:indexmap","dep:macro_uuid","dep:model-entity","dep:model_notifications",@@ -80,6 +81,7 @@ entity_access_db_utils = { path = "../entity_access_db_utils", optional = true }filter_ast = { path = "../filter_ast", optional = true }frecency = { path = "../frecency", optional = true }futures = { workspace = true, optional = true }+indexmap = { workspace = true, optional = true }item_filters = { path = "../item_filters", optional = true }macro_event_broker = { path = "../macro_event_broker", default-features = false }macro_event_topics = { path = "../macro_event_topics" }diff --git a/crates/channels/src/outbound/pg_channels_repo.rs b/crates/channels/src/outbound/pg_channels_repo.rsindex 22211229ca..3b38955383 100644--- a/crates/channels/src/outbound/pg_channels_repo.rs+++ b/crates/channels/src/outbound/pg_channels_repo.rs@@ -39,6 +39,8 @@ use sqlx::{Executor, PgPool, Postgres};#[cfg(feature = "list")]use sqlx::{QueryBuilder, Row, postgres::PgRow};use std::collections::{HashMap, HashSet};++use indexmap::IndexMap;use uuid::Uuid;/// Postgres-backed repository for channels.@@ -1925,8 +1927,10 @@ impl ChannelRepo for PgChannelsRepo {.fetch_all(&self.pool).await?;- // Group by message_id, then fold by emoji within each message.- let mut map: HashMap<Uuid, HashMap<String, Vec<String>>> = HashMap::new();+ // Group by message_id, then fold by emoji within each message. Rows arrive+ // ordered by created_at ASC, so an IndexMap preserves first-reacted order per+ // emoji instead of the random order a HashMap would iterate in.+ let mut map: HashMap<Uuid, IndexMap<String, Vec<String>>> = HashMap::new();for r in rows {map.entry(r.message_id).or_default()diff --git a/crates/channels/src/outbound/pg_channels_repo/tests.rs b/crates/channels/src/outbound/pg_channels_repo/tests.rsindex 411fe29e3e..612b45a6ad 100644--- a/crates/channels/src/outbound/pg_channels_repo/tests.rs+++ b/crates/channels/src/outbound/pg_channels_repo/tests.rs@@ -1001,8 +1001,17 @@ async fn reactions_grouped_by_emoji(pool: Pool<Postgres>) -> anyhow::Result<()>let repo = repo(pool);let map = repo.get_reactions_batch(&[MSG1, MSG3]).await?;- // msg1 has thumbsup (2 users) and tada (1 user)+ // msg1 has thumbsup (2 users) and tada (1 user), and should always come back in+ // first-reacted-at order (thumbsup before tada) rather than shuffled, since the+ // fixture reacts thumbsup before tada.let msg1_reactions = map.get(&MSG1).unwrap();+ assert_eq!(+ msg1_reactions+ .iter()+ .map(|r| r.emoji.as_str())+ .collect::<Vec<_>>(),+ vec!["\u{1f44d}", "\u{1f389}"]+ );let thumbsup = msg1_reactions.iter().find(|r| r.emoji == "\u{1f44d}")